Compare commits
12 Commits
dcaf340a8e
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| dbab6262a2 | |||
| 838e67fa8d | |||
| 8b328c666b | |||
| 1c963cecd7 | |||
| fa37a75d8c | |||
| 105004ce3a | |||
| 13676822ad | |||
| 7003ec0e4b | |||
| 784b0ea877 | |||
| fe997f2f6f | |||
| 8e87599e1c | |||
| 41564e7b47 |
+18
@@ -0,0 +1,18 @@
|
||||
.rebar3
|
||||
_*
|
||||
.eunit
|
||||
*.o
|
||||
*.beam
|
||||
*.plt
|
||||
*.swp
|
||||
*.swo
|
||||
.erlang.cookie
|
||||
ebin
|
||||
log
|
||||
erl_crash.dump
|
||||
.rebar
|
||||
logs
|
||||
_build
|
||||
.idea
|
||||
*.iml
|
||||
rebar3.crashdump
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
language: erlang
|
||||
|
||||
otp_release:
|
||||
- R13B04
|
||||
- R14B04
|
||||
- R15B03
|
||||
- R16B03
|
||||
- 17.5
|
||||
- 18.3
|
||||
- 19.3
|
||||
- 20.0
|
||||
@@ -0,0 +1,26 @@
|
||||
Copyright 2009 Juan Jose Comellas
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
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
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
+11
-12
@@ -1,19 +1,18 @@
|
||||
Copyright (c) 2009 Juan Jose Comellas
|
||||
All rights reserved.
|
||||
Copyright 2009 Juan Jose Comellas
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
- Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
- Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
- Neither the name of Juan Jose Comellas nor the names of its contributors may
|
||||
be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
3. Neither the name of the copyright holder nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
`rebar.config` file in the following way:
|
||||
```sh
|
||||
```erlang
|
||||
{deps,
|
||||
[
|
||||
{getopt, "1.0.1"}
|
||||
{getopt, "1.0.3"}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
+9
-10
@@ -1,10 +1,9 @@
|
||||
%% -*- mode: Erlang; fill-column: 75; comment-column: 50; -*-
|
||||
{application, getopt,
|
||||
[{description, "Command-line options parser for Erlang"},
|
||||
{vsn, "1.0.1"},
|
||||
{modules, []},
|
||||
{registered, []},
|
||||
{maintainers, ["Juan Jose Comellas"]},
|
||||
{licenses, ["BSD"]},
|
||||
{links, [{"GitHub", "https://github.com/jcomellas/getopt"}]},
|
||||
{applications, [kernel, stdlib]}]}.
|
||||
{application,getopt,
|
||||
[{description,"Command-line options parser for Erlang"},
|
||||
{vsn,"1.0.3"},
|
||||
{modules,[]},
|
||||
{registered,[]},
|
||||
{maintainers,["Juan Jose Comellas"]},
|
||||
{licenses,["BSD"]},
|
||||
{links,[{"GitHub","https://github.com/jcomellas/getopt"}]},
|
||||
{applications,[kernel,stdlib]}]}.
|
||||
|
||||
+14
-6
@@ -9,6 +9,7 @@
|
||||
%%% retrieved from: http://www.opensource.org/licenses/bsd-license.php
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(getopt).
|
||||
-vsn("1.0.3").
|
||||
-author('juanjo@comellas.org').
|
||||
|
||||
-export([parse/2, check/2, parse_and_check/2, format_error/2,
|
||||
@@ -149,12 +150,7 @@ 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)]);
|
||||
@@ -162,9 +158,21 @@ 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.
|
||||
|
||||
+30
-2
@@ -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}}))},
|
||||
@@ -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_() ->
|
||||
@@ -330,6 +357,7 @@ utf8_binary_test_() ->
|
||||
Utf8 = unicode:characters_to_binary(Unicode),
|
||||
io:setopts(standard_error, [{encoding, utf8}]),
|
||||
OptSpecsWithDefault = [{utf8, undefined, "utf8", {utf8_binary, Utf8}, "UTF-8 arg"}],
|
||||
UsageBin = unicode:characters_to_binary(getopt:usage_options(OptSpecsWithDefault)),
|
||||
[{"Empty utf8_binary argument",
|
||||
?_assertEqual({ok, {[{utf8, <<>>}], []}}, parse(OptSpecList, ["--utf8", ""]))},
|
||||
{"Non empty utf8_binary argument",
|
||||
@@ -337,4 +365,4 @@ utf8_binary_test_() ->
|
||||
{"Default utf8_binary argument",
|
||||
?_assertEqual({ok, {[{utf8, Utf8}], []}}, parse(OptSpecsWithDefault, []))},
|
||||
{"Default utf8_binary argument usage",
|
||||
?_assert(is_list(string:find(getopt:usage_options(OptSpecsWithDefault), Unicode)))}].
|
||||
?_assertEqual(1, length(binary:matches(UsageBin, Utf8)))}].
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
{name,"Erlang GetOpt"}.
|
||||
{type,lib}.
|
||||
{modules,[]}.
|
||||
{prefix,none}.
|
||||
{author,"Juan Jose Comellas"}.
|
||||
{desc,"A getopt utility for Erlang"}.
|
||||
{package_id,{"otpr","getopt",{1,0,3}}}.
|
||||
{deps,[]}.
|
||||
{key_name,none}.
|
||||
{a_email,[]}.
|
||||
{c_email,[]}.
|
||||
{copyright,"Juan Jose Comellas"}.
|
||||
{file_exts,[]}.
|
||||
{license,"BSD-3-Clause-Attribution"}.
|
||||
{repo_url,"https://git.qpq.swiss/QPQ-AG/getopt"}.
|
||||
{tags,["args","parsing","getopt","cli"]}.
|
||||
{ws_url,[]}.
|
||||
Reference in New Issue
Block a user