Rolled back getopt to restore custom variables

This commit is contained in:
Tuncer Ayaz 2010-01-01 00:08:00 +01:00
parent 48048c82a6
commit 0817dec7ca
7 changed files with 82 additions and 134 deletions

View file

@ -11,9 +11,6 @@
-module(getopt). -module(getopt).
-author('juanjo@comellas.org'). -author('juanjo@comellas.org').
-export([parse/2, usage/2]).
-define(TAB_LENGTH, 8). -define(TAB_LENGTH, 8).
%% Indentation of the help messages in number of tabs. %% Indentation of the help messages in number of tabs.
-define(INDENTATION, 3). -define(INDENTATION, 3).
@ -47,6 +44,8 @@
Help :: string() | undefined Help :: string() | undefined
}. }.
-export([parse/2, usage/2]).
-spec parse([option_spec()], string() | [string()]) -> {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}. -spec parse([option_spec()], string() | [string()]) -> {ok, {[option()], [string()]}} | {error, {Reason :: atom(), Data :: any()}}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -72,20 +71,12 @@ parse(OptSpecList, CmdLine) ->
-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 long options. %% Process long options.
parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$-, $- | Name] = OptStr | Tail]) -> parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$-, $- | LongName] = OptStr | Tail]) ->
{Option, Tail1} = {Option, Tail1} = get_option(OptSpecList, OptStr, LongName, ?OPT_LONG, Tail),
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); 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, [[$-, ShortName] = OptStr | Tail]) ->
{Option, Tail1} = get_option(OptSpecList, OptStr, ?OPT_SHORT, ShortName, Tail), {Option, Tail1} = get_option(OptSpecList, OptStr, ShortName, ?OPT_SHORT, Tail),
parse(OptSpecList, [Option | OptAcc], ArgAcc, ArgPos, Tail1); parse(OptSpecList, [Option | OptAcc], ArgAcc, ArgPos, Tail1);
%% Process multiple short options with no argument. %% Process multiple short options with no argument.
parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$- | ShortNameList] = OptStr | Tail]) -> parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$- | ShortNameList] = OptStr | Tail]) ->
@ -96,21 +87,12 @@ parse(OptSpecList, OptAcc, ArgAcc, ArgPos, [[$- | ShortNameList] = OptStr | Tail
end, OptAcc, ShortNameList), end, OptAcc, ShortNameList),
parse(OptSpecList, NewOptAcc, ArgAcc, ArgPos, Tail); 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
@ -118,75 +100,28 @@ parse(OptSpecList, OptAcc, ArgAcc, _ArgPos, []) ->
{ok, {lists:reverse(append_default_args(OptSpecList, OptAcc)), lists:reverse(ArgAcc)}}. {ok, {lists:reverse(append_default_args(OptSpecList, OptAcc)), lists:reverse(ArgAcc)}}.
-spec get_option([option_spec()], string(), integer(), string() | char(), [string()]) -> -spec get_option([option_spec()], string(), string() | char(), integer(), [string()]) ->
{option(), [string()]}. {option(), [string()]}.
%% @doc Retrieve the specification corresponding to an option matching a string %% @doc Retrieve the specification corresponding to an option matching a string
%% received on the command line. %% received on the command line.
get_option(OptSpecList, OptStr, FieldPos, OptName, Tail) -> get_option(OptSpecList, OptStr, OptName, FieldPos, Tail) ->
case lists:keysearch(OptName, FieldPos, OptSpecList) of case lists:keysearch(OptName, FieldPos, OptSpecList) of
{value, {Name, _Short, _Long, ArgSpec, _Help} = OptSpec} -> {value, {Name, _Short, _Long, ArgSpec, _Help} = OptSpec} ->
case ArgSpec of case ArgSpec of
undefined -> undefined ->
{Name, Tail}; {Name, Tail};
_ -> _ ->
ArgSpecType = arg_spec_type(ArgSpec),
case Tail of case Tail of
[Arg | Tail1] -> [Arg | Tail1] ->
case (ArgSpecType =:= boolean) andalso not is_boolean_arg(Arg) of {convert_option_arg(OptSpec, Arg), Tail1};
%% Special case for booleans: when the next string
%% is an option we assume the value is 'true'.
true ->
{{Name, true}, Tail};
_ ->
{convert_option_arg(OptSpec, Arg), Tail1}
end;
[] -> [] ->
case ArgSpecType of
boolean ->
{{Name, true}, Tail};
_ ->
throw({error, {missing_option_arg, Name}}) throw({error, {missing_option_arg, Name}})
end end
end
end; end;
false -> false ->
throw({error, {invalid_option, OptStr}}) throw({error, {invalid_option, OptStr}})
end. end.
-spec get_option_embedded_arg([option_spec()], string(), integer(), string(),
string()) -> option().
%% @doc Retrieve the specification corresponding to an option matching a string
%% 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
undefined ->
throw({error, {invalid_option_arg, OptStr}});
_ ->
convert_option_arg(OptSpec, Arg)
end;
false ->
throw({error, {invalid_option, OptStr}})
end.
-spec split_embedded_arg(string()) -> {Name :: string(), Arg :: string()} | string().
%% @doc Split an option string that may contain and option with its argument
%% separated by an equal ('=') character (e.g. "port=1000").
split_embedded_arg(OptStr) ->
split_embedded_arg(OptStr, OptStr, []).
split_embedded_arg(_OptStr, [$= | Tail], Acc) ->
{lists:reverse(Acc), Tail};
split_embedded_arg(OptStr, [Char | Tail], Acc) ->
split_embedded_arg(OptStr, Tail, [Char | Acc]);
split_embedded_arg(OptStr, [], _Acc) ->
OptStr.
-spec get_option_no_arg([option_spec()], string(), string() | char(), integer()) -> option(). -spec get_option_no_arg([option_spec()], string(), string() | char(), integer()) -> option().
%% @doc Retrieve the specification corresponding to an option that has no %% @doc Retrieve the specification corresponding to an option that has no
%% argument and matches a string received on the command line. %% argument and matches a string received on the command line.
@ -194,20 +129,12 @@ get_option_no_arg(OptSpecList, OptStr, OptName, FieldPos) ->
case lists:keysearch(OptName, FieldPos, OptSpecList) of case lists:keysearch(OptName, FieldPos, OptSpecList) of
{value, {Name, _Short, _Long, undefined, _Help}} -> {value, {Name, _Short, _Long, undefined, _Help}} ->
Name; Name;
{value, {Name, _Short, _Long, ArgSpec, _Help}} -> {value, {Name, _Short, _Long, _ArgSpec, _Help}} ->
case arg_spec_type(ArgSpec) of throw({error, {missing_option_arg, Name}});
%% Special case for booleans: if there is no argument we assume
%% the value is 'true'.
boolean ->
{Name, true};
_ ->
throw({error, {missing_option_arg, Name}})
end;
false -> false ->
throw({error, {invalid_option, OptStr}}) throw({error, {invalid_option, OptStr}})
end. end.
-spec find_non_option_arg([option_spec()], integer()) -> {value, option_spec()} | false. -spec find_non_option_arg([option_spec()], integer()) -> {value, option_spec()} | false.
%% @doc Find the option for the discrete argument in position specified in the %% @doc Find the option for the discrete argument in position specified in the
%% Pos argument. %% Pos argument.
@ -238,30 +165,23 @@ append_default_args([], OptAcc) ->
OptAcc. OptAcc.
%% -spec is_option(string()) -> boolean().
%% is_option([Char | _Tail] = OptStr) ->
%% (Char =:= $-) orelse lists:member($=, OptStr).
-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 byt the argument specification.
convert_option_arg({Name, _Short, _Long, ArgSpec, _Help}, Arg) -> convert_option_arg({Name, _Short, _Long, ArgSpec, _Help}, Arg) ->
try try
{Name, to_type(arg_spec_type(ArgSpec), Arg)} Converted = case ArgSpec of
{Type, _DefaultArg} ->
to_type(Type, Arg);
Type when is_atom(Type) ->
to_type(Type, Arg)
end,
{Name, Converted}
catch catch
error:_ -> error:_ ->
throw({error, {invalid_option_arg, {Name, Arg}}}) throw({error, {invalid_option_arg, {Name, Arg}}})
end. end.
-spec arg_spec_type(arg_spec()) -> arg_type() | undefined.
arg_spec_type({Type, _DefaultArg}) ->
Type;
arg_spec_type(Type) when is_atom(Type) ->
Type.
-spec to_type(atom(), string()) -> arg_value(). -spec to_type(atom(), string()) -> arg_value().
to_type(binary, Arg) -> to_type(binary, Arg) ->
list_to_binary(Arg); list_to_binary(Arg);
@ -272,18 +192,12 @@ to_type(integer, Arg) ->
to_type(float, Arg) -> to_type(float, Arg) ->
list_to_float(Arg); list_to_float(Arg);
to_type(boolean, Arg) -> to_type(boolean, Arg) ->
is_boolean_arg(Arg);
to_type(_Type, Arg) ->
Arg.
-spec is_boolean_arg(string()) -> boolean().
is_boolean_arg(Arg) ->
LowerArg = string:to_lower(Arg), LowerArg = string:to_lower(Arg),
(LowerArg =:= "true") orelse (LowerArg =:= "t") orelse (LowerArg =:= "true") orelse (LowerArg =:= "t") orelse
(LowerArg =:= "yes") orelse (LowerArg =:= "y") orelse (LowerArg =:= "yes") orelse (LowerArg =:= "y") orelse
(LowerArg =:= "on") orelse (LowerArg =:= "enabled") orelse (LowerArg =:= "on") orelse (LowerArg =:= "enabled");
(LowerArg =:= "1"). to_type(_Type, Arg) ->
Arg.
-spec usage([option_spec()], string()) -> ok. -spec usage([option_spec()], string()) -> ok.

