diff options
| author | Francesco Mazzoli <francesco@rabbitmq.com> | 2012-05-21 15:47:48 +0100 |
|---|---|---|
| committer | Francesco Mazzoli <francesco@rabbitmq.com> | 2012-05-21 15:47:48 +0100 |
| commit | 854c255187a096656a883a90c6c29f05d1a77f5a (patch) | |
| tree | cfba46a30ebb534ae18fbe097a60e43af777aafd | |
| parent | 4edc93aa0e0bca3bfe41fcd43e396bbce4f36b0c (diff) | |
| download | rabbitmq-server-git-854c255187a096656a883a90c6c29f05d1a77f5a.tar.gz | |
get_options => parse_arguments
| -rw-r--r-- | src/rabbit_control.erl | 12 | ||||
| -rw-r--r-- | src/rabbit_misc.erl | 6 | ||||
| -rw-r--r-- | src/rabbit_plugins.erl | 14 | ||||
| -rw-r--r-- | src/rabbit_tests.erl | 83 |
4 files changed, 59 insertions, 56 deletions
diff --git a/src/rabbit_control.erl b/src/rabbit_control.erl index 3352027c4e..83786cfa92 100644 --- a/src/rabbit_control.erl +++ b/src/rabbit_control.erl @@ -109,12 +109,12 @@ start() -> {ok, [[NodeStr|_]|_]} = init:get_argument(nodename), {Command, Opts, Args} = - case rabbit_misc:get_options(?COMMANDS, - ?GLOBAL_OPTS, - [{?QUIET_OPT, flag}, - {?NODE_OPT, {option, NodeStr}}, - {?VHOST_OPT, {option, "/"}}], - init:get_plain_arguments()) + case rabbit_misc:parse_arguments(?COMMANDS, + ?GLOBAL_OPTS, + [{?QUIET_OPT, flag}, + {?NODE_OPT, {option, NodeStr}}, + {?VHOST_OPT, {option, "/"}}], + init:get_plain_arguments()) of {ok, Res} -> Res; no_command -> print_error("could not recognise command", []), diff --git a/src/rabbit_misc.erl b/src/rabbit_misc.erl index 9619cb0ad2..5c2928fb41 100644 --- a/src/rabbit_misc.erl +++ b/src/rabbit_misc.erl @@ -49,7 +49,7 @@ -export([version_compare/2, version_compare/3]). -export([dict_cons/3, orddict_cons/3, gb_trees_cons/3]). -export([gb_trees_fold/3, gb_trees_foreach/2]). --export([get_options/4]). +-export([parse_arguments/4]). -export([all_module_attributes/1, build_acyclic_graph/3]). -export([now_ms/0]). -export([const_ok/0, const/1]). @@ -182,7 +182,7 @@ -spec(gb_trees_fold/3 :: (fun ((any(), any(), A) -> A), A, gb_tree()) -> A). -spec(gb_trees_foreach/2 :: (fun ((any(), any()) -> any()), gb_tree()) -> 'ok'). --spec(get_options/4 :: +-spec(parse_arguments/4 :: ([{atom(), string()} | atom()], [string()], [{string(), optdef()}], @@ -753,7 +753,7 @@ gb_trees_foreach(Fun, Tree) -> %% Returns either {ok, {atom(), [{string(), string()}], [string()]} which are %% respectively the command, the key-value pairs of the options and the leftover %% arguments; or no_command if no command could be parsed. -get_options(CommandsOpts0, GlobalOpts, Defs, As0) -> +parse_arguments(CommandsOpts0, GlobalOpts, Defs, As0) -> CommandsOpts = lists:map(fun ({C, Fs}) -> {atom_to_list(C), Fs}; (C) -> {atom_to_list(C), []} end, CommandsOpts0), diff --git a/src/rabbit_plugins.erl b/src/rabbit_plugins.erl index c05cee723b..13c68429bf 100644 --- a/src/rabbit_plugins.erl +++ b/src/rabbit_plugins.erl @@ -53,13 +53,13 @@ start() -> init:get_argument(enabled_plugins_file), {ok, [[PluginsDir|_]|_]} = init:get_argument(plugins_dist_dir), {Command, Opts, Args} = - case rabbit_misc:get_options(?COMMANDS, - ?GLOBAL_OPTS, - [{?VERBOSE_OPT, flag}, - {?MINIMAL_OPT, flag}, - {?ENABLED_OPT, flag}, - {?ENABLED_ALL_OPT, flag}], - init:get_plain_arguments()) + case rabbit_misc:parse_arguments(?COMMANDS, + ?GLOBAL_OPTS, + [{?VERBOSE_OPT, flag}, + {?MINIMAL_OPT, flag}, + {?ENABLED_OPT, flag}, + {?ENABLED_ALL_OPT, flag}], + init:get_plain_arguments()) of {ok, Res} -> Res; no_command -> print_error("could not recognise command", []), diff --git a/src/rabbit_tests.erl b/src/rabbit_tests.erl index 24d55e537b..7383d6bf30 100644 --- a/src/rabbit_tests.erl +++ b/src/rabbit_tests.erl @@ -50,7 +50,7 @@ all_tests() -> passed = test_app_management(), passed = test_log_management_during_startup(), passed = test_statistics(), - passed = test_option_parser(), + passed = test_arguments_parser(), passed = test_cluster_management(), passed = test_user_management(), passed = test_runtime_parameters(), @@ -801,7 +801,7 @@ test_log_management_during_startup() -> ok = control_action(start_app, []), passed. -test_option_parser() -> +test_arguments_parser() -> Defs1 = [{"-o1", {option, "foo"}}, {"-o2", {option, "bar"}}, {"-f1", flag}, @@ -811,48 +811,52 @@ test_option_parser() -> GetOptions = fun (Args) -> - rabbit_misc:get_options(Commands1, GlobalOpts1, Defs1, Args) + rabbit_misc:parse_arguments(Commands1, GlobalOpts1, Defs1, Args) end, - check_get_options(no_command, GetOptions, []), - check_get_options(no_command, GetOptions, ["foo", "bar"]), - check_get_options({ok, {command1, [{"-f1", false}, {"-o1", "foo"}], []}}, - GetOptions, ["command1"]), - check_get_options({ok, {command1, [{"-f1", false}, {"-o1", "blah"}], []}}, - GetOptions, ["command1", "-o1", "blah"]), - check_get_options({ok, {command1, [{"-f1", true}, {"-o1", "foo"}], []}}, - GetOptions, ["command1", "-f1"]), - check_get_options({ok, {command1, [{"-f1", false}, {"-o1", "blah"}], []}}, - GetOptions, ["-o1", "blah", "command1"]), - check_get_options( + check_parse_arguments(no_command, GetOptions, []), + check_parse_arguments(no_command, GetOptions, ["foo", "bar"]), + check_parse_arguments( + {ok, {command1, [{"-f1", false}, {"-o1", "foo"}], []}}, + GetOptions, ["command1"]), + check_parse_arguments( + {ok, {command1, [{"-f1", false}, {"-o1", "blah"}], []}}, + GetOptions, ["command1", "-o1", "blah"]), + check_parse_arguments( + {ok, {command1, [{"-f1", true}, {"-o1", "foo"}], []}}, + GetOptions, ["command1", "-f1"]), + check_parse_arguments( + {ok, {command1, [{"-f1", false}, {"-o1", "blah"}], []}}, + GetOptions, ["-o1", "blah", "command1"]), + check_parse_arguments( {ok, {command1, [{"-f1", false}, {"-o1", "blah"}], ["quux"]}}, GetOptions, ["-o1", "blah", "command1", "quux"]), - check_get_options( + check_parse_arguments( {ok, {command1, [{"-f1", true}, {"-o1", "blah"}], ["quux", "baz"]}}, GetOptions, ["command1", "quux", "-f1", "-o1", "blah", "baz"]), %% For duplicate flags, the last one counts - check_get_options( + check_parse_arguments( {ok, {command1, [{"-f1", false}, {"-o1", "second"}], []}}, GetOptions, ["-o1", "first", "command1", "-o1", "second"]), %% If the flag "eats" the command, the command won't be recognised - check_get_options(no_command, GetOptions, + check_parse_arguments(no_command, GetOptions, ["-o1", "command1", "quux"]), %% If the flag doesn't have an argument, it won't be recognised - check_get_options( + check_parse_arguments( {ok, {command1, [{"-f1", false}, {"-o1", "foo"}], ["-o1"]}}, GetOptions, ["command1", "-o1"]), %% If a flag eats another flag, the eaten flag won't be recognised - check_get_options( + check_parse_arguments( {ok, {command1, [{"-f1", false}, {"-o1", "-f1"}], []}}, GetOptions, ["command1", "-o1", "-f1"]), %% Now for some command-specific flags... - check_get_options( + check_parse_arguments( {ok, {command2, [{"-f1", false}, {"-f2", false}, {"-o1", "foo"}, {"-o2", "bar"}], []}}, GetOptions, ["command2"]), - check_get_options( + check_parse_arguments( {ok, {command2, [{"-f1", false}, {"-f2", true}, {"-o1", "baz"}, {"-o2", "bar"}], ["quux", "foo"]}}, GetOptions, ["-f2", "command2", "quux", "-o1", "baz", "foo"]), @@ -861,25 +865,24 @@ test_option_parser() -> Defs2 = [{"-o1", {option, "foo"}}, {"-f1", flag}], Commands2 = [command1, {command2, ["-f1", "-bogus"]}], - CheckError = - fun (Fun) -> - case catch Fun() of - ok -> - exit({got_success_but_expected_failure, - get_options_undefined_option}); - {error, undefined_option} -> - ok - end - end, + CheckError = fun (Fun) -> + case catch Fun() of + ok -> + exit({got_success_but_expected_failure, + parse_arguments_undefined_option}); + {error, undefined_option} -> + ok + end + end, - CheckError( - fun () -> - rabbit_misc:get_options(Commands2, ["-quux"], Defs2, ["command1"]) - end), - CheckError( - fun () -> - rabbit_misc:get_options(Commands2, ["-o1"], Defs2, ["command2"]) - end), + CheckError(fun () -> + rabbit_misc:parse_arguments( + Commands2, ["-quux"], Defs2, ["command1"]) + end), + CheckError(fun () -> + rabbit_misc:parse_arguments( + Commands2, ["-o1"], Defs2, ["command2"]) + end), passed. @@ -1663,7 +1666,7 @@ expand_options(As, Bs) -> end end, Bs, As). -check_get_options(ExpRes, Fun, As) -> +check_parse_arguments(ExpRes, Fun, As) -> SortRes = fun (no_command) -> no_command; ({ok, {C, KVs, As}}) -> {ok, {C, lists:sort(KVs), lists:sort(As)}} |
