Rearrange the arguments passed to check/2 to match existing conventions
Juan Jose Comellas
10 years ago
62 | 62 | %% function. Additionally perform check if all required options (the ones |
63 | 63 | %% without default values) are present. The function is a combination of |
64 | 64 | %% two calls: parse/2 and check/2. |
65 | -spec parse_and_check([option_spec()], string | [string()]) -> | |
66 | {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}. | |
65 | -spec parse_and_check([option_spec()], string() | [string()]) -> | |
66 | {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: term()}}. | |
67 | 67 | parse_and_check(OptSpecList, CmdLine) when is_list(OptSpecList), is_list(CmdLine) -> |
68 | 68 | case parse(OptSpecList, CmdLine) of |
69 | 69 | {ok, {Opts, _}} = Result -> |
70 | case check(Opts, OptSpecList) of | |
71 | ok -> | |
72 | Result; | |
73 | Error -> | |
74 | Error | |
70 | case check(OptSpecList, Opts) of | |
71 | ok -> Result; | |
72 | Error -> Error | |
75 | 73 | end; |
76 | Other -> | |
77 | Other | |
74 | Error -> | |
75 | Error | |
78 | 76 | end. |
79 | 77 | |
80 | 78 | %% @doc Check the parsed command line arguments returning ok if all required |
81 | 79 | %% options (i.e. that don't have defaults) are present, and returning |
82 | 80 | %% error otherwise. |
83 | -spec check([option()], [option_spec()]) -> | |
81 | -spec check([option_spec()], [option()]) -> | |
84 | 82 | ok | {error, {Reason :: atom(), Option :: atom()}}. |
85 | check(ParsedOpts, OptSpecList) when is_list(ParsedOpts), is_list(OptSpecList) -> | |
83 | check(OptSpecList, ParsedOpts) when is_list(OptSpecList), is_list(ParsedOpts) -> | |
86 | 84 | try |
87 | Required = [N || {N, _, _, T, _} <- OptSpecList, not is_tuple(T) andalso T =/= undefined], | |
88 | lists:foreach(fun(O) -> | |
89 | case proplists:is_defined(O, ParsedOpts) of | |
85 | RequiredOpts = [Name || {Name, _, _, Arg, _} <- OptSpecList, | |
86 | not is_tuple(Arg) andalso Arg =/= undefined], | |
87 | lists:foreach(fun (Option) -> | |
88 | case proplists:is_defined(Option, ParsedOpts) of | |
90 | 89 | true -> |
91 | 90 | ok; |
92 | 91 | false -> |
93 | throw({error, {missing_required_option, O}}) | |
92 | throw({error, {missing_required_option, Option}}) | |
94 | 93 | end |
95 | end, Required) | |
96 | catch _:Error -> | |
97 | Error | |
94 | end, RequiredOpts) | |
95 | catch | |
96 | _:Error -> | |
97 | Error | |
98 | 98 | end. |
99 | 99 | |
100 | 100 | |
102 | 102 | %% and/or atoms using the Erlang convention for sending options to a |
103 | 103 | %% function. |
104 | 104 | -spec parse([option_spec()], string() | [string()]) -> |
105 | {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}. | |
105 | {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: term()}}. | |
106 | 106 | parse(OptSpecList, CmdLine) when is_list(CmdLine) -> |
107 | 107 | try |
108 | 108 | Args = if |
142 | 142 | {ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc)}}. |
143 | 143 | |
144 | 144 | %% @doc Format the error code returned by prior call to parse/2 or check/2. |
145 | -spec format_error({error, {Reason :: atom(), Data :: any()}}, [option_spec()]) -> string(). | |
145 | -spec format_error({error, {Reason :: atom(), Data :: term()}}, [option_spec()]) -> string(). | |
146 | 146 | format_error({error, {Reason, Data}}, OptSpecList) -> |
147 | 147 | format_error({Reason, Data}, OptSpecList); |
148 | 148 | format_error({missing_required_option, Name}, OptSpecList) -> |
897 | 897 | ?LINE_LENGTH |
898 | 898 | end. |
899 | 899 | |
900 | -spec to_string(any()) -> string(). | |
901 | to_string(L) when is_list(L) -> | |
902 | case io_lib:printable_list(L) of | |
903 | true -> L; | |
904 | false -> io_lib:format("~p", [L]) | |
900 | ||
901 | -spec to_string(term()) -> string(). | |
902 | to_string(List) when is_list(List) -> | |
903 | case io_lib:printable_list(List) of | |
904 | true -> List; | |
905 | false -> io_lib:format("~p", [List]) | |
905 | 906 | end; |
906 | to_string(A) when is_atom(A) -> | |
907 | atom_to_list(A); | |
908 | to_string(T) -> | |
909 | io_lib:format("~p", [T]). | |
910 | ||
911 | ||
907 | to_string(Atom) when is_atom(Atom) -> | |
908 | atom_to_list(Atom); | |
909 | to_string(Value) -> | |
910 | io_lib:format("~p", [Value]). |