15

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
  • you want the help text to only disable short options?
    – Sam Miller
    Sep 1, 2010 at 21:56
  • 1
    No I only want options such as -v instead of --verbose or defining both. Specifying ",v" instead of "v" does the trick but uglifies the print listing of the description.
    – Novikov
    Sep 1, 2010 at 23:27

1 Answer 1

15

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.

1
  • 1
    @Novikov You are welcome. FYI - I've just created a ticket for your issue and partially fixed that format_name() function. See svn.boost.org/trac/boost/ticket/4644
    – user405725
    Sep 12, 2010 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.