Codebase list erlang-getopt / 3a15ecc
Rearrange the arguments passed to check/2 to match existing conventions Juan Jose Comellas 10 years ago
1 changed file(s) with 30 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
6262 %% function. Additionally perform check if all required options (the ones
6363 %% without default values) are present. The function is a combination of
6464 %% 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()}}.
6767 parse_and_check(OptSpecList, CmdLine) when is_list(OptSpecList), is_list(CmdLine) ->
6868 case parse(OptSpecList, CmdLine) of
6969 {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
7573 end;
76 Other ->
77 Other
74 Error ->
75 Error
7876 end.
7977
8078 %% @doc Check the parsed command line arguments returning ok if all required
8179 %% options (i.e. that don't have defaults) are present, and returning
8280 %% error otherwise.
83 -spec check([option()], [option_spec()]) ->
81 -spec check([option_spec()], [option()]) ->
8482 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) ->
8684 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
9089 true ->
9190 ok;
9291 false ->
93 throw({error, {missing_required_option, O}})
92 throw({error, {missing_required_option, Option}})
9493 end
95 end, Required)
96 catch _:Error ->
97 Error
94 end, RequiredOpts)
95 catch
96 _:Error ->
97 Error
9898 end.
9999
100100
102102 %% and/or atoms using the Erlang convention for sending options to a
103103 %% function.
104104 -spec parse([option_spec()], string() | [string()]) ->
105 {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}.
105 {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: term()}}.
106106 parse(OptSpecList, CmdLine) when is_list(CmdLine) ->
107107 try
108108 Args = if
142142 {ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc)}}.
143143
144144 %% @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().
146146 format_error({error, {Reason, Data}}, OptSpecList) ->
147147 format_error({Reason, Data}, OptSpecList);
148148 format_error({missing_required_option, Name}, OptSpecList) ->
897897 ?LINE_LENGTH
898898 end.
899899
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])
905906 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]).