升压程序选项描述的问题

发布于 2024-10-02 00:32:08 字数 1623 浏览 3 评论 0原文

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

另类 2024-10-09 00:32:08

您可以采用隐藏选项的技术 提供一组用于解析的选项和另一组用于打印帮助的选项。不过,这将要求您维护选项列表的两份副本。

例如,从当前的 desc 定义开始,然后在其下面添加另一个定义:

po::options_description display_options("Allowed options");
display_options.add_options()
    ("help", "produce help message")
    ("optimization", po::value<int>(), "optimization level")
    ("verbose", "enable verbosity (optionally specify level)")
    ;

继续使用 desc 解析命令行选项:

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 << display_options; // not desc
    return 0;
}

这应该显示 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:

po::options_description display_options("Allowed options");
display_options.add_options()
    ("help", "produce help message")
    ("optimization", po::value<int>(), "optimization level")
    ("verbose", "enable verbosity (optionally specify level)")
    ;

Continue to use desc for parsing the command-line options:

po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
po::notify(vm);

But use the new option description for showing the documentation:

if (vm.count("help"))
{
    cout << "Usage: options_description [options]\n";
    cout << display_options; // not desc
    return 0;
}

That should show that optimization has an argument, but it won't reveal what its default value is, and it won't acknowledge that verbose accepts any argument at all.

稀香 2024-10-09 00:32:08

如果它不是内置的,您可以根据 boost 标头中的内容调整 options_description 并实现您自己的 operator<<

您也许可以使用 print 方法来执行此操作。

operator<< 只需调用 print 即可你靠你自己的afai可以看到。

If it's not built-in, you could adapt options_description and implement your own operator<< based on what's in the boost header.

You might be able to do this using the print method.

operator<< just calls through to print so you are on your own afai can see.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文