From d48179748464121ce1835b18c583a8fc7b36e756 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Tue, 6 Sep 2011 12:01:54 -0700 Subject: [PATCH] rbd.cc: use new argument parsing stuff Signed-off-by: Colin McCabe --- src/rbd.cc | 76 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/src/rbd.cc b/src/rbd.cc index 6a7f991fc653b..bc745551df153 100644 --- a/src/rbd.cc +++ b/src/rbd.cc @@ -823,7 +823,6 @@ int main(int argc, const char **argv) librbd::Image image; vector args; - DEFINE_CONF_VARS(usage_exit); argv_to_vec(argc, argv, args); env_to_vec(args); @@ -837,42 +836,55 @@ int main(int argc, const char **argv) int order = 0; const char *imgname = NULL, *snapname = NULL, *destname = NULL, *dest_poolname = NULL, *path = NULL, *secretfile = NULL, *user = NULL, *devpath = NULL; bool is_snap_cmd = false; - FOR_EACH_ARG(args) { - if (CEPH_ARGPARSE_EQ("help", 'h')) { + + std::string val; + std::ostringstream err; + long long sizell = 0; + for (std::vector::iterator i = args.begin(); i != args.end(); ) { + if (ceph_argparse_double_dash(args, i)) { + break; + } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) { usage(); exit(0); - } else if (CEPH_ARGPARSE_EQ("pool", 'p')) { - CEPH_ARGPARSE_SET_ARG_VAL(&poolname, OPT_STR); - } else if (CEPH_ARGPARSE_EQ("dest-pool", '\0')) { - CEPH_ARGPARSE_SET_ARG_VAL(&dest_poolname, OPT_STR); - } else if (CEPH_ARGPARSE_EQ("snap", '\0')) { - CEPH_ARGPARSE_SET_ARG_VAL(&snapname, OPT_STR); - } else if (CEPH_ARGPARSE_EQ("image", 'i')) { - CEPH_ARGPARSE_SET_ARG_VAL(&imgname, OPT_STR); - } else if (CEPH_ARGPARSE_EQ("size", 's')) { - CEPH_ARGPARSE_SET_ARG_VAL(&size, OPT_LONGLONG); - size <<= 20; // MB -> bytes - } else if (CEPH_ARGPARSE_EQ("order", '\0')) { - CEPH_ARGPARSE_SET_ARG_VAL(&order, OPT_INT); - } else if (CEPH_ARGPARSE_EQ("path", '\0')) { - CEPH_ARGPARSE_SET_ARG_VAL(&path, OPT_STR); - } else if (CEPH_ARGPARSE_EQ("dest", '\0')) { - CEPH_ARGPARSE_SET_ARG_VAL(&destname, OPT_STR); - } else if (CEPH_ARGPARSE_EQ("secret", '\0')) { - CEPH_ARGPARSE_SET_ARG_VAL(&secretfile, OPT_STR); - } else if (CEPH_ARGPARSE_EQ("user", '\0')) { - CEPH_ARGPARSE_SET_ARG_VAL(&user, OPT_STR); + } else if (ceph_argparse_witharg(args, i, &val, "-p", "--pool", (char*)NULL)) { + poolname = strdup(val.c_str()); + } else if (ceph_argparse_witharg(args, i, &val, "--dest-pool", (char*)NULL)) { + dest_poolname = strdup(val.c_str()); + } else if (ceph_argparse_witharg(args, i, &val, "--snap", (char*)NULL)) { + snapname = strdup(val.c_str()); + } else if (ceph_argparse_witharg(args, i, &val, "-i", "--image", (char*)NULL)) { + imgname = strdup(val.c_str()); + } else if (ceph_argparse_withlonglong(args, i, &sizell, &err, "-s", "--size", (char*)NULL)) { + if (!err.str().empty()) { + cerr << err.str() << std::endl; + exit(EXIT_FAILURE); + } + size = sizell; + } else if (ceph_argparse_withint(args, i, &order, &err, "--order", (char*)NULL)) { + if (!err.str().empty()) { + cerr << err.str() << std::endl; + exit(EXIT_FAILURE); + } + } else if (ceph_argparse_witharg(args, i, &val, "--path", (char*)NULL)) { + path = strdup(val.c_str()); + } else if (ceph_argparse_witharg(args, i, &val, "--dest", (char*)NULL)) { + destname = strdup(val.c_str()); + } else if (ceph_argparse_witharg(args, i, &val, "--secret", (char*)NULL)) { + secretfile = strdup(val.c_str()); + } else if (ceph_argparse_witharg(args, i, &val, "--user", (char*)NULL)) { + user = strdup(val.c_str()); } else { + const char *v = *i; if (!opt_cmd) { - opt_cmd = get_cmd(CEPH_ARGPARSE_VAL, &is_snap_cmd); + opt_cmd = get_cmd(v, &is_snap_cmd); if (opt_cmd < 0) { - cerr << "invalid command: " << CEPH_ARGPARSE_VAL << std::endl; + cerr << "invalid command: " << v << std::endl; usage_exit(); } } else { switch (opt_cmd) { case OPT_LIST: - set_conf_param(CEPH_ARGPARSE_VAL, &poolname, NULL); + set_conf_param(v, &poolname, NULL); break; case OPT_INFO: case OPT_CREATE: @@ -884,20 +896,20 @@ int main(int argc, const char **argv) case OPT_SNAP_LIST: case OPT_WATCH: case OPT_MAP: - set_conf_param(CEPH_ARGPARSE_VAL, &imgname, NULL); + set_conf_param(v, &imgname, NULL); break; case OPT_UNMAP: - set_conf_param(CEPH_ARGPARSE_VAL, &devpath, NULL); + set_conf_param(v, &devpath, NULL); break; case OPT_EXPORT: - set_conf_param(CEPH_ARGPARSE_VAL, &imgname, &path); + set_conf_param(v, &imgname, &path); break; case OPT_IMPORT: - set_conf_param(CEPH_ARGPARSE_VAL, &path, &destname); + set_conf_param(v, &path, &destname); break; case OPT_COPY: case OPT_RENAME: - set_conf_param(CEPH_ARGPARSE_VAL, &imgname, &destname); + set_conf_param(v, &imgname, &destname); break; } }