Skip to content

Commit

Permalink
Merge pull request #42 from suprematic/master
Browse files Browse the repository at this point in the history
Format "missing option argument" error
  • Loading branch information
jcomellas authored Mar 24, 2018
2 parents 8e87599 + fe997f2 commit 784b0ea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/getopt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,29 @@ parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, []) ->
format_error(OptSpecList, {error, Reason}) ->
format_error(OptSpecList, Reason);
format_error(OptSpecList, {missing_required_option, Name}) ->
OptStr = case lists:keyfind(Name, 1, OptSpecList) of
{Name, undefined, undefined, _Type, _Help} -> ["<", to_string(Name), ">"];
{_Name, undefined, Long, _Type, _Help} -> ["--", Long];
{_Name, Short, undefined, _Type, _Help} -> ["-", Short];
{_Name, Short, Long, _Type, _Help} -> ["-", Short, " (", Long, ")"]
end,
OptStr = opt_to_list(lists:keyfind(Name, 1, OptSpecList)),
lists:flatten(["missing required option: ", OptStr]);
format_error(_OptSpecList, {invalid_option, OptStr}) ->
lists:flatten(["invalid option: ", to_string(OptStr)]);
format_error(_OptSpecList, {invalid_option_arg, {Name, Arg}}) ->
lists:flatten(["option \'", to_string(Name) ++ "\' has invalid argument: ", to_string(Arg)]);
format_error(_OptSpecList, {invalid_option_arg, OptStr}) ->
lists:flatten(["invalid option argument: ", to_string(OptStr)]);
format_error(OptSpecList, {missing_option_arg, Name}) ->
OptStr = opt_to_list(lists:keyfind(Name, 1, OptSpecList)),
lists:flatten(["missing option argument: ", OptStr, " <", to_string(Name), $>]);
format_error(_OptSpecList, {Reason, Data}) ->
lists:flatten([to_string(Reason), " ", to_string(Data)]).

opt_to_list({Name, undefined, undefined, _Type, _Help}) ->
[$<, to_string(Name), $>];
opt_to_list({_Name, undefined, Long, _Type, _Help}) ->
[$-, $-, Long];
opt_to_list({_Name, Short, undefined, _Type, _Help}) ->
[$-, Short];
opt_to_list({_Name, Short, Long, _Type, _Help}) ->
[$-, Short, $\s, $(, Long, $)].


%% @doc Parse a long option, add it to the option accumulator and continue
%% parsing the rest of the arguments recursively.
Expand Down
29 changes: 28 additions & 1 deletion test/getopt_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,19 @@ check_test_() ->
?_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, ""))},
{"Parse arguments and check required option args",
?_assertEqual({error, {missing_option_arg, arg}},
parse_and_check(OptSpecList, "-a"))}].

format_error_test_() ->
OptSpecList =
[
{ arg, $a, "arg", string, "Required arg"},
{ short, $s, undefined, string, "short option"},
{ long, undefined, "long", string, "long option"},
{ other, undefined, undefined, string, "task"}
],
[
{"Format missing option error test 1",
?_assertEqual("missing required option: -a (arg)",
format_error(OptSpecList, {error, {missing_required_option, arg}}))},
Expand All @@ -321,7 +334,21 @@ check_test_() ->
format_error(OptSpecList, {error, {invalid_option_arg, "arg_value"}}))},
{"Format invalid option argument error test 2",
?_assertEqual("option 'verbose' has invalid argument: 100",
format_error(OptSpecList, {error, {invalid_option_arg, {verbose, "100"}}}))}
format_error(OptSpecList, {error, {invalid_option_arg, {verbose, "100"}}}))},
{"Format missing option argument error test 1",
?_assertEqual("missing option argument: -a (arg) <arg>",
format_error(OptSpecList, {error, {missing_option_arg, arg}}))},
{"Format missing option argument error test 2",
?_assertEqual("missing option argument: -a (arg) <arg>",
format_error(OptSpecList, {missing_option_arg, arg}))},
{"Format missing option argument error test 3",
?_assertEqual("missing option argument: -s <short>",
format_error(OptSpecList, {missing_option_arg, short}))},
{"Format missing option argument error test 4",
?_assertEqual("missing option argument: --long <long>",
format_error(OptSpecList, {missing_option_arg, long}))},
{"Format missing option argument error test 5",
?_assertError(_, format_error(OptSpecList, {missing_option_arg, unknown}))}
].

utf8_binary_test_() ->
Expand Down

0 comments on commit 784b0ea

Please sign in to comment.