From b7406701447ae3dc910e1be21b5df3a539253f8d Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Fri, 15 Apr 2011 13:49:55 -0700 Subject: [PATCH] cconf: fix usage; clean up some code cconf: fix obsolete usage message. Add --list-all-sections flag. Use new ceph_argparse stuff. Update tests. Signed-off-by: Colin McCabe --- src/cconf.cc | 103 +++++++++++++++--------------- src/test/cli/cconf/help.t | 7 +- src/test/cli/cconf/invalid-args.t | 33 +--------- src/test/cli/cconf/option.t | 24 +++++++ src/test/cli/cconf/simple.t | 33 +--------- 5 files changed, 83 insertions(+), 117 deletions(-) diff --git a/src/cconf.cc b/src/cconf.cc index 2de7899f829bf..e8e14b64227c7 100644 --- a/src/cconf.cc +++ b/src/cconf.cc @@ -26,6 +26,9 @@ #include "common/config.h" #include "include/str_list.h" +using std::deque; +using std::string; + static void usage() { // TODO: add generic_usage once cerr/derr issues are resolved @@ -35,7 +38,8 @@ USAGE\n\ cconf \n\ \n\ ACTIONS\n\ - -l|--list-sections List sections in prefix\n\ + -L|--list-all-sections List all sections\n\ + -l|--list-sections List sections with the given prefix\n\ --lookup Print a configuration setting to stdout.\n\ Returns 0 (success) if the configuration setting is\n\ found; 1 otherwise.\n\ @@ -50,8 +54,8 @@ FLAGS\n\ If there is no action given, the action will default to --lookup.\n\ \n\ EXAMPLES\n\ -$ cconf --name client.cconf -c /etc/ceph/ceph.conf -t mon -i 0 'mon addr'\n\ -Find out if there is a 'mon addr' defined in /etc/ceph/ceph.conf\n\ +$ cconf --name mon.0 -c /etc/ceph/ceph.conf 'mon addr'\n\ +Find out what the value of 'mon add' is for monitor 0.\n\ \n\ $ cconf -l mon\n\ List sections beginning with 'mon'.\n\ @@ -62,7 +66,7 @@ Return code will be 0 on success; error code otherwise.\n\ exit(1); } -static int list_sections(const char *prefix) +static int list_sections(const std::string &prefix) { std::vector sections; int ret = g_conf.get_all_sections(sections); @@ -70,23 +74,23 @@ static int list_sections(const char *prefix) return 2; for (std::vector::const_iterator p = sections.begin(); p != sections.end(); ++p) { - if (strncmp(prefix, p->c_str(), strlen(prefix)) == 0) { + if (strncmp(prefix.c_str(), p->c_str(), prefix.size()) == 0) { cout << *p << std::endl; } } return 0; } -static int lookup(const deque §ions, - const char *key, bool resolve_search) +static int lookup(const std::deque §ions, + const std::string &key, bool resolve_search) { std::vector my_sections; - for (deque::const_iterator s = sections.begin(); s != sections.end(); ++s) { + for (deque::const_iterator s = sections.begin(); s != sections.end(); ++s) { my_sections.push_back(*s); } g_conf.get_my_sections(my_sections); std::string val; - int ret = g_conf.get_val_from_conf_file(my_sections, key, val, true); + int ret = g_conf.get_val_from_conf_file(my_sections, key.c_str(), val, true); if (ret == -ENOENT) return 1; else if (ret == 0) { @@ -108,61 +112,56 @@ static int lookup(const deque §ions, int main(int argc, const char **argv) { - char *section; - vector args, nargs; - deque sections; - DEFINE_CONF_VARS(usage); + vector args; + deque sections; bool resolve_search = false; - std::string action("lookup"); + std::string action; + std::string lookup_key; + std::string section_list_prefix; argv_to_vec(argc, argv, args); env_to_vec(args); + common_init(args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); - FOR_EACH_ARG(args) { - if (CEPH_ARGPARSE_EQ("section", 's')) { - CEPH_ARGPARSE_SET_ARG_VAL(§ion, OPT_STR); - sections.push_back(section); - } else if (CEPH_ARGPARSE_EQ("resolve-search", 'r')) { - CEPH_ARGPARSE_SET_ARG_VAL(&resolve_search, OPT_BOOL); - } else if (CEPH_ARGPARSE_EQ("help", 'h')) { + std::string val; + for (std::vector::iterator i = args.begin(); i != args.end(); ) { + if (ceph_argparse_witharg(args, i, &val, "-s", "--section", (char*)NULL)) { + sections.push_back(val); + } else if (ceph_argparse_flag(args, i, "-r", "--resolve_search", (char*)NULL)) { + resolve_search = true; + } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) { action = "help"; - } else if (CEPH_ARGPARSE_EQ("list-sections", 'l')) { - action = "list-sections"; - } else if (CEPH_ARGPARSE_EQ("lookup", '\0')) { + } else if (ceph_argparse_witharg(args, i, &val, "--lookup", (char*)NULL)) { action = "lookup"; - } - else { - nargs.push_back(args[i]); + lookup_key = val; + } else if (ceph_argparse_flag(args, i, "-L", "--list_all_sections", (char*)NULL)) { + action = "list-sections"; + section_list_prefix = ""; + } else if (ceph_argparse_witharg(args, i, &val, "-l", "--list_sections", (char*)NULL)) { + action = "list-sections"; + section_list_prefix = val; + } else { + if (((action == "lookup") || (action == "")) && (lookup_key.empty())) { + action = "lookup"; + lookup_key = *i++; + } else { + cerr << "unable to parse option: '" << *i << "'" << std::endl; + usage(); + exit(1); + } } } - common_init(nargs, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); - if (action == "help") { usage(); exit(0); - } - else if (action == "list-sections") { - if (nargs.size() != 1) - usage(); - return list_sections(nargs[0]); - } - else if (action == "lookup") { - if (nargs.size() != 1) { - cerr << "lookup: expected exactly one argument" << std::endl; - usage(); - } - return lookup(sections, nargs[0], resolve_search); - } - else if ((nargs.size() >= 1) && (nargs[0][0] == '-')) { - cerr << "Parse error at argument: " << nargs[0] << std::endl; - usage(); - } - else { - if (nargs.size() != 1) { - cerr << "lookup: expected exactly one argument" << std::endl; - usage(); - } - return lookup(sections, nargs[0], resolve_search); + } else if (action == "list-sections") { + return list_sections(section_list_prefix); + } else if (action == "lookup") { + return lookup(sections, lookup_key, resolve_search); + } else { + cerr << "You must give an action, such as --lookup or --list-all-sections." << std::endl; + cerr << "Pass --help for more help." << std::endl; + exit(1); } } diff --git a/src/test/cli/cconf/help.t b/src/test/cli/cconf/help.t index 3968607655937..75aacad3b37ab 100644 --- a/src/test/cli/cconf/help.t +++ b/src/test/cli/cconf/help.t @@ -5,7 +5,8 @@ cconf ACTIONS - -l|--list-sections List sections in prefix + -L|--list-all-sections List all sections + -l|--list-sections List sections with the given prefix --lookup Print a configuration setting to stdout. Returns 0 (success) if the configuration setting is found; 1 otherwise. @@ -20,8 +21,8 @@ If there is no action given, the action will default to --lookup. EXAMPLES - [$] cconf --name client.cconf -c /etc/ceph/ceph.conf -t mon -i 0 'mon addr' (re) - Find out if there is a 'mon addr' defined in /etc/ceph/ceph.conf + [$] cconf --name mon.0 -c /etc/ceph/ceph.conf 'mon addr' (re) + Find out what the value of 'mon add' is for monitor 0. [$] cconf -l mon (re) List sections beginning with 'mon'. diff --git a/src/test/cli/cconf/invalid-args.t b/src/test/cli/cconf/invalid-args.t index d1247fc9f48c5..0130dca0d2717 100644 --- a/src/test/cli/cconf/invalid-args.t +++ b/src/test/cli/cconf/invalid-args.t @@ -11,36 +11,7 @@ You must pass a string of the form TYPE.ID to the --name option. Valid types are: auth, mon, osd, mds, client [1] -# TODO output an error (missing key), not the whole usage $ cconf -c test.conf -s bar - lookup: expected exactly one argument - Ceph configuration query tool - - USAGE - cconf - - ACTIONS - -l|--list-sections List sections in prefix - --lookup Print a configuration setting to stdout. - Returns 0 (success) if the configuration setting is - found; 1 otherwise. - -r|--resolve-search search for the first file that exists and - can be opened in the resulted comma - delimited search list. - - FLAGS - --name name Set type.id - [-s
] Add to list of sections to search - - If there is no action given, the action will default to --lookup. - - EXAMPLES - [$] cconf --name client.cconf -c /etc/ceph/ceph.conf -t mon -i 0 'mon addr' (re) - Find out if there is a 'mon addr' defined in /etc/ceph/ceph.conf - - [$] cconf -l mon (re) - List sections beginning with 'mon'. - - RETURN CODE - Return code will be 0 on success; error code otherwise. + You must give an action, such as --lookup or --list-all-sections. + Pass --help for more help. [1] diff --git a/src/test/cli/cconf/option.t b/src/test/cli/cconf/option.t index 23ac93bc4f9bd..d1d63f2367cd1 100644 --- a/src/test/cli/cconf/option.t +++ b/src/test/cli/cconf/option.t @@ -18,6 +18,30 @@ $ cconf --conf=test.conf bar -s foo blue + $ cconf --conf=test.conf -L + bar + baz + foo + global + nobar + thud + + $ cconf --conf=test.conf --list-all-sections + bar + baz + foo + global + nobar + thud + + $ cconf --conf=test.conf --list_all_sections + bar + baz + foo + global + nobar + thud + # TODO man page stops in the middle of a sentence $ cconf -c test.conf bar -s xyzzy diff --git a/src/test/cli/cconf/simple.t b/src/test/cli/cconf/simple.t index 591129a852cc5..556e2fac16a44 100644 --- a/src/test/cli/cconf/simple.t +++ b/src/test/cli/cconf/simple.t @@ -1,33 +1,4 @@ -#TODO just show an error, not the whole usage $ cconf - lookup: expected exactly one argument - Ceph configuration query tool - - USAGE - cconf - - ACTIONS - -l|--list-sections List sections in prefix - --lookup Print a configuration setting to stdout. - Returns 0 (success) if the configuration setting is - found; 1 otherwise. - -r|--resolve-search search for the first file that exists and - can be opened in the resulted comma - delimited search list. - - FLAGS - --name name Set type.id - [-s
] Add to list of sections to search - - If there is no action given, the action will default to --lookup. - - EXAMPLES - [$] cconf --name client.cconf -c /etc/ceph/ceph.conf -t mon -i 0 'mon addr' (re) - Find out if there is a 'mon addr' defined in /etc/ceph/ceph.conf - - [$] cconf -l mon (re) - List sections beginning with 'mon'. - - RETURN CODE - Return code will be 0 on success; error code otherwise. + You must give an action, such as --lookup or --list-all-sections. + Pass --help for more help. [1] -- 2.47.3