Rearrange format_error/2 arguments to match existing conventions

This commit is contained in:
Juan Jose Comellas 2013-07-19 02:00:36 -03:00
parent 2f0a25a13e
commit 078d003a93
2 changed files with 17 additions and 11 deletions

View File

@ -142,22 +142,25 @@ parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, []) ->
%% not present but had default arguments in the specification. %% not present but had default arguments in the specification.
{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 :: term()}}, [option_spec()]) -> string(). -spec format_error([option_spec()], {error, {Reason :: atom(), Data :: term()}} |
format_error({error, {Reason, Data}}, OptSpecList) -> {Reason :: term(), Data :: term()}) -> string().
format_error({Reason, Data}, OptSpecList); format_error(OptSpecList, {error, Reason}) ->
format_error({missing_required_option, Name}, OptSpecList) -> format_error(OptSpecList, Reason);
format_error(OptSpecList, {missing_required_option, Name}) ->
{_Name, Short, Long, _Type, _Help} = lists:keyfind(Name, 1, OptSpecList), {_Name, Short, Long, _Type, _Help} = lists:keyfind(Name, 1, OptSpecList),
lists:flatten(["missing required option: -", [Short], " (", to_string(Long), ")"]); lists:flatten(["missing required option: -", [Short], " (", to_string(Long), ")"]);
format_error({invalid_option, OptStr}, _OptSpecList) -> format_error(_OptSpecList, {invalid_option, OptStr}) ->
"invalid option: " ++ to_string(OptStr); "invalid option: " ++ to_string(OptStr);
format_error({invalid_option_arg, {Name, Arg}}, _OptSpecList) -> format_error(_OptSpecList, {invalid_option_arg, {Name, Arg}}) ->
lists:flatten(["option \'", to_string(Name) ++ "\' has invalid argument: ", to_string(Arg)]); lists:flatten(["option \'", to_string(Name) ++ "\' has invalid argument: ", to_string(Arg)]);
format_error({invalid_option_arg, OptStr}, _OptSpecList) -> format_error(_OptSpecList, {invalid_option_arg, OptStr}) ->
"invalid option argument: " ++ to_string(OptStr); "invalid option argument: " ++ to_string(OptStr);
format_error({Reason, Data}, _OptSpecList) -> format_error(_OptSpecList, {Reason, Data}) ->
lists:append([to_string(Reason), " ", to_string(Data)]). lists:append([to_string(Reason), " ", to_string(Data)]).
%% @doc Parse a long option, add it to the option accumulator and continue %% @doc Parse a long option, add it to the option accumulator and continue
%% parsing the rest of the arguments recursively. %% parsing the rest of the arguments recursively.
%% A long option can have the following syntax: %% A long option can have the following syntax:

View File

@ -292,10 +292,13 @@ check_test_() ->
{ok, {Opts, _}} = parse(OptSpecList, ""), {ok, {Opts, _}} = parse(OptSpecList, ""),
[ [
{"Check required options", {"Check required options",
?_assertEqual({error, {missing_required_option, arg}}, check(Opts, OptSpecList))}, ?_assertEqual({error, {missing_required_option, arg}}, check(OptSpecList, Opts))},
{"Parse arguments and check required options", {"Parse arguments and check required options",
?_assertEqual({error, {missing_required_option, arg}}, parse_and_check(OptSpecList, ""))}, ?_assertEqual({error, {missing_required_option, arg}}, parse_and_check(OptSpecList, ""))},
{"Format error test", {"Format error test 1",
?_assertEqual("missing required option: -a (arg)", ?_assertEqual("missing required option: -a (arg)",
format_error({missing_required_option, arg}, OptSpecList))} format_error(OptSpecList, {error, {missing_required_option, arg}}))},
{"Format error test 2",
?_assertEqual("missing required option: -a (arg)",
format_error(OptSpecList, {missing_required_option, arg}))}
]. ].