Short options only in boost::program_options

后端 未结 1 1526
被撕碎了的回忆
被撕碎了的回忆 2021-01-08 00:21

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

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


        
相关标签:
1条回答
  • 2021-01-08 01:08

    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;
            }
    }
    

    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.

    0 讨论(0)
提交回复
热议问题