短选项仅在升压:: program_options [英] Short options only in boost::program_options

查看:105
本文介绍了短选项仅在升压:: program_options的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个着手推动中没有他们的同行长短期指定的选项?

How would one go about specifying short options without their long counterparts in boost?

(",w", po::value<int>(), "Perfrom write with N frames")

生成此

-w [ -- ] arg : Perfrom write with N frames

要指定短选项的任何方式只?

Any way to specify short options only?

推荐答案

如果您正在使用命令行解析器,有一种方法来设置不同的风格。因此该解决方案将是使用仅长选择,使allow_long_disguise样式允许长选项与一个短划线(即,-long_option)来指定。下面是一个例子:

If you are using command line parser, there is a way to set different styles. So the solution would be to use only long options and enable allow_long_disguise style which allows long options to be specified with one dash (i.e. "-long_option"). Here is an example:

#include <iostream>
#include <boost/program_options.hpp>

namespace options = boost::program_options;
using namespace std;

int
main (int argc, char *argv[])
{
        options::options_description desc (string (argv[0]).append(" options"));
        desc.add_options()
            ("h", "Display this message")
        ;
        options::variables_map args;
        options::store (options::command_line_parser (argc, argv).options (desc)
                        .style (options::command_line_style::default_style |
                                options::command_line_style::allow_long_disguise)
                        .run (), args);
        options::notify (args);
        if (args.count ("h"))
        {
            cout << desc << endl;
            return 0;
        }
}

有将与输出描述一个小问题,但:

There will be a little problem with the description output though:

$ ./test --h
./test options:
  --h                   Display this message

和一个难以解决,因为这是被用于形成本输出:

And that one is hard to fix because this is what is being used to form this output:

std::string
option_description::format_name() const
{
    if (!m_short_name.empty())
        return string(m_short_name).append(" [ --").
        append(m_long_name).append(" ]");
    else
        return string("--").append(m_long_name);
}

本该想到的是更换唯一的解决方法中得到的字符串 - - 和。例如:

The only fix for this that comes to mind is replacing "--" with "-" in resulting string. For example:

#include <iostream>
#include <sstream>
#include <boost/program_options.hpp>
#include <boost/algorithm/string/replace.hpp>

namespace options = boost::program_options;
using namespace std;

int
main (int argc, char *argv[])
{
        options::options_description desc (string (argv[0]).append(" options"));
        desc.add_options()
            ("h", "Display this message")
        ;
        options::variables_map args;
        options::store (options::command_line_parser (argc, argv).options (desc)
                        .style (options::command_line_style::default_style |
                                options::command_line_style::allow_long_disguise)
                        .run (), args);
        options::notify (args);
        if (args.count ("h"))
        {
            std::stringstream stream;
            stream << desc;
            string helpMsg = stream.str ();
            boost::algorithm::replace_all (helpMsg, "--", "-");
            cout << helpMsg << endl;
            return 0;
        }
}

你能做的最好的事情是修复code其中它打印空长选项说明并发送补丁库的作者。

The best thing you can do is to fix the code where it prints empty long option description and send a patch to the author of the library.

这篇关于短选项仅在升压:: program_options的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