mirror of https://github.com/Icinga/icinga2.git
CLI framework: Add support for unrecognized parameters
Required for feature enable command for example. fixes #7371
This commit is contained in:
parent
15bd96aa9a
commit
3513d1f2f9
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -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.");
|
||||
|
|
|
@ -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;
|
||||
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue