mirror of
https://github.com/correl/rebar.git
synced 2024-11-23 19:19:54 +00:00
Break out command line argument parsing to a dedicated routine
This commit is contained in:
parent
48c2c169ab
commit
18e1b37e6f
1 changed files with 38 additions and 29 deletions
|
@ -47,45 +47,20 @@ run(["version"]) ->
|
||||||
{ok, Vsn} = application:get_key(rebar, vsn),
|
{ok, Vsn} = application:get_key(rebar, vsn),
|
||||||
?CONSOLE("Version ~s built ~s\n", [Vsn, ?BUILD_TIME]),
|
?CONSOLE("Version ~s built ~s\n", [Vsn, ?BUILD_TIME]),
|
||||||
ok;
|
ok;
|
||||||
run(Args) ->
|
run(RawArgs) ->
|
||||||
%% 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),
|
||||||
|
|
||||||
%% Parse getopt options
|
%% Parse out command line arguments -- what's left is a list of commands to
|
||||||
OptSpecList = option_spec_list(),
|
%% run
|
||||||
case getopt:parse(OptSpecList, Args) of
|
Commands = parse_args(RawArgs),
|
||||||
{ok, {_Options, []}} ->
|
|
||||||
%% no command to run specified
|
|
||||||
getopt:usage(OptSpecList, "rebar");
|
|
||||||
{ok, {Options, NonOptArgs}} ->
|
|
||||||
case proplists:get_bool(help, Options) of
|
|
||||||
true ->
|
|
||||||
%% display help
|
|
||||||
getopt:usage(OptSpecList, "rebar");
|
|
||||||
false ->
|
|
||||||
%% Set global variables based on getopt options
|
|
||||||
set_global_flag(Options, verbose),
|
|
||||||
set_global_flag(Options, force),
|
|
||||||
|
|
||||||
%% run rebar with supplied options
|
|
||||||
run2(NonOptArgs)
|
|
||||||
end;
|
|
||||||
{error, {Reason, Data}} ->
|
|
||||||
?ERROR("Error: ~s ~p~n~n", [Reason, Data]),
|
|
||||||
getopt:usage(OptSpecList, "rebar")
|
|
||||||
end.
|
|
||||||
|
|
||||||
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,6 +72,40 @@ run2(Args) ->
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%% ===================================================================
|
%% ===================================================================
|
||||||
|
|
||||||
|
%%
|
||||||
|
%% Parse command line arguments using getopt and also filtering out any
|
||||||
|
%% key=value pairs. What's left is the list of commands to run
|
||||||
|
%%
|
||||||
|
parse_args(Args) ->
|
||||||
|
%% Parse getopt options
|
||||||
|
OptSpecList = option_spec_list(),
|
||||||
|
case getopt:parse(OptSpecList, Args) of
|
||||||
|
{ok, {_Options, []}} ->
|
||||||
|
%% no command to run specified
|
||||||
|
getopt:usage(OptSpecList, "rebar"),
|
||||||
|
halt(1);
|
||||||
|
{ok, {Options, NonOptArgs}} ->
|
||||||
|
case proplists:get_bool(help, Options) of
|
||||||
|
true ->
|
||||||
|
%% display help
|
||||||
|
getopt:usage(OptSpecList, "rebar"),
|
||||||
|
halt(0);
|
||||||
|
false ->
|
||||||
|
%% Set global variables based on getopt options
|
||||||
|
set_global_flag(Options, verbose),
|
||||||
|
set_global_flag(Options, force),
|
||||||
|
|
||||||
|
%% Filter all the flags (i.e. strings of form key=value) from the
|
||||||
|
%% command line arguments. What's left will be the commands to run.
|
||||||
|
filter_flags(NonOptArgs, [])
|
||||||
|
end;
|
||||||
|
{error, {Reason, Data}} ->
|
||||||
|
?ERROR("Error: ~s ~p~n~n", [Reason, Data]),
|
||||||
|
getopt:usage(OptSpecList, "rebar"),
|
||||||
|
halt(1)
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
%%
|
%%
|
||||||
%% set global flag based on getopt option boolean value
|
%% set global flag based on getopt option boolean value
|
||||||
%%
|
%%
|
||||||
|
|
Loading…
Reference in a new issue