From 18e1b37e6f1e81325e8ce8167a639b521a8f4726 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 1 Jan 2010 06:22:25 -0700 Subject: [PATCH] Break out command line argument parsing to a dedicated routine --- src/rebar_core.erl | 67 ++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/src/rebar_core.erl b/src/rebar_core.erl index 9cb1b57..e250a99 100644 --- a/src/rebar_core.erl +++ b/src/rebar_core.erl @@ -47,45 +47,20 @@ 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 getopt options - OptSpecList = option_spec_list(), - case getopt:parse(OptSpecList, Args) of - {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), + %% Parse out command line arguments -- what's left is a list of commands to + %% run + Commands = parse_args(RawArgs), - %% 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 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], @@ -97,6 +72,40 @@ run2(Args) -> %% 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 %%