View file

@ -83,7 +83,12 @@ get_global(Key, Default) ->
end. end.
is_verbose() -> is_verbose() ->
get_global(verbose, false). case get_global(verbose, "0") of
"1" ->
true;
_ ->
false
end.
%% =================================================================== %% ===================================================================

View file

@ -51,8 +51,8 @@ run(Args) ->
%% Pre-load the rebar app so that we get default configuration %% Pre-load the rebar app so that we get default configuration
ok = application:load(rebar), ok = application:load(rebar),
OptSpecList = option_spec_list(),
%% Parse getopt options %% Parse getopt options
OptSpecList = option_spec_list(),
case getopt:parse(OptSpecList, Args) of case getopt:parse(OptSpecList, Args) of
{ok, {_Options, []}} -> {ok, {_Options, []}} ->
%% no command to run specified %% no command to run specified
@ -63,7 +63,7 @@ run(Args) ->
%% display help %% display help
getopt:usage(OptSpecList, "rebar"); getopt:usage(OptSpecList, "rebar");
false -> false ->
%% set global variables based on getopt options %% Set global variables based on getopt options
set_global_flag(Options, verbose), set_global_flag(Options, verbose),
set_global_flag(Options, force), set_global_flag(Options, force),
@ -75,13 +75,17 @@ run(Args) ->
getopt:usage(OptSpecList, "rebar") getopt:usage(OptSpecList, "rebar")
end. end.
run2(Commands) -> run2(Args) ->
%% Make sure crypto is running %% Make sure crypto is running
crypto:start(), crypto:start(),
%% Initialize logging system %% Initialize logging system
rebar_log:init(), rebar_log:init(),
%% Filter all the flags (i.e. string of form key=value) from the
%% command line arguments. What's left will be the commands to run.
Commands = filter_flags(Args, []),
%% Convert command strings to atoms %% Convert command strings to atoms
CommandAtoms = [list_to_atom(C) || C <- Commands], CommandAtoms = [list_to_atom(C) || C <- Commands],
@ -97,7 +101,12 @@ run2(Commands) ->
%% set global flag based on getopt option boolean value %% set global flag based on getopt option boolean value
%% %%
set_global_flag(Options, Flag) -> set_global_flag(Options, Flag) ->
Value = proplists:get_bool(Flag, Options), Value = case proplists:get_bool(Flag, Options) of
true ->
"1";
false ->
"0"
end,
rebar_config:set_global(Flag, Value). rebar_config:set_global(Flag, Value).
%% %%
@ -107,10 +116,30 @@ 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"},
{verbose, $v, "verbose", {boolean, false}, "Be verbose about what gets done"}, {verbose, $v, "verbose", undefined, "Be verbose about what gets done"},
{force, $f, "force", {boolean, false}, "Force"} {force, $f, "force", undefined, "Force"}
]. ].
%%
%% Seperate all commands (single-words) from flags (key=value) and store
%% values into the rebar_config global storage.
%%
filter_flags([], Commands) ->
lists:reverse(Commands);
filter_flags([Item | Rest], Commands) ->
case string:tokens(Item, "=") of
[Command] ->
filter_flags(Rest, [Command | Commands]);
[KeyStr, Value] ->
Key = list_to_atom(KeyStr),
rebar_config:set_global(Key, Value),
filter_flags(Rest, Commands);
Other ->
?CONSOLE("Ignoring command line argument: ~p\n", [Other]),
filter_flags(Rest, Commands)
end.
process_dir(Dir, ParentConfig, Commands) -> process_dir(Dir, ParentConfig, Commands) ->
ok = file:set_cwd(Dir), ok = file:set_cwd(Dir),
Config = rebar_config:new(ParentConfig), Config = rebar_config:new(ParentConfig),

