views:

89

answers:

1

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

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

generates this

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

Any way to specify short options only?

+2  A: 

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.

Vlad Lazarenko
Very insightful, thanks!
Novikov
@Novikov You are welcome. FYI - I've just created a ticket for your issue and partially fixed that format_name() function. See https://svn.boost.org/trac/boost/ticket/4644
Vlad Lazarenko