如何在——help输出中显示命令行操作数描述
How to display commandline operand description in --help output
我正在使用Boost。program_options解析POSIX实用程序实现的命令行。以cmp
为例,
现在我想有一个额外的参数--help
,它显示了所有参数的描述,这在这种情况下很重要。我:
po::options_description options("Options");
options.add_options()("help", "Show this help output.")
(",l", "(Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference.")
(",s", "Write nothing for differing files; return exit status only.")
po::positional_options_description operands;
operands.add("file1", 1);//, "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.")
operands.add("file2", 1);//, "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.");
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(options).positional(operands).run(), vm);
po::notify(vm);
if(vm.count("help"))
{
std::cout << "cmp: compare two filesnUsage: cmp [ -l | -s ] file1 file2n" << options;
return 0;
}
无法显示file1
和file2
选项的描述。我当然可以将它们添加到options
,但这会添加至少两个不想要的参数[-]-file{1,2}
,这是我真的不想要的。我只想要--help
的输出(没有明显的硬编码):
cmp: compare two files
Usage: cmp [ -l | -s ] file1 file2
Options:
--help Show this help output.
-l (Lowercase ell.) Write the byte number (decimal) and the differing bytes (octal) for each difference.
-s Write nothing for differing files; return exit status only.
Operands:
file1 A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.
file2 A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.
有没有什么方法可以在不破坏图书馆的情况下实现这一点?我认为这是非常基本的东西,但我在任何教程中都找不到。
UPDATE为了大家的利益,我提交了一个功能请求,希望以向后兼容的方式。
这并不理想,但是创建一个"虚拟"程序选项集,让boost::program_options格式化器为您格式化其帮助文本,然后通过快速替换删除虚线怎么样?
然后输出options
帮助文本之外的帮助文本。
像这样:
po::options_description dummy_options("Operands");
dummy_options.add_options()
("file1", po::value<std::string>(), "A pathname of the first file to be compared. If file1 is '-', the standard input shall be used.")
("file2", po::value<std::string>(), "A pathname of the second file to be compared. If file2 is '-', the standard input shall be used.")
;
std::stringstream s;
s << dummy_options;
std::string dummy_help_text = s.str();
boost::replace_all(dummy_help_text, "--", "");
boost::replace_all(dummy_help_text, "arg", " ");
std::cout << dummy_help_text << std::endl;
输出如下所示:
Operands:
file1 A pathname of the first file to be compared. If file1
is '-', the standard input shall be used.
file2 A pathname of the second file to be compared. If file2
is '-', the standard input shall be used.
这是不理想的,因为,除其他事项外,列之间的间距将不匹配其他options
输出的帮助输出。但对于一些又快又脏的东西,这基本上是可行的。
无论如何,这是一个想法。
(我在Boost中没有看到任何东西。Program_Options API,它允许您以"正确"的方式执行此操作。https://stackoverflow.com/a/3621947/368896给出了一个提示,这类事情是不支持的,但这是3年前的事了。
相关文章:
- 从命令行c++发送文本文件名
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 数组长度,为什么从命令行获取时不能使用它?
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 通过 Openssl 命令行加密,通过 c++ 解密
- 在命令行上将输入显示到数组中时如何删除 ./a.out?
- 使用 Visual Studio 2019 显示 CMake 的编译器命令行
- 当我仅使用链接器选项 /LTCG时,为什么Visual Studio在Linker命令行中显示选项 /PGD
- 如何在命令行lldb中显示CGAL异常
- 整数命令行参数都显示为2
- 显示命令行应用程序中的进度
- *忽略* C++和 C# 如何在命令行中显示百分比
- 在变量上显示默认文本,用户可以在C++命令行程序上编辑这些变量
- 从命令行启动时,在for循环期间不显示C++cout语句
- 如何在——help输出中显示命令行操作数描述
- 是否可以在不显示窗口的情况下启动命令行应用程序
- Git 状态显示奇怪的未跟踪"path_of_filer"文件,如何通过命令行删除