]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph_argparse: handle double dashes consistently
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Tue, 23 Aug 2011 23:30:03 +0000 (16:30 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Tue, 23 Aug 2011 23:30:03 +0000 (16:30 -0700)
Handle double dashes in the ceph_argparse functions, so that any piece
of code doing argument parsing will correctly interpret them.

Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/common/ceph_argparse.cc
src/common/config.cc
src/test/ceph_argparse.cc

index d0570d425f217c09f77d97c29fac04379df46786..ad4b8097fe3f345d3f1a2171c00bd5b52ff65a91 100644 (file)
@@ -254,6 +254,18 @@ static void dashes_to_underscores(const char *input, char *output)
   *o++ = '\0';
 }
 
+/** Once we see a standalone double dash, '--', we stop looking for any
+ * other options and flags. */
+static bool handle_double_dash(std::vector<const char*> &args,
+       std::vector<const char*>::iterator &i)
+{
+  if (*i == "--") {
+    i = args.end();
+    return true;
+  }
+  return false;
+}
+
 bool ceph_argparse_flag(std::vector<const char*> &args,
        std::vector<const char*>::iterator &i, ...)
 {
@@ -264,6 +276,8 @@ bool ceph_argparse_flag(std::vector<const char*> &args,
   const char *a;
   va_list ap;
 
+  if (handle_double_dash(args, i))
+    return false;
   va_start(ap, i);
   while (1) {
     a = va_arg(ap, char*);
@@ -290,6 +304,8 @@ bool ceph_argparse_binary_flag(std::vector<const char*> &args,
   va_list ap;
   int strlen_a;
 
+  if (handle_double_dash(args, i))
+    return false;
   // does this argument match any of the possibilities?
   va_start(ap, oss);
   while (1) {
@@ -338,6 +354,8 @@ bool ceph_argparse_witharg(std::vector<const char*> &args,
   va_list ap;
   int strlen_a;
 
+  if (handle_double_dash(args, i))
+    return false;
   // does this argument match any of the possibilities?
   va_start(ap, ret);
   while (1) {
index e97e7fc34474b979292724bdd352090cc49a3caa..7110c0d0e3e429446ed719d41d85099a36cbcf62 100644 (file)
@@ -599,10 +599,6 @@ parse_argv(std::vector<const char*>& args)
   // observer notifications later.
   std::string val;
   for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
-    if (strcmp(*i, "--") == 0) {
-      i = args.erase(i);
-      break;
-    }
     if (ceph_argparse_flag(args, i, "--show_conf", (char*)NULL)) {
       cerr << cf << std::endl;
       _exit(0);
index cf7c953a3441617bef3fbf889a42bb524753e8ac..9f13d33e1ee09687f1a3c20105c8c7d50e632e88 100644 (file)
@@ -107,6 +107,28 @@ TEST(CephArgParse, SimpleArgParse) {
   ASSERT_EQ(found_bar, "");
 }
 
+TEST(CephArgParse, DoubleDash) {
+  const char *ARGS[] = { "./myprog", "--foo", "5", "--", "--bar", "6", NULL };
+
+  int foo = -1, bar = -1;
+  VectorContainer args(ARGS);
+  for (std::vector<const char*>::iterator i = args.arr.begin();
+       i != args.arr.end(); )
+  {
+    std::string myarg;
+    if (ceph_argparse_witharg(args.arr, i, &myarg, "--foo", (char*)NULL)) {
+      foo = atoi(myarg.c_str());
+    }
+    else if (ceph_argparse_witharg(args.arr, i, &myarg, "--bar", (char*)NULL)) {
+      bar = atoi(myarg.c_str());
+    }
+    else
+      ++i;
+  }
+  ASSERT_EQ(foo, 5);
+  ASSERT_EQ(bar, -1);
+}
+
 
 TEST(CephArgParse, WithDashesAndUnderscores) {
   const char *BAZSTUFF1[] = { "./myprog", "--goo", "--baz-stuff", "50", "--end", NULL };