]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
librados-config: refactor to use boost::program_options
authorKefu Chai <kchai@redhat.com>
Sun, 17 Nov 2019 02:43:28 +0000 (10:43 +0800)
committerKefu Chai <kchai@redhat.com>
Sun, 17 Nov 2019 17:29:52 +0000 (01:29 +0800)
for two reasons:

* since this program does not parse or read ceph options, there is no
  need to link against libglobal. the advantage of removing this linkaga
  is that we can have smaller executable, and smaller debuginfo package.
* better readability and less LOC.

for better readability

Fixes: https://tracker.ceph.com/issues/42782
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/CMakeLists.txt
src/librados-config.cc

index 91680cbe5282bb23014ee48bcc47a8ff350ada00..7f53515155221c31e617f0eb3322aaab0f2d6c54 100644 (file)
@@ -480,7 +480,7 @@ add_subdirectory(mgr)
 set(librados_config_srcs
   librados-config.cc)
 add_executable(librados-config ${librados_config_srcs})
-target_link_libraries(librados-config librados global ${BLKID_LIBRARIES} ${CMAKE_DL_LIBS} ${GSSAPI_LIBRARIES})
+target_link_libraries(librados-config librados Boost::program_options)
 
 install(TARGETS librados-config DESTINATION bin)
 
index e8bb694d40429072e3d1a9c36f98d85526dde191..36bda89bb63edecf1039cd1fed691c87cda8cde6 100644 (file)
  * Foundation.  See file COPYING.
  *
  */
+#include <iostream>
 
-#include "common/config.h"
+#include <boost/program_options/cmdline.hpp>
+#include <boost/program_options/option.hpp>
+#include <boost/program_options/options_description.hpp>
+#include <boost/program_options/parsers.hpp>
+#include <boost/program_options/variables_map.hpp>
 
-#include "common/ceph_argparse.h"
-#include "global/global_init.h"
-#include "global/global_context.h"
 #include "include/rados/librados.h"
 
-void usage()
-{
-  cout << "usage: librados-config [option]\n"
-       << "where options are:\n"
-       << "  --version                    library version\n"
-       << "  --vernum                     library version code\n";
-}
-
-void usage_exit()
-{
-  usage();
-  exit(1);
-}
+namespace po = boost::program_options;
 
 int main(int argc, const char **argv) 
 {
-  vector<const char*> args;
-  argv_to_vec(argc, argv, args);
-  if (args.empty()) {
-    cerr << argv[0] << ": -h or --help for usage" << std::endl;
-    exit(1);
-  }
-  if (ceph_argparse_need_usage(args)) {
-    usage();
-    exit(0);
-  }
-
-  bool opt_version = false;
-  bool opt_vernum = false;
-
-  auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
-                        CODE_ENVIRONMENT_UTILITY,
-                        CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
-  common_init_finish(g_ceph_context);
-  for (std::vector<const char*>::iterator i = args.begin();
-       i != args.end(); ) {
-    if (strcmp(*i, "--") == 0) {
-      break;
-    }
-    else if (strcmp(*i, "--version") == 0) {
-      opt_version = true;
-      i = args.erase(i);
-    }
-    else if (strcmp(*i, "--vernum") == 0) {
-      opt_vernum = true;
-      i = args.erase(i);
-    }
-    else
-      ++i;
-  }
-
-  if (!opt_version && !opt_vernum)
-    usage_exit();
-
-  if (opt_version) {
+  po::options_description desc{"usage: librados-config [option]"};
+  desc.add_options()
+    ("help,h", "print this help message")
+    ("version", "library version")
+    ("vernum", "library version code");
+
+  po::parsed_options parsed =
+    po::command_line_parser(argc, argv).options(desc).run();
+  po::variables_map vm;
+  po::store(parsed, vm);
+  po::notify(vm);
+
+  if (vm.count("help")) {
+    std::cout << desc << std::endl;
+  } else if (vm.count("version")) {
     int maj, min, ext;
     rados_version(&maj, &min, &ext);
-    cout << maj << "." << min << "." << ext << std::endl;
-  } else if (opt_vernum) {
-    cout << hex << LIBRADOS_VERSION_CODE << dec << std::endl;
+    std::cout << maj << "." << min << "." << ext << std::endl;
+  } else if (vm.count("vernum")) {
+    std::cout << std::hex << LIBRADOS_VERSION_CODE << std::dec << std::endl;
+  } else {
+    std::cerr << argv[0] << ": -h or --help for usage" << std::endl;
+    return 1;
   }
-
-  return 0;
 }