14

我需要能够boost::program_options解析在命令行上传递的双精度数组。对于正双打,这当然没问题(std::vector<double>在 add_options 中使用 multitoken),但对于负双打,我知道这些是模棱两可的论点。

这是我想要接受的演示:

mycommand --extent -1.0 -2.0 -3.0 1.0 2.0 3.0 --some-other-argument somevalue

extent将由一个Bounds类支持,该类至少有一个构造函数,该构造函数接受六个单独的T参数(在本例中为 -- double)。

template <typename T>
class Bounds
{
public:
    typedef T value_type;
    typedef typename std::vector< Range<T> >::size_type size_type;

    typedef typename std::vector< Range<T> > Ranges;

    Bounds( T minx, T miny, T minz, 
            T maxx, T maxy, T maxz)
    {
        // fill Ranges vector
    }

private:
    Ranges ranges;
};

我还必须提供什么来支持在Bounds课堂上使用 add_options ?我想做类似的事情。可能的?

namespace po = boost::program_options;
po::options_description options("options");

options.add_options()
    ("extent,e", po::value< Bounds< double > >(), "Extent to clip points to")

po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
  options(options).positional(p).run(), vm);

po::notify(vm);

if (vm.count("extent")) 
{
    Bounds<double> bounds = vm["extent"].as< Bounds<double> >();
    // do other stuff
}
4

3 回答 3

8

诀窍是强制boost将所有数字分类为位置值(不要与 混淆positional_options_description。您这样做的方式是定义 astyle_parser并将其提供给command_line_parseras a extra_style_parser

    #include <boost/program_options/option.hpp>
    #include <boost/lexical_cast/try_lexical_convert.hpp>
    #include <boost/program_options/value_semantic.hpp>

    using po = boost::program_options;

    std::vector<po::option> ignore_numbers(std::vector<std::string>& args)
    {
        std::vector<po::option> result;
        int pos = 0;
        while(!args.empty()) {
            const auto& arg = args[0];
            double num;
            if(boost::conversion::try_lexical_convert(arg, num)) {
                result.push_back(po::option());
                po::option& opt = result.back();

                opt.position_key = pos++;
                opt.value.push_back(arg);
                opt.original_tokens.push_back(arg);

                args.erase(args.begin());
            } else {
                break;
            }
        }

        return result;
    }

一旦你有了它,你就是这样使用它的:

    po::store(po::command_line_parser(argc, argv)
        .extra_style_parser(&po::ignore_numbers)
        .options(commands)
        .run(), vm);

您现在可以同时使用负数和短命令行参数。

但是,仍然存在一个问题,没有办法限制每个参数采用的标记数量,如果使用位置参数,这可能会出现问题。例如,这样的事情是行不通的:

    foo --coords 1 2 3 4 bar.baz

为了解决这个问题,我们需要添加一种方法来强制参数所需的令牌数量:

    template<class T, class charT = char>
    class bounded_typed_value : public po::typed_value<T, charT>
    {
    public:
        bounded_typed_value(T* store_to)
            : typed_value<T, charT>(store_to), m_min(-1), m_max(-1) {}

        unsigned min_tokens() const {
            if(m_min < 0) {
                return po::typed_value<T, charT>::min_tokens();
            } else {
                return (unsigned)m_min;
            }
        }

        unsigned max_tokens() const {
            if(m_max < 0) {
                return po::typed_value<T, charT>::max_tokens();
            } else {
                return (unsigned)m_max;
            }
        }

        bounded_typed_value* min_tokens(unsigned min_tokens)
        {
            if(min_tokens > 1) {
                po::typed_value<T, charT>::multitoken();
            }

            m_min = min_tokens;

            return this;
        }

        bounded_typed_value* max_tokens(unsigned max_tokens)
        {
            if(max_tokens > 1) {
                po::typed_value<T, charT>::multitoken();
            }

            m_max = max_tokens;

            return this;
        }

        bounded_typed_value* fixed_tokens(unsigned num_tokens)
        {
            if(num_tokens > 1) {
                po::typed_value<T, charT>::multitoken();
            }

            m_min = num_tokens;
            m_max = num_tokens;

            return this;
        }

    private:
        int m_min;
        int m_max;
    };


    template<class T, class charT = char>
    bounded_typed_value<T, charT>*
    bounded_value()
    {
        return new bounded_typed_value<T, charT>(0);
    }

你现在可以像这样把它们放在一起:

    po::positional_options_description p;
    p.add("file-name", -1);

    boost::program_options::options_description desc;
    desc.add_options()
        ("coords,c", boost::program_options::bounded_value<std::vector<double>>()->fixed_tokens(4), "Bounding box");

    po::store(po::command_line_parser(argc, argv)
        .extra_style_parser(&po::ignore_numbers)
        .positional(p)
        .options(commands)
        .run(), vm);
于 2016-06-23T13:46:51.673 回答
2

此处指定的处理负数的方法也可能对您有用。

我正在用简单的解析器解析它

store(command_line_parser(argc, argv).options(commands).run(), vm);

,但解决方案是使用扩展的:

parse_command_line
于 2010-11-05T15:16:46.037 回答
2

简单的方法是将参数用引号括起来: mycommand --extent '-1.0 -2.0 -3.0 1.0 2.0 3.0' --some-other-argument somevalue

于 2010-11-30T23:09:01.403 回答