CLI framework: Change unrecognized into positional options

refs #7371
This commit is contained in:
Michael Friedrich 2014-10-14 14:27:37 +02:00
parent 92454ca520
commit d9f5409e8b
3 changed files with 19 additions and 17 deletions

View File

@ -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<std::vector<std::string> >(), "positional argument");
po::positional_options_description positionalDesc;
positionalDesc.add("arg", -1);
String cmdname;
CLICommand::Ptr command;
po::variables_map vm;
std::vector<std::string> 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<std::string> args;
if (vm.count("arg"))
args = vm["arg"].as<std::vector<std::string> >();
rc = command->Run(vm, args);
}
#ifndef _DEBUG
Application::Exit(rc);

View File

@ -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<std::string>& 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;

View File

@ -49,8 +49,9 @@ public:
static void Unregister(const std::vector<String>& 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<std::string>& 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);