From 078d003a93f04ff956d2040d0aafdab1ad2ed662 Mon Sep 17 00:00:00 2001 From: Juan Jose Comellas Date: Fri, 19 Jul 2013 02:00:36 -0300 Subject: [PATCH] Rearrange format_error/2 arguments to match existing conventions --- src/getopt.erl | 19 +++++++++++-------- test/getopt_test.erl | 9 ++++++--- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/getopt.erl b/src/getopt.erl index 59e9cb7..d5c31cb 100644 --- a/src/getopt.erl +++ b/src/getopt.erl @@ -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: diff --git a/test/getopt_test.erl b/test/getopt_test.erl index d0b5626..a642382 100644 --- a/test/getopt_test.erl +++ b/test/getopt_test.erl @@ -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}))} ].