Added support for embedded short options (e.g. -j2) and option
terminator (i.e. "--").
This commit is contained in:
parent
2a42bd761c
commit
5ce0e76f87
@ -34,6 +34,7 @@ option_spec_list() ->
|
|||||||
[
|
[
|
||||||
%% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
|
%% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
|
||||||
{help, $h, "help", undefined, "Show the program options"},
|
{help, $h, "help", undefined, "Show the program options"},
|
||||||
|
{jobs, $j, "jobs", {integer, 1}, "Number of concurrent jobs"},
|
||||||
{verbose, $v, "verbose", {boolean, false}, "Be verbose about what gets done"},
|
{verbose, $v, "verbose", {boolean, false}, "Be verbose about what gets done"},
|
||||||
{quiet, $q, "quiet", {boolean, false}, "Be quiet about what gets done"},
|
{quiet, $q, "quiet", {boolean, false}, "Be quiet about what gets done"},
|
||||||
{force, $f, "force", {boolean, false}, "Force"}
|
{force, $f, "force", {boolean, false}, "Force"}
|
||||||
|
@ -35,6 +35,7 @@ option_spec_list() ->
|
|||||||
[
|
[
|
||||||
%% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
|
%% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
|
||||||
{help, $h, "help", undefined, "Show the program options"},
|
{help, $h, "help", undefined, "Show the program options"},
|
||||||
|
{jobs, $j, "jobs", {integer, 1}, "Number of concurrent jobs"},
|
||||||
{verbose, $v, "verbose", {boolean, false}, "Be verbose about what gets done"},
|
{verbose, $v, "verbose", {boolean, false}, "Be verbose about what gets done"},
|
||||||
{quiet, $q, "quiet", {boolean, false}, "Be quiet about what gets done"},
|
{quiet, $q, "quiet", {boolean, false}, "Be quiet about what gets done"},
|
||||||
{force, $f, "force", {boolean, false}, "Force"}
|
{force, $f, "force", {boolean, false}, "Force"}
|
||||||
|
296
src/getopt.erl
296
src/getopt.erl
@ -69,142 +69,148 @@ parse(OptSpecList, CmdLine) ->
|
|||||||
Error
|
Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
-spec parse([option_spec()], [option()], [string()], integer(), [string()]) ->
|
-spec parse([option_spec()], [option()], [string()], integer(), [string()]) ->
|
||||||
{ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data:: any()}}.
|
{ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data:: any()}}.
|
||||||
|
%% Process the option terminator.
|
||||||
|
parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, ["--" | Tail]) ->
|
||||||
|
% Any argument present after the terminator is not considered an option.
|
||||||
|
{ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc, Tail)}};
|
||||||
%% Process long options.
|
%% Process long options.
|
||||||
parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$-, $- | Name] = OptStr | Tail]) ->
|
parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$-, $- | OptArg] = OptStr | Tail]) ->
|
||||||
{Option, Tail1} =
|
parse_option_long(OptSpecList, OptAcc, ArgAcc, ArgPos, Tail, OptStr, OptArg);
|
||||||
case split_embedded_arg(Name) of
|
|
||||||
{Name1, Arg} ->
|
|
||||||
%% Get option that has its argument within the same string
|
|
||||||
%% separated by an equal ('=') character.
|
|
||||||
{get_option_embedded_arg(OptSpecList, OptStr, ?OPT_LONG, Name1, Arg), Tail};
|
|
||||||
_Name1 ->
|
|
||||||
get_option(OptSpecList, OptStr, ?OPT_LONG, Name, Tail)
|
|
||||||
end,
|
|
||||||
parse(OptSpecList, [Option | OptAcc], ArgAcc, ArgPos, Tail1);
|
|
||||||
%% Process short options.
|
%% Process short options.
|
||||||
parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$-, ShortName] = OptStr | Tail]) ->
|
parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$- | OptArg] = OptStr | Tail]) ->
|
||||||
{Option, Tail1} = get_option(OptSpecList, OptStr, ?OPT_SHORT, ShortName, Tail),
|
parse_option_short(OptSpecList, OptAcc, ArgAcc, ArgPos, Tail, OptStr, OptArg);
|
||||||
parse(OptSpecList, [Option | OptAcc], ArgAcc, ArgPos, Tail1);
|
|
||||||
%% Process multiple short options with no argument.
|
|
||||||
parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$- | ShortNameList] = OptStr | Tail]) ->
|
|
||||||
NewOptAcc =
|
|
||||||
lists:foldl(
|
|
||||||
fun (ShortName, OptAcc1) ->
|
|
||||||
[get_option_no_arg(OptSpecList, OptStr, ShortName, ?OPT_SHORT) | OptAcc1]
|
|
||||||
end, OptAcc, ShortNameList),
|
|
||||||
parse(OptSpecList, NewOptAcc, ArgAcc, ArgPos, Tail);
|
|
||||||
%% Process non-option arguments.
|
%% Process non-option arguments.
|
||||||
parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [OptStr | Tail]) ->
|
parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [Arg | Tail]) ->
|
||||||
case split_embedded_arg(OptStr) of
|
|
||||||
{Name, Arg} ->
|
|
||||||
%% Get option that has its argument within the same string
|
|
||||||
%% separated by an equal ('=') character.
|
|
||||||
parse(OptSpecList, [get_option_embedded_arg(OptSpecList, OptStr, ?OPT_LONG, Name, Arg) | OptAcc],
|
|
||||||
ArgAcc, ArgPos, Tail);
|
|
||||||
Arg ->
|
|
||||||
case find_non_option_arg(OptSpecList, ArgPos) of
|
case find_non_option_arg(OptSpecList, ArgPos) of
|
||||||
{value, OptSpec} when ?IS_OPT_SPEC(OptSpec) ->
|
{value, OptSpec} when ?IS_OPT_SPEC(OptSpec) ->
|
||||||
parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc],
|
parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc],
|
||||||
ArgAcc, ArgPos + 1, Tail);
|
ArgAcc, ArgPos + 1, Tail);
|
||||||
false ->
|
false ->
|
||||||
parse(OptSpecList, OptAcc, [Arg | ArgAcc], ArgPos, Tail)
|
parse(OptSpecList, OptAcc, [Arg | ArgAcc], ArgPos, Tail)
|
||||||
end
|
|
||||||
end;
|
end;
|
||||||
parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, []) ->
|
parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, []) ->
|
||||||
%% Once we have completed gathering the options we add the ones that were
|
% Once we have completed gathering the options we add the ones that were
|
||||||
%% not present but had default arguments in the specification.
|
% not present but had default arguments in the specification.
|
||||||
{ok, {lists:reverse(append_default_args(OptSpecList, OptAcc)), lists:reverse(ArgAcc)}}.
|
{ok, {lists:reverse(append_default_options(OptSpecList, OptAcc)), lists:reverse(ArgAcc)}}.
|
||||||
|
|
||||||
|
|
||||||
-spec get_option([option_spec()], string(), integer(), string() | char(), [string()]) ->
|
|
||||||
{option(), [string()]}.
|
%% A long option can have the following formats:
|
||||||
%% @doc Retrieve the specification corresponding to an option matching a string
|
%% --foo Single option 'foo', no argument
|
||||||
%% received on the command line.
|
%% --foo=bar Single option 'foo', argument "bar"
|
||||||
get_option(OptSpecList, OptStr, FieldPos, OptName, Tail) ->
|
%% --foo bar Single option 'foo', argument "bar"
|
||||||
case lists:keysearch(OptName, FieldPos, OptSpecList) of
|
-spec parse_option_long([option_spec()], [option()], [string()], integer(), [string()], string(), string()) ->
|
||||||
{value, {Name, _Short, _Long, ArgSpec, _Help} = OptSpec} ->
|
{ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data:: any()}}.
|
||||||
case ArgSpec of
|
parse_option_long(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, OptArg) ->
|
||||||
undefined ->
|
case split_assigned_arg(OptArg) of
|
||||||
{Name, Tail};
|
{Long, Arg} ->
|
||||||
_ ->
|
% Get option that has its argument within the same string
|
||||||
ArgSpecType = arg_spec_type(ArgSpec),
|
% separated by an equal ('=') character (e.g. "--port=1000").
|
||||||
case Tail of
|
parse_option_assigned_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, Long, Arg);
|
||||||
[Arg | Tail1] ->
|
|
||||||
case (ArgSpecType =:= boolean) andalso not is_boolean_arg(Arg) of
|
Long ->
|
||||||
%% Special case for booleans: when the next string
|
case lists:keysearch(Long, ?OPT_LONG, OptSpecList) of
|
||||||
%% is an option we assume the value is 'true'.
|
{value, {Name, _Short, Long, undefined, _Help}} ->
|
||||||
true ->
|
parse(OptSpecList, [Name | OptAcc], ArgAcc, ArgPos, Args);
|
||||||
{{Name, true}, Tail};
|
|
||||||
_ ->
|
{value, {_Name, _Short, Long, _ArgSpec, _Help} = OptSpec} ->
|
||||||
{convert_option_arg(OptSpec, Arg), Tail1}
|
% The option argument string is empty, but the option requires
|
||||||
end;
|
% an argument, so we look into the next string in the list.
|
||||||
[] ->
|
parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptSpec);
|
||||||
case ArgSpecType of
|
|
||||||
boolean ->
|
|
||||||
{{Name, true}, Tail};
|
|
||||||
_ ->
|
|
||||||
throw({error, {missing_option_arg, Name}})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end;
|
|
||||||
false ->
|
false ->
|
||||||
throw({error, {invalid_option, OptStr}})
|
throw({error, {invalid_option, OptStr}})
|
||||||
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
-spec get_option_embedded_arg([option_spec()], string(), integer(), string(),
|
parse_option_assigned_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, Long, Arg) ->
|
||||||
string()) -> option().
|
case lists:keysearch(Long, ?OPT_LONG, OptSpecList) of
|
||||||
%% @doc Retrieve the specification corresponding to an option matching a string
|
{value, {_Name, _Short, Long, ArgSpec, _Help} = OptSpec} ->
|
||||||
%% received on the command line that had its argument assigned within the
|
|
||||||
%% same string (e.g. "verbose=true").
|
|
||||||
get_option_embedded_arg(OptSpecList, OptStr, FieldPos, OptName, Arg) ->
|
|
||||||
case lists:keysearch(OptName, FieldPos, OptSpecList) of
|
|
||||||
{value, {_Name, _Short, _Long, ArgSpec, _Help} = OptSpec} ->
|
|
||||||
case ArgSpec of
|
case ArgSpec of
|
||||||
undefined ->
|
undefined ->
|
||||||
throw({error, {invalid_option_arg, OptStr}});
|
throw({error, {invalid_option_arg, OptStr}});
|
||||||
_ ->
|
_ ->
|
||||||
convert_option_arg(OptSpec, Arg)
|
parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc], ArgAcc, ArgPos, Args)
|
||||||
end;
|
end;
|
||||||
false ->
|
false ->
|
||||||
throw({error, {invalid_option, OptStr}})
|
throw({error, {invalid_option, OptStr}})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
-spec split_embedded_arg(string()) -> {Name :: string(), Arg :: string()} | string().
|
-spec split_assigned_arg(string()) -> {Name :: string(), Arg :: string()} | string().
|
||||||
%% @doc Split an option string that may contain and option with its argument
|
%% @doc Split an option string that may contain and option with its argument
|
||||||
%% separated by an equal ('=') character (e.g. "port=1000").
|
%% separated by an equal ('=') character (e.g. "port=1000").
|
||||||
split_embedded_arg(OptStr) ->
|
split_assigned_arg(OptStr) ->
|
||||||
split_embedded_arg(OptStr, OptStr, []).
|
split_assigned_arg(OptStr, OptStr, []).
|
||||||
|
|
||||||
split_embedded_arg(_OptStr, [$= | Tail], Acc) ->
|
split_assigned_arg(_OptStr, [$= | Tail], Acc) ->
|
||||||
{lists:reverse(Acc), Tail};
|
{lists:reverse(Acc), Tail};
|
||||||
split_embedded_arg(OptStr, [Char | Tail], Acc) ->
|
split_assigned_arg(OptStr, [Char | Tail], Acc) ->
|
||||||
split_embedded_arg(OptStr, Tail, [Char | Acc]);
|
split_assigned_arg(OptStr, Tail, [Char | Acc]);
|
||||||
split_embedded_arg(OptStr, [], _Acc) ->
|
split_assigned_arg(OptStr, [], _Acc) ->
|
||||||
OptStr.
|
OptStr.
|
||||||
|
|
||||||
|
|
||||||
-spec get_option_no_arg([option_spec()], string(), string() | char(), integer()) -> option().
|
|
||||||
%% @doc Retrieve the specification corresponding to an option that has no
|
|
||||||
%% argument and matches a string received on the command line.
|
%% A short option can have the following formats:
|
||||||
get_option_no_arg(OptSpecList, OptStr, OptName, FieldPos) ->
|
%% -a Single option 'a', no argument
|
||||||
case lists:keysearch(OptName, FieldPos, OptSpecList) of
|
%% -a foo Single option 'a', argument "foo"
|
||||||
{value, {Name, _Short, _Long, undefined, _Help}} ->
|
%% -afoo Single option 'a', argument "foo"
|
||||||
Name;
|
%% -abc Multiple options: 'a'; 'b'; 'c'
|
||||||
{value, {Name, _Short, _Long, ArgSpec, _Help}} ->
|
%% -bcafoo Multiple options: 'b'; 'c'; 'a' with argument "foo"
|
||||||
case arg_spec_type(ArgSpec) of
|
-spec parse_option_short([option_spec()], [option()], [string()], integer(), [string()], string(), string()) ->
|
||||||
%% Special case for booleans: if there is no argument we assume
|
{ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data:: any()}}.
|
||||||
%% the value is 'true'.
|
parse_option_short(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptStr, [Short | Arg]) ->
|
||||||
boolean ->
|
case lists:keysearch(Short, ?OPT_SHORT, OptSpecList) of
|
||||||
{Name, true};
|
{value, {Name, Short, _Long, undefined, _Help}} ->
|
||||||
|
parse_option_short(OptSpecList, [Name | OptAcc], ArgAcc, ArgPos, Args, OptStr, Arg);
|
||||||
|
|
||||||
|
{value, {_Name, Short, _Long, ArgSpec, _Help} = OptSpec} ->
|
||||||
|
case Arg of
|
||||||
|
[] ->
|
||||||
|
% The option argument string is empty, but the option requires
|
||||||
|
% an argument, so we look into the next string in the list.
|
||||||
|
parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, OptSpec);
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
throw({error, {missing_option_arg, Name}})
|
case is_valid_arg(ArgSpec, Arg) of
|
||||||
|
true ->
|
||||||
|
parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc], ArgAcc, ArgPos, Args);
|
||||||
|
_ ->
|
||||||
|
parse_option_short(OptSpecList, [convert_option_no_arg(OptSpec) | OptAcc], ArgAcc, ArgPos, Args, OptStr, Arg)
|
||||||
|
end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
false ->
|
false ->
|
||||||
throw({error, {invalid_option, OptStr}})
|
throw({error, {invalid_option, OptStr}})
|
||||||
|
end;
|
||||||
|
parse_option_short(OptSpecList, OptAcc, ArgAcc, ArgPos, Args, _OptStr, []) ->
|
||||||
|
parse(OptSpecList, OptAcc, ArgAcc, ArgPos, Args).
|
||||||
|
|
||||||
|
|
||||||
|
%% @doc Retrieve the argument for an option from the next string in the list of
|
||||||
|
%% command-line parameters.
|
||||||
|
parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, [Arg | Tail] = Args, {Name, _Short, _Long, ArgSpec, _Help} = OptSpec) ->
|
||||||
|
% Special case for booleans: when the next string is an option we assume
|
||||||
|
% the value is 'true'.
|
||||||
|
case (arg_spec_type(ArgSpec) =:= boolean) andalso not is_boolean_arg(Arg) of
|
||||||
|
true ->
|
||||||
|
parse(OptSpecList, [{Name, true} | OptAcc], ArgAcc, ArgPos, Args);
|
||||||
|
_ ->
|
||||||
|
parse(OptSpecList, [convert_option_arg(OptSpec, Arg) | OptAcc], ArgAcc, ArgPos, Tail)
|
||||||
|
end;
|
||||||
|
parse_option_next_arg(OptSpecList, OptAcc, ArgAcc, ArgPos, [] = Args, {Name, _Short, _Long, ArgSpec, _Help}) ->
|
||||||
|
% Special case for booleans: when the next string is missing we assume the
|
||||||
|
% value is 'true'.
|
||||||
|
case arg_spec_type(ArgSpec) of
|
||||||
|
boolean ->
|
||||||
|
parse(OptSpecList, [{Name, true} | OptAcc], ArgAcc, ArgPos, Args);
|
||||||
|
_ ->
|
||||||
|
throw({error, {missing_option_arg, Name}})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
@ -221,10 +227,10 @@ find_non_option_arg([], _Pos) ->
|
|||||||
false.
|
false.
|
||||||
|
|
||||||
|
|
||||||
-spec append_default_args([option_spec()], [option()]) -> [option()].
|
-spec append_default_options([option_spec()], [option()]) -> [option()].
|
||||||
%% @doc Appends the default values of the options that are not present.
|
%% @doc Appends the default values of the options that are not present.
|
||||||
append_default_args([{Name, _Short, _Long, {_Type, DefaultArg}, _Help} | Tail], OptAcc) ->
|
append_default_options([{Name, _Short, _Long, {_Type, DefaultArg}, _Help} | Tail], OptAcc) ->
|
||||||
append_default_args(Tail,
|
append_default_options(Tail,
|
||||||
case lists:keymember(Name, 1, OptAcc) of
|
case lists:keymember(Name, 1, OptAcc) of
|
||||||
false ->
|
false ->
|
||||||
[{Name, DefaultArg} | OptAcc];
|
[{Name, DefaultArg} | OptAcc];
|
||||||
@ -232,18 +238,29 @@ append_default_args([{Name, _Short, _Long, {_Type, DefaultArg}, _Help} | Tail],
|
|||||||
OptAcc
|
OptAcc
|
||||||
end);
|
end);
|
||||||
%% For options with no default argument.
|
%% For options with no default argument.
|
||||||
append_default_args([_Head | Tail], OptAcc) ->
|
append_default_options([_Head | Tail], OptAcc) ->
|
||||||
append_default_args(Tail, OptAcc);
|
append_default_options(Tail, OptAcc);
|
||||||
append_default_args([], OptAcc) ->
|
append_default_options([], OptAcc) ->
|
||||||
OptAcc.
|
OptAcc.
|
||||||
|
|
||||||
|
|
||||||
%% -spec is_option(string()) -> boolean().
|
-spec convert_option_no_arg(option_spec()) -> option().
|
||||||
%% is_option([Char | _Tail] = OptStr) ->
|
convert_option_no_arg({Name, _Short, _Long, ArgSpec, _Help}) ->
|
||||||
%% (Char =:= $-) orelse lists:member($=, OptStr).
|
case ArgSpec of
|
||||||
|
% Special case for booleans: if there is no argument we assume
|
||||||
|
% the value is 'true'.
|
||||||
|
{boolean, _DefaultValue} ->
|
||||||
|
{Name, true};
|
||||||
|
boolean ->
|
||||||
|
{Name, true};
|
||||||
|
{_Type, DefaultValue} ->
|
||||||
|
{Name, DefaultValue};
|
||||||
|
_ ->
|
||||||
|
throw({error, {missing_option_arg, Name}})
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
-spec convert_option_arg(option_spec(), string()) -> [option()].
|
-spec convert_option_arg(option_spec(), string()) -> option().
|
||||||
%% @doc Convert the argument passed in the command line to the data type
|
%% @doc Convert the argument passed in the command line to the data type
|
||||||
%% indicated by the argument specification.
|
%% indicated by the argument specification.
|
||||||
convert_option_arg({Name, _Short, _Long, ArgSpec, _Help}, Arg) ->
|
convert_option_arg({Name, _Short, _Long, ArgSpec, _Help}, Arg) ->
|
||||||
@ -277,6 +294,29 @@ to_type(_Type, Arg) ->
|
|||||||
Arg.
|
Arg.
|
||||||
|
|
||||||
|
|
||||||
|
% -spec is_valid_option([option_spec()], Opt :: char() | string(), FieldPos :: integer()) -> boolean().
|
||||||
|
% is_valid_option(OptSpecList, Opt, FieldPos) ->
|
||||||
|
% case lists:keysearch(Opt, FieldPos, OptSpecList) of
|
||||||
|
% {value, {_Name, _Short, _Long, _ArgSpec, _Help}} ->
|
||||||
|
% true;
|
||||||
|
% _ ->
|
||||||
|
% false
|
||||||
|
% end.
|
||||||
|
|
||||||
|
|
||||||
|
-spec is_valid_arg(arg_spec() | arg_type(), string()) -> boolean().
|
||||||
|
is_valid_arg({Type, _DefaultArg}, Arg) ->
|
||||||
|
is_valid_arg(Type, Arg);
|
||||||
|
is_valid_arg(boolean, Arg) ->
|
||||||
|
is_boolean_arg(Arg);
|
||||||
|
is_valid_arg(integer, Arg) ->
|
||||||
|
is_integer_arg(Arg);
|
||||||
|
is_valid_arg(float, Arg) ->
|
||||||
|
is_float_arg(Arg);
|
||||||
|
is_valid_arg(_Type, _Arg) ->
|
||||||
|
true.
|
||||||
|
|
||||||
|
|
||||||
-spec is_boolean_arg(string()) -> boolean().
|
-spec is_boolean_arg(string()) -> boolean().
|
||||||
is_boolean_arg(Arg) ->
|
is_boolean_arg(Arg) ->
|
||||||
LowerArg = string:to_lower(Arg),
|
LowerArg = string:to_lower(Arg),
|
||||||
@ -286,6 +326,24 @@ is_boolean_arg(Arg) ->
|
|||||||
(LowerArg =:= "1").
|
(LowerArg =:= "1").
|
||||||
|
|
||||||
|
|
||||||
|
-spec is_integer_arg(string()) -> boolean().
|
||||||
|
is_integer_arg([Head | Tail]) when Head >= $0, Head =< $9 ->
|
||||||
|
is_integer_arg(Tail);
|
||||||
|
is_integer_arg([_Head | _Tail]) ->
|
||||||
|
false;
|
||||||
|
is_integer_arg([]) ->
|
||||||
|
true.
|
||||||
|
|
||||||
|
|
||||||
|
-spec is_float_arg(string()) -> boolean().
|
||||||
|
is_float_arg([Head | Tail]) when (Head >= $0 andalso Head =< $9) orelse Head =:= $. ->
|
||||||
|
is_float_arg(Tail);
|
||||||
|
is_float_arg([_Head | _Tail]) ->
|
||||||
|
false;
|
||||||
|
is_float_arg([]) ->
|
||||||
|
true.
|
||||||
|
|
||||||
|
|
||||||
-spec usage([option_spec()], string()) -> ok.
|
-spec usage([option_spec()], string()) -> ok.
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @spec usage(OptSpecList :: option_spec_list(), ProgramName :: string()) -> ok.
|
%% @spec usage(OptSpecList :: option_spec_list(), ProgramName :: string()) -> ok.
|
||||||
@ -308,10 +366,10 @@ usage_cmd_line([{Name, Short, Long, ArgSpec, _Help} | Tail], Acc) ->
|
|||||||
case ArgSpec of
|
case ArgSpec of
|
||||||
undefined ->
|
undefined ->
|
||||||
if
|
if
|
||||||
%% For options with short form and no argument.
|
% For options with short form and no argument.
|
||||||
Short =/= undefined ->
|
Short =/= undefined ->
|
||||||
[$\s, $[, $-, Short, $]];
|
[$\s, $[, $-, Short, $]];
|
||||||
%% For options with only long form and no argument.
|
% For options with only long form and no argument.
|
||||||
Long =/= undefined ->
|
Long =/= undefined ->
|
||||||
[$\s, $[, $-, $-, Long, $]];
|
[$\s, $[, $-, $-, Long, $]];
|
||||||
true ->
|
true ->
|
||||||
@ -319,13 +377,13 @@ usage_cmd_line([{Name, Short, Long, ArgSpec, _Help} | Tail], Acc) ->
|
|||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
if
|
if
|
||||||
%% For options with short form and argument.
|
% For options with short form and argument.
|
||||||
Short =/= undefined ->
|
Short =/= undefined ->
|
||||||
[$\s, $[, $-, Short, $\s, $<, atom_to_list(Name), $>, $]];
|
[$\s, $[, $-, Short, $\s, $<, atom_to_list(Name), $>, $]];
|
||||||
%% For options with only long form and argument.
|
% For options with only long form and argument.
|
||||||
Long =/= undefined ->
|
Long =/= undefined ->
|
||||||
[$\s, $[, $-, $-, Long, $\s, $<, atom_to_list(Name), $>, $]];
|
[$\s, $[, $-, $-, Long, $\s, $<, atom_to_list(Name), $>, $]];
|
||||||
%% For options with neither short nor long form and argument.
|
% For options with neither short nor long form and argument.
|
||||||
true ->
|
true ->
|
||||||
[$\s, $<, atom_to_list(Name), $>]
|
[$\s, $<, atom_to_list(Name), $>]
|
||||||
end
|
end
|
||||||
@ -346,19 +404,19 @@ usage_options([{Name, Short, Long, _ArgSpec, _Help} = OptSpec | Tail], Acc) ->
|
|||||||
case Long of
|
case Long of
|
||||||
undefined ->
|
undefined ->
|
||||||
case Short of
|
case Short of
|
||||||
%% Neither short nor long form (non-option argument).
|
% Neither short nor long form (non-option argument).
|
||||||
undefined ->
|
undefined ->
|
||||||
[$<, atom_to_list(Name), $>];
|
[$<, atom_to_list(Name), $>];
|
||||||
%% Only short form.
|
% Only short form.
|
||||||
_ ->
|
_ ->
|
||||||
[$-, Short]
|
[$-, Short]
|
||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
case Short of
|
case Short of
|
||||||
%% Only long form.
|
% Only long form.
|
||||||
undefined ->
|
undefined ->
|
||||||
[$-, $-, Long];
|
[$-, $-, Long];
|
||||||
%% Both short and long form.
|
% Both short and long form.
|
||||||
_ ->
|
_ ->
|
||||||
[$-, Short, $,, $\s, $-, $-, Long]
|
[$-, Short, $,, $\s, $-, $-, Long]
|
||||||
end
|
end
|
||||||
@ -377,9 +435,9 @@ add_option_help({_Name, _Short, _Long, _ArgSpec, Help}, Prefix, Acc) when is_lis
|
|||||||
Tab = lists:duplicate(ceiling(TabSize / ?TAB_LENGTH), $\t),
|
Tab = lists:duplicate(ceiling(TabSize / ?TAB_LENGTH), $\t),
|
||||||
[[$\s, $\s, FlatPrefix, Tab, Help, $\n] | Acc];
|
[[$\s, $\s, FlatPrefix, Tab, Help, $\n] | Acc];
|
||||||
_ ->
|
_ ->
|
||||||
%% The indentation for the option description is 3 tabs (i.e. 24 characters)
|
% The indentation for the option description is 3 tabs (i.e. 24 characters)
|
||||||
%% IMPORTANT: Change the number of tabs below if you change the
|
% IMPORTANT: Change the number of tabs below if you change the
|
||||||
%% value of the INDENTATION macro.
|
% value of the INDENTATION macro.
|
||||||
[[$\t, $\t, $\t, Help, $\n], [$\s, $\s, FlatPrefix, $\n] | Acc]
|
[[$\t, $\t, $\t, Help, $\n], [$\s, $\s, FlatPrefix, $\n] | Acc]
|
||||||
end;
|
end;
|
||||||
add_option_help(_Opt, _Prefix, Acc) ->
|
add_option_help(_Opt, _Prefix, Acc) ->
|
||||||
@ -391,8 +449,8 @@ add_option_help(_Opt, _Prefix, Acc) ->
|
|||||||
ceiling(X) ->
|
ceiling(X) ->
|
||||||
T = erlang:trunc(X),
|
T = erlang:trunc(X),
|
||||||
case (X - T) of
|
case (X - T) of
|
||||||
%% Neg when Neg < 0 ->
|
% Neg when Neg < 0 ->
|
||||||
%% T;
|
% T;
|
||||||
Pos when Pos > 0 ->
|
Pos when Pos > 0 ->
|
||||||
T + 1;
|
T + 1;
|
||||||
_ ->
|
_ ->
|
||||||
|
@ -116,7 +116,7 @@ parse_1_test_() ->
|
|||||||
{?HELP(Short), ?_assertEqual({ok, {[short], []}}, parse([Short], [[$-, ?SHORT(Short)]]))},
|
{?HELP(Short), ?_assertEqual({ok, {[short], []}}, parse([Short], [[$-, ?SHORT(Short)]]))},
|
||||||
{?HELP(ShortArg), ?_assertEqual({ok, {[{short_arg, "value"}], []}}, parse([ShortArg], [[$-, ?SHORT(ShortArg)], "value"]))},
|
{?HELP(ShortArg), ?_assertEqual({ok, {[{short_arg, "value"}], []}}, parse([ShortArg], [[$-, ?SHORT(ShortArg)], "value"]))},
|
||||||
{?HELP(ShortDefArg), ?_assertMatch({ok, {[{short_def_arg, "default-short"}], []}}, parse([ShortDefArg], []))},
|
{?HELP(ShortDefArg), ?_assertMatch({ok, {[{short_def_arg, "default-short"}], []}}, parse([ShortDefArg], []))},
|
||||||
{?HELP(ShortInt), ?_assertEqual({ok, {[{short_int, 100}], []}}, parse([ShortInt], [[$-, ?SHORT(ShortInt)], "100"]))},
|
{?HELP(ShortInt), ?_assertEqual({ok, {[{short_int, 100}], []}}, parse([ShortInt], [[$-, ?SHORT(ShortInt), $1, $0, $0]]))},
|
||||||
{"Unsorted multiple short form options and arguments in a single string",
|
{"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"))},
|
?_assertMatch({ok, {[short, short2, short3], ["arg1", "arg2"]}}, parse([Short, Short2, Short3], "arg1 -abc arg2"))},
|
||||||
{"Short form option and arguments",
|
{"Short form option and arguments",
|
||||||
@ -127,12 +127,10 @@ parse_1_test_() ->
|
|||||||
{?HELP(Long), ?_assertMatch({ok, {[long], []}}, parse([Long], ["--long"]))},
|
{?HELP(Long), ?_assertMatch({ok, {[long], []}}, parse([Long], ["--long"]))},
|
||||||
{?HELP(LongArg), ?_assertMatch({ok, {[{long_arg, "value"}], []}}, parse([LongArg], ["--long-arg", "value"]))},
|
{?HELP(LongArg), ?_assertMatch({ok, {[{long_arg, "value"}], []}}, parse([LongArg], ["--long-arg", "value"]))},
|
||||||
{?HELP(LongArg), ?_assertMatch({ok, {[{long_arg, "value"}], []}}, parse([LongArg], ["--long-arg=value"]))},
|
{?HELP(LongArg), ?_assertMatch({ok, {[{long_arg, "value"}], []}}, parse([LongArg], ["--long-arg=value"]))},
|
||||||
{?HELP(LongArg), ?_assertMatch({ok, {[{long_arg, "value"}], []}}, parse([LongArg], ["long-arg=value"]))},
|
|
||||||
{?HELP(LongArg), ?_assertMatch({ok, {[{long_arg, "value=1"}], []}}, parse([LongArg], ["--long-arg=value=1"]))},
|
{?HELP(LongArg), ?_assertMatch({ok, {[{long_arg, "value=1"}], []}}, parse([LongArg], ["--long-arg=value=1"]))},
|
||||||
{?HELP(LongDefArg), ?_assertMatch({ok, {[{long_def_arg, "default-long"}], []}}, parse([LongDefArg], []))},
|
{?HELP(LongDefArg), ?_assertMatch({ok, {[{long_def_arg, "default-long"}], []}}, parse([LongDefArg], []))},
|
||||||
{?HELP(LongInt), ?_assertMatch({ok, {[{long_int, 100}], []}}, parse([LongInt], ["--long-int", "100"]))},
|
{?HELP(LongInt), ?_assertMatch({ok, {[{long_int, 100}], []}}, parse([LongInt], ["--long-int", "100"]))},
|
||||||
{?HELP(LongInt), ?_assertMatch({ok, {[{long_int, 100}], []}}, parse([LongInt], ["--long-int=100"]))},
|
{?HELP(LongInt), ?_assertMatch({ok, {[{long_int, 100}], []}}, parse([LongInt], ["--long-int=100"]))},
|
||||||
{?HELP(LongInt), ?_assertMatch({ok, {[{long_int, 100}], []}}, parse([LongInt], ["long-int=100"]))},
|
|
||||||
{"Long form option and arguments",
|
{"Long form option and arguments",
|
||||||
?_assertMatch({ok, {[long], ["arg1", "arg2"]}}, parse([Long], ["--long", "arg1", "arg2"]))},
|
?_assertMatch({ok, {[long], ["arg1", "arg2"]}}, parse([Long], ["--long", "arg1", "arg2"]))},
|
||||||
{"Long form option and arguments (unsorted)",
|
{"Long form option and arguments (unsorted)",
|
||||||
@ -143,12 +141,10 @@ parse_1_test_() ->
|
|||||||
{?HELP(ShortLongArg), ?_assertEqual({ok, {[{short_long_arg, "value"}], []}}, parse([ShortLongArg], [[$-, ?SHORT(ShortLongArg)], "value"]))},
|
{?HELP(ShortLongArg), ?_assertEqual({ok, {[{short_long_arg, "value"}], []}}, parse([ShortLongArg], [[$-, ?SHORT(ShortLongArg)], "value"]))},
|
||||||
{?HELP(ShortLongArg), ?_assertMatch({ok, {[{short_long_arg, "value"}], []}}, parse([ShortLongArg], ["--short-long-arg", "value"]))},
|
{?HELP(ShortLongArg), ?_assertMatch({ok, {[{short_long_arg, "value"}], []}}, parse([ShortLongArg], ["--short-long-arg", "value"]))},
|
||||||
{?HELP(ShortLongArg), ?_assertMatch({ok, {[{short_long_arg, "value"}], []}}, parse([ShortLongArg], ["--short-long-arg=value"]))},
|
{?HELP(ShortLongArg), ?_assertMatch({ok, {[{short_long_arg, "value"}], []}}, parse([ShortLongArg], ["--short-long-arg=value"]))},
|
||||||
{?HELP(ShortLongArg), ?_assertMatch({ok, {[{short_long_arg, "value"}], []}}, parse([ShortLongArg], ["short-long-arg=value"]))},
|
|
||||||
{?HELP(ShortLongDefArg), ?_assertMatch({ok, {[{short_long_def_arg, "default-short-long"}], []}}, parse([ShortLongDefArg], []))},
|
{?HELP(ShortLongDefArg), ?_assertMatch({ok, {[{short_long_def_arg, "default-short-long"}], []}}, parse([ShortLongDefArg], []))},
|
||||||
{?HELP(ShortLongInt), ?_assertEqual({ok, {[{short_long_int, 1234}], []}}, parse([ShortLongInt], [[$-, ?SHORT(ShortLongInt)], "1234"]))},
|
{?HELP(ShortLongInt), ?_assertEqual({ok, {[{short_long_int, 1234}], []}}, parse([ShortLongInt], [[$-, ?SHORT(ShortLongInt)], "1234"]))},
|
||||||
{?HELP(ShortLongInt), ?_assertMatch({ok, {[{short_long_int, 1234}], []}}, parse([ShortLongInt], ["--short-long-int", "1234"]))},
|
{?HELP(ShortLongInt), ?_assertMatch({ok, {[{short_long_int, 1234}], []}}, parse([ShortLongInt], ["--short-long-int", "1234"]))},
|
||||||
{?HELP(ShortLongInt), ?_assertMatch({ok, {[{short_long_int, 1234}], []}}, parse([ShortLongInt], ["--short-long-int=1234"]))},
|
{?HELP(ShortLongInt), ?_assertMatch({ok, {[{short_long_int, 1234}], []}}, parse([ShortLongInt], ["--short-long-int=1234"]))},
|
||||||
{?HELP(ShortLongInt), ?_assertMatch({ok, {[{short_long_int, 1234}], []}}, parse([ShortLongInt], ["short-long-int=1234"]))},
|
|
||||||
%% Non-option arguments
|
%% Non-option arguments
|
||||||
{?HELP(NonOptArg), ?_assertMatch({ok, {[{non_opt_arg, "value"}], []}}, parse([NonOptArg], ["value"]))},
|
{?HELP(NonOptArg), ?_assertMatch({ok, {[{non_opt_arg, "value"}], []}}, parse([NonOptArg], ["value"]))},
|
||||||
{?HELP(NonOptInt), ?_assertMatch({ok, {[{non_opt_int, 1234}], []}}, parse([NonOptInt], ["1234"]))},
|
{?HELP(NonOptInt), ?_assertMatch({ok, {[{non_opt_int, 1234}], []}}, parse([NonOptInt], ["1234"]))},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user