Browse Source

Break out command line argument parsing to a dedicated routine

pull/3/head
Dave Smith 15 years ago
parent
commit
18e1b37e6f
1 changed files with 36 additions and 27 deletions
  1. +36
    -27
      src/rebar_core.erl

+ 36
- 27
src/rebar_core.erl View File

@ -47,55 +47,64 @@ run(["version"]) ->
{ok, Vsn} = application:get_key(rebar, vsn),
?CONSOLE("Version ~s built ~s\n", [Vsn, ?BUILD_TIME]),
ok;
run(Args) ->
run(RawArgs) ->
%% Pre-load the rebar app so that we get default configuration
ok = application:load(rebar),
%% Parse out command line arguments -- what's left is a list of commands to
%% run
Commands = parse_args(RawArgs),
%% Make sure crypto is running
crypto:start(),
%% Initialize logging system
rebar_log:init(),
%% Convert command strings to atoms
CommandAtoms = [list_to_atom(C) || C <- Commands],
%% Load rebar.config, if it exists
process_dir(rebar_utils:get_cwd(), rebar_config:new(), CommandAtoms).
%% ===================================================================
%% 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");
getopt:usage(OptSpecList, "rebar"),
halt(1);
{ok, {Options, NonOptArgs}} ->
case proplists:get_bool(help, Options) of
true ->
%% display help
getopt:usage(OptSpecList, "rebar");
getopt:usage(OptSpecList, "rebar"),
halt(0);
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)
%% 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")
getopt:usage(OptSpecList, "rebar"),
halt(1)
end.
run2(Args) ->
%% Make sure crypto is running
crypto:start(),
%% Initialize logging system
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
CommandAtoms = [list_to_atom(C) || C <- Commands],
%% Load rebar.config, if it exists
process_dir(rebar_utils:get_cwd(), rebar_config:new(), CommandAtoms).
%% ===================================================================
%% Internal functions
%% ===================================================================
%%
%% set global flag based on getopt option boolean value

Loading…
Cancel
Save