The command line arguments can now be passed as string.

This commit is contained in:
Juan Jose Comellas
2009-10-13 18:34:51 -03:00
parent 5a1d192b4a
commit 08392f62fe
3 changed files with 20 additions and 15 deletions
+9 -3
View File
@@ -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 ->
+2 -3
View File
@@ -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)",