]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
conf: arguments parsing cleanup
authorYehuda Sadeh <yehuda@hq.newdream.net>
Thu, 12 Mar 2009 22:09:59 +0000 (15:09 -0700)
committerYehuda Sadeh <yehuda@hq.newdream.net>
Thu, 12 Mar 2009 22:10:18 +0000 (15:10 -0700)
src/cconf.cc
src/config.cc
src/config.h

index 82beeb0aa3ffc8511d3449a831a8b55afaeadd03..d3df67622adfc95b7e001ff909df6ef2bec69397 100644 (file)
@@ -27,10 +27,12 @@ int main(int argc, const char **argv)
   const char *key = NULL, *defval = NULL;
   const char *list_sections = 0;
   char *val;
+  char *section;
   int param = 0;
   vector<const char*> args, nargs;
   deque<const char *> sections;
   unsigned i;
+  DEFINE_CONF_VARS(usage);
 
   argv_to_vec(argc, argv, args);
   env_to_vec(args);
@@ -38,12 +40,9 @@ int main(int argc, const char **argv)
   if (args.size() < 2)
     usage();
 
-  for (unsigned i=0; i<args.size(); i++) {
-    if (strcmp(args[i], "-t") == 0) {
-      if (i < args.size() - 1)
-        type = args[++i];
-      else
-       usage();
+  FOR_EACH_ARG(args) {
+    if (CONF_ARG_EQ("type", 't')) {
+       CONF_SAFE_SET_ARG_VAL(&type, OPT_STR);
     } else {
        nargs.push_back(args[i]);
     }
@@ -52,19 +51,13 @@ int main(int argc, const char **argv)
 
   common_init(args, type, false);
 
-  for (unsigned i=0; i<args.size(); i++) {
-      if (strcmp(args[i], "-l") == 0 ||
-              strcmp(args[i], "--list_sections") == 0) {
-      if (i < args.size() - 1)
-       list_sections = args[++i];
-      else
-       usage();
-    } else if (strcmp(args[i], "-s") == 0) {
-      if (i < args.size() - 1)
-        sections.push_back(args[++i]);
-      else
-       usage();
-    } else {
+  FOR_EACH_ARG(args) {
+    if (CONF_ARG_EQ("list_sections", 'l')) {
+       CONF_SAFE_SET_ARG_VAL(&list_sections, OPT_STR);
+    } else if (CONF_ARG_EQ("section", 's')) {
+       CONF_SAFE_SET_ARG_VAL(&section, OPT_STR);
+        sections.push_back(section);
+    } else if (*args[i] != '-') {
       switch (param) {
        case 0:
            key = args[i];
@@ -74,6 +67,9 @@ int main(int argc, const char **argv)
            break;
       }
       param++;
+    } else {
+      cerr << "unrecognized argument: " << args[i] << std::endl;
+      usage();
     }
   }
 
index a8efbf2e087e67c6d8cabc2bf05c530a8482f9ff..e5fc1b76b8bf5471a90cd6e3c81e492956301297 100644 (file)
@@ -686,7 +686,7 @@ static bool cmd_is_char(const char *cmd)
                cmd[1] && !cmd[2]);
 }
 
-static bool conf_cmd_equals(const char *cmd, const char *opt, char char_opt, unsigned int *val_pos)
+bool conf_cmd_equals(const char *cmd, const char *opt, char char_opt, unsigned int *val_pos)
 {
        unsigned int i;
        unsigned int len = strlen(opt);
@@ -880,33 +880,14 @@ void parse_config_file(ConfFile *cf, bool auto_update)
   }
 }
 
-
-#define CONF_NEXT_VAL (val_pos ? &args[i][val_pos] : args[++i])
-#define CONF_SET_ARG_VAL(dest, type) \
-       conf_set_conf_val(dest, type, CONF_NEXT_VAL)
-#define CONF_SAFE_SET_ARG_VAL(dest, type) \
-       do { \
-          if (__isarg || val_pos) \
-               CONF_SET_ARG_VAL(dest, type); \
-       } while (0)
-#define CONF_SET_BOOL_ARG_VAL(dest) \
-       conf_set_conf_val(dest, OPT_BOOL, (val_pos ? &args[i][val_pos] : "true"))
-#define CONF_ARG_EQ(str_cmd, char_cmd) \
-       conf_cmd_equals(args[i], str_cmd, char_cmd, &val_pos)
-
-#define DEFINE_CONF_VARS \
-unsigned int val_pos; \
-bool __isarg
-
-#define FOR_EACH_ARG(args) \
-__isarg = 1 < args.size(); \
-for (unsigned i=0; i<args.size(); i++, __isarg = i+1 < args.size()) 
-
-
+bool is_bool_param(const char *param)
+{
+       return ((strcasecmp(param, "true")==0) || (strcasecmp(param, "false")==0));
+}
 
 void parse_startup_config_options(std::vector<const char*>& args, const char *module_type)
 {
-  DEFINE_CONF_VARS;
+  DEFINE_CONF_VARS(NULL);
   std::vector<const char *> nargs;
 
   if (!g_conf.id)
@@ -1012,7 +993,7 @@ ConfFile *conf_get_conf_file()
 void parse_config_options(std::vector<const char*>& args)
 {
   int opt_len = sizeof(config_optionsp)/sizeof(config_option);
-  DEFINE_CONF_VARS;
+  DEFINE_CONF_VARS(NULL);
 
   std::vector<const char*> nargs;
   FOR_EACH_ARG(args) {
@@ -1024,7 +1005,7 @@ void parse_config_options(std::vector<const char*>& args)
       } else if (CONF_ARG_EQ(config_optionsp[optn].name,
            config_optionsp[optn].char_option)) {
         if (__isarg || val_pos || config_optionsp[optn].type == OPT_BOOL)
-           CONF_SET_ARG_VAL(config_optionsp[optn].val_ptr, config_optionsp[optn].type);
+           CONF_SAFE_SET_ARG_VAL(config_optionsp[optn].val_ptr, config_optionsp[optn].type);
         else
           continue;
       } else {
index 69ebc4b73010a1084311bd829b47fa3645e8c4c0..ffddc65637aaed38295db02c333d5201724fa615 100644 (file)
@@ -379,28 +379,43 @@ ConfFile *conf_get_conf_file();
 
 char *conf_post_process_val(const char *val);
 int conf_read_key(const char *alt_section, const char *key, opt_type_t type, void *out, void *def);
+bool conf_set_conf_val(void *field, opt_type_t type, const char *val);
+bool conf_cmd_equals(const char *cmd, const char *opt, char char_opt, unsigned int *val_pos);
+
 
 #define CONF_NEXT_VAL (val_pos ? &args[i][val_pos] : args[++i])
+
 #define CONF_SET_ARG_VAL(dest, type) \
        conf_set_conf_val(dest, type, CONF_NEXT_VAL)
+
 #define CONF_SAFE_SET_ARG_VAL(dest, type) \
        do { \
-          if (__isarg || val_pos) \
+          if (type == OPT_BOOL) { \
+               if (__isarg || val_pos) { \
+                       CONF_SET_ARG_VAL(dest, type); \
+               } else \
+                       conf_set_conf_val(dest, type, "true"); \
+          } else if (__isarg || val_pos) { \
                CONF_SET_ARG_VAL(dest, type); \
+         } else if (args_usage) \
+               args_usage(); \
        } while (0)
+
 #define CONF_SET_BOOL_ARG_VAL(dest) \
        conf_set_conf_val(dest, OPT_BOOL, (val_pos ? &args[i][val_pos] : "true"))
+
 #define CONF_ARG_EQ(str_cmd, char_cmd) \
        conf_cmd_equals(args[i], str_cmd, char_cmd, &val_pos)
 
-#define DEFINE_CONF_VARS \
-unsigned int val_pos; \
-bool __isarg
+#define DEFINE_CONF_VARS(usage_func) \
+       unsigned int val_pos; \
+       void (*args_usage)() = usage_func; \
+       bool __isarg
 
-#define FOR_EACH_ARG(args) \
-__isarg = 1 < args.size(); \
-for (unsigned i=0; i<args.size(); i++, __isarg = i+1 < args.size())
 
+#define FOR_EACH_ARG(args) \
+       __isarg = 1 < args.size(); \
+       for (unsigned i=0; i<args.size(); i++, __isarg = i+1 < args.size()) 
 
 #include "common/debug.h"