Rearrange the arguments passed to check/2 to match existing conventions
This commit is contained in:
parent
3699279ee9
commit
3a15ecc1ac
@ -63,39 +63,39 @@
|
|||||||
%% function. Additionally perform check if all required options (the ones
|
%% function. Additionally perform check if all required options (the ones
|
||||||
%% without default values) are present. The function is a combination of
|
%% without default values) are present. The function is a combination of
|
||||||
%% two calls: parse/2 and check/2.
|
%% two calls: parse/2 and check/2.
|
||||||
-spec parse_and_check([option_spec()], string | [string()]) ->
|
-spec parse_and_check([option_spec()], string() | [string()]) ->
|
||||||
{ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}.
|
{ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: term()}}.
|
||||||
parse_and_check(OptSpecList, CmdLine) when is_list(OptSpecList), is_list(CmdLine) ->
|
parse_and_check(OptSpecList, CmdLine) when is_list(OptSpecList), is_list(CmdLine) ->
|
||||||
case parse(OptSpecList, CmdLine) of
|
case parse(OptSpecList, CmdLine) of
|
||||||
{ok, {Opts, _}} = Result ->
|
{ok, {Opts, _}} = Result ->
|
||||||
case check(Opts, OptSpecList) of
|
case check(OptSpecList, Opts) of
|
||||||
ok ->
|
ok -> Result;
|
||||||
Result;
|
Error -> Error
|
||||||
Error ->
|
|
||||||
Error
|
|
||||||
end;
|
end;
|
||||||
Other ->
|
Error ->
|
||||||
Other
|
Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @doc Check the parsed command line arguments returning ok if all required
|
%% @doc Check the parsed command line arguments returning ok if all required
|
||||||
%% options (i.e. that don't have defaults) are present, and returning
|
%% options (i.e. that don't have defaults) are present, and returning
|
||||||
%% error otherwise.
|
%% error otherwise.
|
||||||
-spec check([option()], [option_spec()]) ->
|
-spec check([option_spec()], [option()]) ->
|
||||||
ok | {error, {Reason :: atom(), Option :: atom()}}.
|
ok | {error, {Reason :: atom(), Option :: atom()}}.
|
||||||
check(ParsedOpts, OptSpecList) when is_list(ParsedOpts), is_list(OptSpecList) ->
|
check(OptSpecList, ParsedOpts) when is_list(OptSpecList), is_list(ParsedOpts) ->
|
||||||
try
|
try
|
||||||
Required = [N || {N, _, _, T, _} <- OptSpecList, not is_tuple(T) andalso T =/= undefined],
|
RequiredOpts = [Name || {Name, _, _, Arg, _} <- OptSpecList,
|
||||||
lists:foreach(fun(O) ->
|
not is_tuple(Arg) andalso Arg =/= undefined],
|
||||||
case proplists:is_defined(O, ParsedOpts) of
|
lists:foreach(fun (Option) ->
|
||||||
|
case proplists:is_defined(Option, ParsedOpts) of
|
||||||
true ->
|
true ->
|
||||||
ok;
|
ok;
|
||||||
false ->
|
false ->
|
||||||
throw({error, {missing_required_option, O}})
|
throw({error, {missing_required_option, Option}})
|
||||||
end
|
end
|
||||||
end, Required)
|
end, RequiredOpts)
|
||||||
catch _:Error ->
|
catch
|
||||||
Error
|
_:Error ->
|
||||||
|
Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
@ -103,7 +103,7 @@ check(ParsedOpts, OptSpecList) when is_list(ParsedOpts), is_list(OptSpecList) ->
|
|||||||
%% and/or atoms using the Erlang convention for sending options to a
|
%% and/or atoms using the Erlang convention for sending options to a
|
||||||
%% function.
|
%% function.
|
||||||
-spec parse([option_spec()], string() | [string()]) ->
|
-spec parse([option_spec()], string() | [string()]) ->
|
||||||
{ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}.
|
{ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: term()}}.
|
||||||
parse(OptSpecList, CmdLine) when is_list(CmdLine) ->
|
parse(OptSpecList, CmdLine) when is_list(CmdLine) ->
|
||||||
try
|
try
|
||||||
Args = if
|
Args = if
|
||||||
@ -143,7 +143,7 @@ parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, []) ->
|
|||||||
{ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc)}}.
|
{ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc)}}.
|
||||||
|
|
||||||
%% @doc Format the error code returned by prior call to parse/2 or check/2.
|
%% @doc Format the error code returned by prior call to parse/2 or check/2.
|
||||||
-spec format_error({error, {Reason :: atom(), Data :: any()}}, [option_spec()]) -> string().
|
-spec format_error({error, {Reason :: atom(), Data :: term()}}, [option_spec()]) -> string().
|
||||||
format_error({error, {Reason, Data}}, OptSpecList) ->
|
format_error({error, {Reason, Data}}, OptSpecList) ->
|
||||||
format_error({Reason, Data}, OptSpecList);
|
format_error({Reason, Data}, OptSpecList);
|
||||||
format_error({missing_required_option, Name}, OptSpecList) ->
|
format_error({missing_required_option, Name}, OptSpecList) ->
|
||||||
@ -898,15 +898,14 @@ line_length() ->
|
|||||||
?LINE_LENGTH
|
?LINE_LENGTH
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec to_string(any()) -> string().
|
|
||||||
to_string(L) when is_list(L) ->
|
-spec to_string(term()) -> string().
|
||||||
case io_lib:printable_list(L) of
|
to_string(List) when is_list(List) ->
|
||||||
true -> L;
|
case io_lib:printable_list(List) of
|
||||||
false -> io_lib:format("~p", [L])
|
true -> List;
|
||||||
|
false -> io_lib:format("~p", [List])
|
||||||
end;
|
end;
|
||||||
to_string(A) when is_atom(A) ->
|
to_string(Atom) when is_atom(Atom) ->
|
||||||
atom_to_list(A);
|
atom_to_list(Atom);
|
||||||
to_string(T) ->
|
to_string(Value) ->
|
||||||
io_lib:format("~p", [T]).
|
io_lib:format("~p", [Value]).
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user