The command line arguments can now be passed as string.
This commit is contained in:
parent
5a1d192b4a
commit
08392f62fe
@ -24,7 +24,7 @@ Usage
|
||||
|
||||
The *getopt* module provides two functions:
|
||||
|
||||
parse([#option{}], Args :: [string()]) -> {ok, {Options, NonOptionArgs}} | {error, {Reason, Data}}
|
||||
parse([#option{}], Args :: string() | [string()]) -> {ok, {Options, NonOptionArgs}} | {error, {Reason, Data}}
|
||||
usage([#option{}], ProgramName :: string()) -> ok
|
||||
|
||||
The ``parse/2`` function receives a list of ``option`` records (defined in
|
||||
@ -102,19 +102,19 @@ e.g. For a program named ex1.escript with the following option specifications:
|
||||
}
|
||||
].
|
||||
|
||||
And this command line invocation:
|
||||
And this command line:
|
||||
|
||||
$ ./ex1.escript -h myhost --port 1000 -x myfile.txt dummy1 dummy2
|
||||
Args = "-h myhost --port 1000 -x myfile.txt dummy1 dummy2"
|
||||
|
||||
The arguments would be:
|
||||
Which could also be passed in the format the ``main/1`` function receives the arguments in escripts:
|
||||
|
||||
Args = ["-h", "myhost", "--port", "1000", "-x", "myfile.txt", "dummy1", "dummy2"].
|
||||
|
||||
So, the call to ``getopt:parse/2``:
|
||||
The call to ``getopt:parse/2``:
|
||||
|
||||
> getopt:parse(OptSpec, Args).
|
||||
|
||||
Would return:
|
||||
Will return:
|
||||
|
||||
{ok,{[{host,"myhost"},
|
||||
{port,1000},
|
||||
@ -125,11 +125,11 @@ Would return:
|
||||
|
||||
Also, the call to ``getopt:usage/2``:
|
||||
|
||||
> getopt:usage(OptSpec, "ex1.escript").
|
||||
> getopt:usage(OptSpec, "ex1").
|
||||
|
||||
Would show on *stdout*:
|
||||
Will show (on *stdout*):
|
||||
|
||||
Usage: ex1.escript [-h <host>] [-p <port>] [--dbname <dbname>] [-x] <file>
|
||||
Usage: ex1 [-h <host>] [-p <port>] [--dbname <dbname>] [-x] <file>
|
||||
|
||||
-h, --host Database server host
|
||||
-p, --port Database server port
|
||||
|
@ -21,15 +21,21 @@
|
||||
-export([parse/2, usage/2]).
|
||||
|
||||
|
||||
-spec parse([option_spec()], [string()]) -> {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}.
|
||||
-spec parse([option_spec()], string() | [string()]) -> {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}.
|
||||
%%--------------------------------------------------------------------
|
||||
%% @spec parse(OptSpecList::[option_spec()], Args::[string()]) -> [option()].
|
||||
%% @spec parse(OptSpecList::[option_spec()], Args::string() | [string()]) -> [option()].
|
||||
%% @doc Parse the command line options and arguments returning a list of tuples
|
||||
%% and/or atoms using the Erlang convention for sending options to a
|
||||
%% function.
|
||||
%%--------------------------------------------------------------------
|
||||
parse(OptSpecList, Args) ->
|
||||
parse(OptSpecList, CmdLine) ->
|
||||
try
|
||||
Args = if
|
||||
is_integer(hd(CmdLine)) ->
|
||||
string:tokens(CmdLine, " \t\n");
|
||||
true ->
|
||||
CmdLine
|
||||
end,
|
||||
parse(OptSpecList, [], [], 0, Args)
|
||||
catch
|
||||
throw: {error, {_Reason, _Data}} = Error ->
|
||||
|
@ -184,9 +184,8 @@ parse_1_test_() ->
|
||||
{ShortArg#option.help, ?_assertMatch({ok, {[{short_arg, "value"}], []}}, parse([ShortArg], [[$-, ShortArg#option.short], "value"]))},
|
||||
{ShortDefArg#option.help, ?_assertMatch({ok, {[{short_def_arg, "default-short"}], []}}, parse([ShortDefArg], []))},
|
||||
{ShortInt#option.help, ?_assertMatch({ok, {[{short_int, 100}], []}}, parse([ShortInt], [[$-, ShortInt#option.short], "100"]))},
|
||||
{"Unsorted multiple short form options and arguments",
|
||||
?_assertMatch({ok, {[short, short2, short3], ["arg1", "arg2"]}},
|
||||
parse([Short, Short2, Short3], ["arg1", [$-, Short#option.short, Short2#option.short, Short3#option.short], "arg2"]))},
|
||||
{"Unsorted multiple short form options and arguments in a single string",
|
||||
?_assertMatch({ok, {[short, short2, short3], ["arg1", "arg2"]}}, parse([Short, Short2, Short3], "arg1 -abc arg2"))},
|
||||
{"Short form option and arguments",
|
||||
?_assertMatch({ok, {[short], ["arg1", "arg2"]}}, parse([Short], [[$-, Short#option.short], "arg1", "arg2"]))},
|
||||
{"Short form option and arguments (unsorted)",
|
||||
|
Loading…
x
Reference in New Issue
Block a user