升压程序选项描述的问题
这是 boost::program_options 中的示例之一的精简版本:
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <iostream>
#include <iterator>
using namespace std;
int main( int argc, char *argv[ ] )
{
try {
int opt;
po::options_description desc("Allowed options");
desc.add_options()
( "help", "produce help message" )
( "optimization", po::value< int >(&opt)->default_value(10), "optimization level" )
( "verbose", po::value< int >()->implicit_value( 1 ), "enable verbosity (optionally specify level)" )
;
po::variables_map vm;
po::store( po::command_line_parser( argc, argv ).options( desc ).run(), vm );
po::notify( vm );
if ( vm.count( "help" ) )
{
cout << "Usage: options_description [options]\n";
cout << desc;
return 0;
}
if ( vm.count( "verbose" ) )
{
cout << "Verbosity enabled. Level is " << vm[ "verbose" ].as< int >() << "\n";
}
}
catch( std::exception& e )
{
cout << e.what() << "\n";
return 1;
}
return 0;
}
当我使用 --help 运行此命令时,我得到以下输出:
Usage: options_description [options]
Allowed options:
--help produce help message
--optimization arg (=10) optimization level
--verbose [=arg(=1)] enable verbosity (optionally specify level)
有没有办法摆脱选项列中的 = 或可能只是删除显示默认参数和隐式参数?
Here is a striped down version of one of the examples in boost::program_options:
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <iostream>
#include <iterator>
using namespace std;
int main( int argc, char *argv[ ] )
{
try {
int opt;
po::options_description desc("Allowed options");
desc.add_options()
( "help", "produce help message" )
( "optimization", po::value< int >(&opt)->default_value(10), "optimization level" )
( "verbose", po::value< int >()->implicit_value( 1 ), "enable verbosity (optionally specify level)" )
;
po::variables_map vm;
po::store( po::command_line_parser( argc, argv ).options( desc ).run(), vm );
po::notify( vm );
if ( vm.count( "help" ) )
{
cout << "Usage: options_description [options]\n";
cout << desc;
return 0;
}
if ( vm.count( "verbose" ) )
{
cout << "Verbosity enabled. Level is " << vm[ "verbose" ].as< int >() << "\n";
}
}
catch( std::exception& e )
{
cout << e.what() << "\n";
return 1;
}
return 0;
}
When I run this with a --help I get this output:
Usage: options_description [options]
Allowed options:
--help produce help message
--optimization arg (=10) optimization level
--verbose [=arg(=1)] enable verbosity (optionally specify level)
Is there a way to get rid of the = in the options column or possibly just drop the display of default and implicit args?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以采用隐藏选项的技术 提供一组用于解析的选项和另一组用于打印帮助的选项。不过,这将要求您维护选项列表的两份副本。
例如,从当前的
desc
定义开始,然后在其下面添加另一个定义:继续使用
desc
解析命令行选项:但使用新的选项描述用于显示文档:
这应该显示
optimization
有一个参数,但它不会透露其默认值是什么,并且不会承认verbose
接受任何根本没有争论。You can adapt the technique of hidden options to provide one set of options for parsing and another set for printing the help. This will require you to maintain two copies of the option list, though.
For example, start with your current
desc
definition, and then below it add another one:Continue to use
desc
for parsing the command-line options:But use the new option description for showing the documentation:
That should show that
optimization
has an argument, but it won't reveal what its default value is, and it won't acknowledge thatverbose
accepts any argument at all.如果它不是内置的,您可以根据 boost 标头中的内容调整
options_description
并实现您自己的operator<<
。您也许可以使用print
方法来执行此操作。operator<<
只需调用print
即可你靠你自己的afai可以看到。If it's not built-in, you could adapt
options_description
and implement your ownoperator<<
based on what's in the boost header.You might be able to do this using theprint
method.operator<<
just calls through toprint
so you are on your own afai can see.