From d9f5409e8bbc2116dc31af90a30f2c7ed8fc5b41 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 14 Oct 2014 14:27:37 +0200 Subject: [PATCH] CLI framework: Change unrecognized into positional options refs #7371 --- icinga-app/icinga.cpp | 18 +++++++++++++----- lib/base/clicommand.cpp | 13 +++---------- lib/base/clicommand.hpp | 5 +++-- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index a7fcafe9a..89c0df7f6 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -137,15 +137,18 @@ int Main(void) po::options_description hiddenDesc("Hidden options"); hiddenDesc.add_options() - ("no-stack-rlimit", "used internally, do not specify manually"); + ("no-stack-rlimit", "used internally, do not specify manually") + ("arg", po::value >(), "positional argument"); + + po::positional_options_description positionalDesc; + positionalDesc.add("arg", -1); String cmdname; CLICommand::Ptr command; po::variables_map vm; - std::vector ap; try { - CLICommand::ParseCommand(argc, argv, visibleDesc, hiddenDesc, vm, ap, cmdname, command, autocomplete); + CLICommand::ParseCommand(argc, argv, visibleDesc, hiddenDesc, positionalDesc, vm, cmdname, command, autocomplete); } catch (const std::exception& ex) { std::ostringstream msgbuf; msgbuf << "Error while parsing command-line options: " << ex.what(); @@ -348,8 +351,13 @@ int Main(void) if (autocomplete) { CLICommand::ShowCommands(argc, argv, &visibleDesc, &hiddenDesc, true, autoindex); rc = 0; - } else if (command) - rc = command->Run(vm, ap); + } else if (command) { + std::vector args; + if (vm.count("arg")) + args = vm["arg"].as >(); + + rc = command->Run(vm, args); + } #ifndef _DEBUG Application::Exit(rc); diff --git a/lib/base/clicommand.cpp b/lib/base/clicommand.cpp index b4dec7129..41d373f25 100644 --- a/lib/base/clicommand.cpp +++ b/lib/base/clicommand.cpp @@ -64,9 +64,8 @@ RegisterCLICommandHelper::RegisterCLICommandHelper(const String& name, const CLI } bool CLICommand::ParseCommand(int argc, char **argv, po::options_description& visibleDesc, - po::options_description& hiddenDesc, po::variables_map& vm, - std::vector& ap, String& cmdname, - CLICommand::Ptr& command, bool autocomplete) + po::options_description& hiddenDesc, po::positional_options_description& positionalDesc, po::variables_map& vm, + String& cmdname, CLICommand::Ptr& command, bool autocomplete) { boost::mutex::scoped_lock lock(l_RegistryMutex); @@ -116,13 +115,7 @@ found_command: adesc.add(visibleDesc); adesc.add(hiddenDesc); - po::parsed_options parsed = po::command_line_parser(argc - arg_end, argv + arg_end). - options(adesc).allow_unregistered().run(); - - ap = collect_unrecognized(parsed.options, - po::include_positional); - - po::store(parsed, vm); + po::store(po::command_line_parser(argc - arg_end, argv + arg_end).options(adesc).positional(positionalDesc).run(), vm); po::notify(vm); return true; diff --git a/lib/base/clicommand.hpp b/lib/base/clicommand.hpp index 1ba306065..931160701 100644 --- a/lib/base/clicommand.hpp +++ b/lib/base/clicommand.hpp @@ -49,8 +49,9 @@ public: static void Unregister(const std::vector& name); static bool ParseCommand(int argc, char **argv, boost::program_options::options_description& visibleDesc, - boost::program_options::options_description& hiddenDesc, - boost::program_options::variables_map& vm, std::vector& ap, String& cmdname, + boost::program_options::options_description& hiddenDesc, + boost::program_options::positional_options_description& positionalDesc, + boost::program_options::variables_map& vm, String& cmdname, CLICommand::Ptr& command, bool autocomplete); static void ShowCommands(int argc, char **argv, boost::program_options::options_description *visibleDesc = NULL, boost::program_options::options_description *hiddenDesc = NULL, bool autocomplete = false, int autoindex = -1);