From 18e1b37e6f1e81325e8ce8167a639b521a8f4726 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 1 Jan 2010 06:22:25 -0700 Subject: Break out command line argument parsing to a dedicated routine --- src/rebar_core.erl | 63 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 27 deletions(-) (limited to 'src/rebar_core.erl') 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,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 -- cgit v1.1