New commands and opts (-V/--version/version/help)

This commit is contained in:
Tuncer Ayaz 2010-05-03 11:38:27 +02:00
parent c319662849
commit 914e5fbf68
2 changed files with 38 additions and 18 deletions

View file

@ -6,11 +6,12 @@ _rebar()
COMPREPLY=() COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
sopts="-h -c -v -f -j" sopts="-h -c -v -V -f -j"
lopts=" --help --commands --verbose --force --jobs=" lopts=" --help --commands --verbose --force --jobs= --version"
cmdsnvars="analyze build_plt check_plt check-deps clean compile \ cmdsnvars="analyze build_plt check_plt check-deps clean compile \
create create-app create-node delete-deps eunit \ create create-app create-node delete-deps eunit \
get-deps generate install int_test perf_test test xref \ get-deps generate help install int_test perf_test test \
version xref \
case= force=1 jobs= suite= verbose=1 appid= target= template=" case= force=1 jobs= suite= verbose=1 appid= target= template="
if [[ ${cur} == --* ]] ; then if [[ ${cur} == --* ]] ; then

View file

@ -41,11 +41,13 @@
%% Public API %% Public API
%% =================================================================== %% ===================================================================
run(["help"]) ->
help(),
ok;
run(["version"]) -> run(["version"]) ->
%% Load application spec and display vsn and build time info %% Load application spec and display vsn and build time info
ok = application:load(rebar), ok = application:load(rebar),
{ok, Vsn} = application:get_key(rebar, vsn), version(),
?CONSOLE("Version ~s built ~s\n", [Vsn, ?BUILD_TIME]),
ok; ok;
run(RawArgs) -> run(RawArgs) ->
%% Pre-load the rebar app so that we get default configuration %% Pre-load the rebar app so that we get default configuration
@ -87,7 +89,7 @@ parse_args(Args) ->
case getopt:parse(OptSpecList, Args) of case getopt:parse(OptSpecList, Args) of
{ok, {Options, NonOptArgs}} -> {ok, {Options, NonOptArgs}} ->
%% Check options and maybe halt execution %% Check options and maybe halt execution
{ok, continue} = print_help_maybe_halt(Options, NonOptArgs), {ok, continue} = show_info_maybe_halt(Options, NonOptArgs),
%% Set global variables based on getopt options %% Set global variables based on getopt options
set_global_flag(Options, verbose), set_global_flag(Options, verbose),
@ -123,26 +125,32 @@ set_global_flag(Options, Flag) ->
rebar_config:set_global(Flag, Value). rebar_config:set_global(Flag, Value).
%% %%
%% print help and maybe halt execution %% show info and maybe halt execution
%% %%
print_help_maybe_halt(Options, NonOptArgs) -> show_info_maybe_halt(Opts, NonOptArgs) ->
case proplists:get_bool(help, Options) of case proplists:get_bool(help, Opts) of
true -> true ->
help(), help(),
halt(0); halt(0);
false -> false ->
case proplists:get_bool(commands, Options) of case proplists:get_bool(commands, Opts) of
true -> true ->
commands(), commands(),
halt(0); halt(0);
false -> false ->
case NonOptArgs of case proplists:get_bool(version, Opts) of
[] -> true ->
io:format("No command to run specified!~n"), version(),
help(), halt(0);
halt(1); false ->
_ -> case NonOptArgs of
{ok, continue} [] ->
?CONSOLE("No command to run specified!~n",[]),
help(),
halt(1);
_ ->
{ok, continue}
end
end end
end end
end. end.
@ -187,11 +195,21 @@ perf_test [suite=] [case=] Run ct suites in ./perf_test
test [suite=] [case=] Run ct suites in ./test test [suite=] [case=] Run ct suites in ./test
xref Run cross reference analysis xref Run cross reference analysis
help Show the program options
version Show version information
">>, ">>,
io:put_chars(S), io:put_chars(S),
%% workaround to delay exit until all output is written %% workaround to delay exit until all output is written
timer:sleep(300). timer:sleep(300).
%%
%% show version information and halt
%%
version() ->
{ok, Vsn} = application:get_key(rebar, vsn),
?CONSOLE("Version ~s built ~s\n", [Vsn, ?BUILD_TIME]).
%% %%
%% options accepted via getopt %% options accepted via getopt
%% %%
@ -205,6 +223,7 @@ option_spec_list() ->
{help, $h, "help", undefined, "Show the program options"}, {help, $h, "help", undefined, "Show the program options"},
{commands, $c, "commands", undefined, "Show available commands"}, {commands, $c, "commands", undefined, "Show available commands"},
{verbose, $v, "verbose", undefined, "Be verbose about what gets done"}, {verbose, $v, "verbose", undefined, "Be verbose about what gets done"},
{version, $V, "version", undefined, "Show version information"},
{force, $f, "force", undefined, "Force"}, {force, $f, "force", undefined, "Force"},
{jobs, $j, "jobs", integer, JobsHelp} {jobs, $j, "jobs", integer, JobsHelp}
]. ].
@ -312,7 +331,7 @@ apply_commands([], _Modules, _Config, _ModuleFile) ->
apply_commands([Command | Rest], Modules, Config, ModuleFile) -> apply_commands([Command | Rest], Modules, Config, ModuleFile) ->
case select_modules(Modules, Command, []) of case select_modules(Modules, Command, []) of
[] -> [] ->
?CONSOLE("WARNING: ~p command does not apply to directory ~s\n", ?CONSOLE("WARNING: '~p' command does not apply to directory ~s\n",
[Command, rebar_utils:get_cwd()]), [Command, rebar_utils:get_cwd()]),
apply_commands(Rest, Modules, Config, ModuleFile); apply_commands(Rest, Modules, Config, ModuleFile);
TargetModules -> TargetModules ->