View file

@ -73,8 +73,8 @@ run_test_if_present(TestDir, Config, File) ->
run_test(TestDir, Config, _File) -> run_test(TestDir, Config, _File) ->
{Cmd, RawLog} = make_cmd(TestDir, Config), {Cmd, RawLog} = make_cmd(TestDir, Config),
clear_log(RawLog), clear_log(RawLog),
case rebar_config:is_verbose() of case rebar_config:get_global(verbose, "0") of
true -> "0" ->
Output = " >> " ++ RawLog ++ " 2>&1"; Output = " >> " ++ RawLog ++ " 2>&1";
_ -> _ ->
Output = " 2>&1 | tee -a " ++ RawLog Output = " 2>&1 | tee -a " ++ RawLog
@ -125,8 +125,8 @@ check_log(RawLog) ->
%% Show the log if it hasn't already been shown because verbose was on %% Show the log if it hasn't already been shown because verbose was on
show_log(RawLog) -> show_log(RawLog) ->
?CONSOLE("Showing log\n", []), ?CONSOLE("Showing log\n", []),
case rebar_config:is_verbose() of case rebar_config:get_global(verbose, "0") of
true -> "0" ->
{ok, Contents} = file:read_file(RawLog), {ok, Contents} = file:read_file(RawLog),
?CONSOLE("~s", [Contents]); ?CONSOLE("~s", [Contents]);
_ -> _ ->

