Compare commits

..

No commits in common. "838e67fa8dac8e99e06382df0de397765facda05" and "dcaf340a8e3358d35d4884f69e108b7fad2f5b91" have entirely different histories.

7 changed files with 32 additions and 77 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
/_build

View File

@ -1,11 +0,0 @@
language: erlang
otp_release:
- R13B04
- R14B04
- R15B03
- R16B03
- 17.5
- 18.3
- 19.3
- 20.0

View File

@ -1,18 +1,19 @@
Copyright 2009 Juan Jose Comellas Copyright (c) 2009 Juan Jose Comellas
All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without modification,
modification, are permitted provided that the following conditions are met: are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this - Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, - Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution. and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors - Neither the name of Juan Jose Comellas nor the names of its contributors may
may be used to endorse or promote products derived from this software without be used to endorse or promote products derived from this software without
specific prior written permission. specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED

View File

@ -24,10 +24,10 @@ To build the (very) limited documentation run `rebar edoc`.
To use getopt in your project you can just add it as a dependency in your To use getopt in your project you can just add it as a dependency in your
`rebar.config` file in the following way: `rebar.config` file in the following way:
```erlang ```sh
{deps, {deps,
[ [
{getopt, "1.0.2"} {getopt, "1.0.1"}
] ]
} }
``` ```

View File

@ -1,9 +1,10 @@
{application,getopt, %% -*- mode: Erlang; fill-column: 75; comment-column: 50; -*-
[{description,"Command-line options parser for Erlang"}, {application, getopt,
{vsn,"1.0.2"}, [{description, "Command-line options parser for Erlang"},
{modules,[]}, {vsn, "1.0.1"},
{registered,[]}, {modules, []},
{maintainers,["Juan Jose Comellas"]}, {registered, []},
{licenses,["BSD"]}, {maintainers, ["Juan Jose Comellas"]},
{links,[{"GitHub","https://github.com/jcomellas/getopt"}]}, {licenses, ["BSD"]},
{applications,[kernel,stdlib]}]}. {links, [{"GitHub", "https://github.com/jcomellas/getopt"}]},
{applications, [kernel, stdlib]}]}.

View File

@ -149,7 +149,12 @@ parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, []) ->
format_error(OptSpecList, {error, Reason}) -> format_error(OptSpecList, {error, Reason}) ->
format_error(OptSpecList, Reason); format_error(OptSpecList, Reason);
format_error(OptSpecList, {missing_required_option, Name}) -> format_error(OptSpecList, {missing_required_option, Name}) ->
OptStr = opt_to_list(lists:keyfind(Name, 1, OptSpecList)), 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,
lists:flatten(["missing required option: ", OptStr]); lists:flatten(["missing required option: ", OptStr]);
format_error(_OptSpecList, {invalid_option, OptStr}) -> format_error(_OptSpecList, {invalid_option, OptStr}) ->
lists:flatten(["invalid option: ", to_string(OptStr)]); lists:flatten(["invalid option: ", to_string(OptStr)]);
@ -157,21 +162,9 @@ 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(_OptSpecList, {invalid_option_arg, OptStr}) -> format_error(_OptSpecList, {invalid_option_arg, OptStr}) ->
lists:flatten(["invalid option argument: ", to_string(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}) -> format_error(_OptSpecList, {Reason, Data}) ->
lists:flatten([to_string(Reason), " ", to_string(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 %% @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.

View File

@ -298,19 +298,6 @@ check_test_() ->
?_assertEqual({error, {missing_required_option, arg}}, check(OptSpecList, Opts))}, ?_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, ""))},
{"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", {"Format missing option error test 1",
?_assertEqual("missing required option: -a (arg)", ?_assertEqual("missing required option: -a (arg)",
format_error(OptSpecList, {error, {missing_required_option, arg}}))}, format_error(OptSpecList, {error, {missing_required_option, arg}}))},
@ -334,21 +321,7 @@ format_error_test_() ->
format_error(OptSpecList, {error, {invalid_option_arg, "arg_value"}}))}, format_error(OptSpecList, {error, {invalid_option_arg, "arg_value"}}))},
{"Format invalid option argument error test 2", {"Format invalid option argument error test 2",
?_assertEqual("option 'verbose' has invalid argument: 100", ?_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_() -> utf8_binary_test_() ->
@ -357,7 +330,6 @@ utf8_binary_test_() ->
Utf8 = unicode:characters_to_binary(Unicode), Utf8 = unicode:characters_to_binary(Unicode),
io:setopts(standard_error, [{encoding, utf8}]), io:setopts(standard_error, [{encoding, utf8}]),
OptSpecsWithDefault = [{utf8, undefined, "utf8", {utf8_binary, Utf8}, "UTF-8 arg"}], OptSpecsWithDefault = [{utf8, undefined, "utf8", {utf8_binary, Utf8}, "UTF-8 arg"}],
UsageBin = unicode:characters_to_binary(getopt:usage_options(OptSpecsWithDefault)),
[{"Empty utf8_binary argument", [{"Empty utf8_binary argument",
?_assertEqual({ok, {[{utf8, <<>>}], []}}, parse(OptSpecList, ["--utf8", ""]))}, ?_assertEqual({ok, {[{utf8, <<>>}], []}}, parse(OptSpecList, ["--utf8", ""]))},
{"Non empty utf8_binary argument", {"Non empty utf8_binary argument",
@ -365,4 +337,4 @@ utf8_binary_test_() ->
{"Default utf8_binary argument", {"Default utf8_binary argument",
?_assertEqual({ok, {[{utf8, Utf8}], []}}, parse(OptSpecsWithDefault, []))}, ?_assertEqual({ok, {[{utf8, Utf8}], []}}, parse(OptSpecsWithDefault, []))},
{"Default utf8_binary argument usage", {"Default utf8_binary argument usage",
?_assertEqual(1, length(binary:matches(UsageBin, Utf8)))}]. ?_assert(is_list(string:find(getopt:usage_options(OptSpecsWithDefault), Unicode)))}].