我想在我的程序中实现子命令。我还需要能够为不同的子命令提供不同的参数选项。使用Boost.Program_options做这件事的最佳方式是什么?
子命令在svn、git和apt-get等程序中使用。
例如,在GIT中,一些可用的子命令是:
git status
git push
git add
git pull
我的问题基本上和这个人的一样:http://boost.2283326.n4.nabble.com/subcommands-with-program-options-like-svn-command-td2585537.html
发布于 2014-04-16 11:42:16
如果我没理解错的话,您需要解析以下形式的命令行选项:
[--generic-option ...] cmd [--cmd-specific-option ... ]
这是我的示例解决方案。为了清楚起见,我将省略任何验证代码,但希望您能看到如何相当简单地添加它。
在本例中,我们有"ls“子命令,可能还有其他子命令。每个子命令都有一些特定的选项,此外还有一些通用选项。因此,让我们从解析通用选项和命令名开始。
po::options_description global("Global options");
global.add_options()
("debug", "Turn on debug output")
("command", po::value<std::string>(), "command to execute")
("subargs", po::value<std::vector<std::string> >(), "Arguments for command");
po::positional_options_description pos;
pos.add("command", 1).
add("subargs", -1);
po::variables_map vm;
po::parsed_options parsed = po::command_line_parser(argc, argv).
options(global).
positional(pos).
allow_unregistered().
run();
po::store(parsed, vm);
请注意,我们为命令名创建了一个位置选项,并为命令选项创建了多个位置选项。
现在,我们对相关的命令名进行分支并重新解析。我们现在以字符串数组的形式传递无法识别的选项,而不是传入原始的argc
和argv
。collect_unrecognized
函数可以提供这一点-我们所要做的就是删除(位置)命令名,并使用相关的options_description
重新解析。
std::string cmd = vm["command"].as<std::string>();
if (cmd == "ls")
{
// ls command has the following options:
po::options_description ls_desc("ls options");
ls_desc.add_options()
("hidden", "Show hidden files")
("path", po::value<std::string>(), "Path to list");
// Collect all the unrecognized options from the first pass. This will include the
// (positional) command name, so we need to erase that.
std::vector<std::string> opts = po::collect_unrecognized(parsed.options, po::include_positional);
opts.erase(opts.begin());
// Parse again...
po::store(po::command_line_parser(opts).options(ls_desc).run(), vm);
请注意,我们对特定于命令的选项和通用选项使用了相同的variables_map
。由此,我们可以执行相关的操作。
这里的代码片段取自一个可编译的源文件,其中包括一些单元测试。你可以在gist here上找到它。请随时下载并使用它。
https://stackoverflow.com/questions/15541498
复制相似问题