When using boost::program_options, how does one set the name of the argument?

后端 未结 6 1850
借酒劲吻你
借酒劲吻你 2021-02-01 18:02

When using boost::program_options, how do I set the name of an argument for boost::program_options::value<>()?

#include 

        
6条回答
  •  你的背包
    2021-02-01 18:39

    The program_options::value_semantic class doesn't parameterize the argument name, so I think you will have to define your own class. Something like this:

    struct my_arg_type
        : public boost::program_options::typed_value
    {
        my_arg_type(std::string const& name)
            : boost::program_options::typed_value(&my_value)
            , my_name(name)
            , my_value(0)
        {
        }
        std::string name() const { return my_name; }
        std::string my_name;
        int my_value;
    };
    
    boost::program_options::options_description desc;
    
    my_arg_type arg("foo");
    desc.add_options()
        ("width", &arg, "give width");
    

    This should give something like:

    --witdh foo    give width
    

提交回复
热议问题