View file

@ -35,8 +35,8 @@
%% =================================================================== %% ===================================================================
init() -> init() ->
case rebar_config:is_verbose() of case rebar_config:get_global(verbose, "0") of
true -> "1" ->
set_level(debug); set_level(debug);
_ -> _ ->
set_level(error) set_level(error)

View file

@ -65,11 +65,11 @@ install(Config, File) ->
true -> true ->
%% Already exists -- check for force=1 global flag and only %% Already exists -- check for force=1 global flag and only
%% continue if it's set %% continue if it's set
case rebar_config:get_global(force, false) of case rebar_config:get_global(force, "0") of
false -> "0" ->
?ERROR("~s already exists. Installation failed.\n", [AppId]), ?ERROR("~s already exists. Installation failed.\n", [AppId]),
?FAIL; ?FAIL;
true -> "1" ->
?WARN("~s already exists, but forcibly overwriting.\n", [AppId]) ?WARN("~s already exists, but forcibly overwriting.\n", [AppId])
end; end;
false -> false ->

View file

@ -179,8 +179,8 @@ mk_target_dir(TargetDir) ->
ok; ok;
{error, eexist} -> {error, eexist} ->
%% Output directory already exists; if force=1, wipe it out %% Output directory already exists; if force=1, wipe it out
case rebar_config:get_global(force, false) of case rebar_config:get_global(force, "0") of
true -> "1" ->
rebar_file_utils:rm_rf(TargetDir), rebar_file_utils:rm_rf(TargetDir),
ok = file:make_dir(TargetDir); ok = file:make_dir(TargetDir);
_ -> _ ->