CLI framework: Add support for unrecognized parameters

Required for feature enable command for example.

fixes #7371
This commit is contained in:
Michael Friedrich 2014-10-13 18:07:52 +02:00
parent 15bd96aa9a
commit 3513d1f2f9
9 changed files with 68 additions and 59 deletions

View File

@ -142,9 +142,10 @@ int Main(void)
String cmdname;
CLICommand::Ptr command;
po::variables_map vm;
std::vector<std::string> ap;
try {
CLICommand::ParseCommand(argc, argv, visibleDesc, hiddenDesc, vm, cmdname, command, autocomplete);
CLICommand::ParseCommand(argc, argv, visibleDesc, hiddenDesc, vm, ap, cmdname, command, autocomplete);
} catch (const std::exception& ex) {
std::ostringstream msgbuf;
msgbuf << "Error while parsing command-line options: " << ex.what();
@ -348,7 +349,7 @@ int Main(void)
CLICommand::ShowCommands(argc, argv, &visibleDesc, &hiddenDesc, true, autoindex);
rc = 0;
} else if (command)
rc = command->Run(vm);
rc = command->Run(vm, ap);
#ifndef _DEBUG
Application::Exit(rc);

View File

@ -65,7 +65,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,
String& cmdname, CLICommand::Ptr& command, bool autocomplete)
std::vector<std::string>& ap, String& cmdname,
CLICommand::Ptr& command, bool autocomplete)
{
boost::mutex::scoped_lock lock(l_RegistryMutex);
@ -115,7 +116,13 @@ found_command:
adesc.add(visibleDesc);
adesc.add(hiddenDesc);
po::store(po::parse_command_line(argc - arg_end, argv + arg_end, adesc), vm);
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::notify(vm);
return true;

View File

@ -42,7 +42,7 @@ public:
virtual String GetDescription(void) const = 0;
virtual String GetShortDescription(void) const = 0;
virtual void InitParameters(boost::program_options::options_description& visibleDesc, boost::program_options::options_description& hiddenDesc) const = 0;
virtual int Run(const boost::program_options::variables_map& vm) const = 0;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const = 0;
static CLICommand::Ptr GetByName(const std::vector<String>& name);
static void Register(const std::vector<String>& name, const CLICommand::Ptr& command);
@ -50,7 +50,8 @@ public:
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, String& cmdname, CLICommand::Ptr& command, bool autocomplete);
boost::program_options::variables_map& vm, std::vector<std::string>& ap, 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);
};

View File

@ -299,7 +299,7 @@ void DaemonCommand::InitParameters(boost::program_options::options_description&
*
* @returns An exit status.
*/
int DaemonCommand::Run(const po::variables_map& vm) const
int DaemonCommand::Run(const po::variables_map& vm, const std::vector<std::string>& ap) const
{
ScriptVariable::Set("UseVfork", true, false, true);

View File

@ -41,7 +41,7 @@ public:
virtual String GetShortDescription(void) const;
virtual void InitParameters(boost::program_options::options_description& visibleDesc,
boost::program_options::options_description& hiddenDesc) const;
virtual int Run(const boost::program_options::variables_map& vm) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
};
}

View File

@ -49,7 +49,7 @@ void PKINewCACommand::InitParameters(boost::program_options::options_description
*
* @returns An exit status.
*/
int PKINewCACommand::Run(const boost::program_options::variables_map& vm) const
int PKINewCACommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
{
String cadir = Application::GetLocalStateDir() + "/lib/icinga2/ca";

View File

@ -40,7 +40,7 @@ public:
virtual String GetShortDescription(void) const;
virtual void InitParameters(boost::program_options::options_description& visibleDesc,
boost::program_options::options_description& hiddenDesc) const;
virtual int Run(const boost::program_options::variables_map& vm) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
};

View File

@ -52,7 +52,7 @@ void PKINewCertCommand::InitParameters(boost::program_options::options_descripti
*
* @returns An exit status.
*/
int PKINewCertCommand::Run(const boost::program_options::variables_map& vm) const
int PKINewCertCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
{
if (!vm.count("cn")) {
Log(LogCritical, "cli", "Common name (--cn) must be specified.");

View File

@ -40,7 +40,7 @@ public:
virtual String GetShortDescription(void) const;
virtual void InitParameters(boost::program_options::options_description& visibleDesc,
boost::program_options::options_description& hiddenDesc) const;
virtual int Run(const boost::program_options::variables_map& vm) const;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const;
};