根据第一个选项选择选项组
Selecting option group according the first option
我已经实现了一个应用程序(让我们称之为"app"),它执行两种类型的任务,TA
和TB
。每种任务类型的命令行选项都不同。这就是为什么我决定用户将用"命令"指定任务类型:
app command_a --opt_1 --opt_2 # For TA
app command_b --opt_3 --opt_4 # For TB
当然,command_a
和command_b
是相互排斥的(你可以在这里看到如何做到这一点)。
问题1:选择用于解析命令行的options_description对象的最佳方法是什么?
问题2:实现命令帮助系统的最佳方式是什么?示例:
app help command_a # Display help for TA
这是我在研究库2个小时后可以实现的最佳答案。我把这个作为答案发布是因为在某种程度上给出了这个问题的解决方案,但我知道很有可能有人提出了更好的解决方案。
选择用于解析命令行的options_description对象的最佳方法是什么
到目前为止,我成功地根据第一个选项在两个options_description对象之间进行了选择(有关更多详细信息,请参阅代码)。我所做的是:
- 创建两个选项_描述对象(
OptDescA
用于TA
,OptDescB
用于TB
) - 检查第一个参数是
command_a
还是command_b
- 根据第一个参数,我用
OptDescA
或OptDescB
解析命令行
对于点3,我必须将argc
递减1,并将argv
指针向前移动1。
po::store(po::parse_command_line(argc - 1, argv + 1, OptDescA), vm);
这样我就不必在OptDescA
或OptDescB
中处理command_a
或command_b
。
实现"命令"帮助系统的最佳方式是什么
嗯,这对我来说是最困难的。有关实现的详细信息,请参阅下面的代码。
我的帮助系统的问题是我必须键入:
app help command_a
相反,最常见的是:
app command_a help
此外,在您键入应用程序帮助后,输出为:
Available commands:
--help arg Display this message
--command_a Do command_a stuff
--command_b Do command_b stuff
注意丑陋的--help arg
代码
#include <iostream>
#include <string>
#include <boost/program_options.hpp>
using namespace std;
void help_system(string target);
int main(int argc, char *argv[])
{
namespace po = boost::program_options;
po::options_description command_a("Command_a options");
command_a.add_options()
("option_1", po::value<int>()->required(), "option_1 desc")
("option_2", po::value<int>()->required(), "option_2 desc")
;
po::options_description command_b("Command_b options");
command_b.add_options()
("option_3", po::value<int>()->required(), "option_3 desc")
("option_4", po::value<int>()->required(), "option_4 desc")
;
po::options_description commands("Available commands");
commands.add_options()
("help", po::value<string>()->default_value(""), "Display this message")
("command_a", "Do command_a stuff")
("command_b", "Do command_b stuff")
;
po::variables_map vm;
if (string("command_a") == string(*(argv + 1))) {
try{ po::store(po::parse_command_line(argc - 1, argv + 1, command_a), vm); }
catch (exception &e){ cout << "Error: " << e.what() << endl; }
}
else if (string("command_b") == string(*(argv + 1))) {
try{ po::store(po::parse_command_line(argc - 1, argv + 1, command_b), vm); }
catch (exception &e){ cout << "Error: " << e.what() << endl; }
}
else if (string("help") == string(*(argv + 1)))
{
cout << commands << endl;
}
try { po::notify(vm); }
catch (exception &e) { cout << "Error: " << e.what() << endl; }
return 0;
}
void help_system(string target)
{
if (target.c_str() == "command_a") {} // The ideal is to do "cout << command_a" here
// but right now command_a is out of the scope.
if (target.c_str() == "command_b") {}
}
相关文章:
- 两个函数模板候选项.将一个参数作为引用后,选择不太专业的模板
- 如何在 DirectX 11 应用程序中提供选择图形适配器的选项?
- 我如何在Qt中选择构建选项
- 如何防止在 switch 语句中多次选择同一选项/值
- 是/否总是选择第一个选项(C )
- 如何确定函数的返回值在编译时选择可能的选项
- 如何防止用户多次选择一个选项
- 在菜单前键入字符串后,C 如何从菜单中选择选项
- 如何在选择错误选项时保持程序运行而不退出
- 正在更新选项卡选择的CPropertyPage
- 选择哪个选项在C++中使用sql
- IFstream在选择菜单选项后无法打开文件
- Scons 选择哪个版本的 g++ |无法识别的命令行选项"-std=c++11"
- 在双括号 QString 之间仅选择一个选项
- 在提升构建中根据操作系统选择编译器选项
- 当选择一个选项时,如何执行另一个c++程序
- C 使用 Y/N 选项循环开关。选择 N 不会关闭程序
- 根据第一个选项选择选项组
- 带有人类可读选项选择器的c++类函数
- 仅为QFileSystemModel的第一列(驱动器、文件夹、文件)选择选项