Skip to content

Commit

Permalink
Rearrange format_error/2 arguments to match existing conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
jcomellas committed Jul 19, 2013
1 parent 2f0a25a commit 078d003
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/getopt.erl
Expand Up @@ -142,22 +142,25 @@ parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, []) ->
%% not present but had default arguments in the specification.
{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.
-spec format_error({error, {Reason :: atom(), Data :: term()}}, [option_spec()]) -> string().
format_error({error, {Reason, Data}}, OptSpecList) ->
format_error({Reason, Data}, OptSpecList);
format_error({missing_required_option, Name}, OptSpecList) ->
-spec format_error([option_spec()], {error, {Reason :: atom(), Data :: term()}} |
{Reason :: term(), Data :: term()}) -> string().
format_error(OptSpecList, {error, Reason}) ->
format_error(OptSpecList, Reason);
format_error(OptSpecList, {missing_required_option, Name}) ->
{_Name, Short, Long, _Type, _Help} = lists:keyfind(Name, 1, OptSpecList),
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);
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)]);
format_error({invalid_option_arg, OptStr}, _OptSpecList) ->
format_error(_OptSpecList, {invalid_option_arg, 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)]).


%% @doc Parse a long option, add it to the option accumulator and continue
%% parsing the rest of the arguments recursively.
%% A long option can have the following syntax:
Expand Down
9 changes: 6 additions & 3 deletions test/getopt_test.erl
Expand Up @@ -292,10 +292,13 @@ check_test_() ->
{ok, {Opts, _}} = parse(OptSpecList, ""),
[
{"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",
?_assertEqual({error, {missing_required_option, arg}}, parse_and_check(OptSpecList, ""))},
{"Format error test",
{"Format error test 1",
?_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}))}
].

0 comments on commit 078d003

Please sign in to comment.