Pergunta

Here is a striped down version of one of the examples in boost::program_options:

#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include <iostream>
#include <iterator>
using namespace std;

int main( int argc, char *argv[ ] )
{
    try {
        int opt;
        po::options_description desc("Allowed options");
        desc.add_options()
            ( "help", "produce help message" )
            ( "optimization", po::value< int >(&opt)->default_value(10), "optimization level" )
            ( "verbose", po::value< int >()->implicit_value( 1 ), "enable verbosity (optionally specify level)"  )
        ;

        po::variables_map vm;
        po::store( po::command_line_parser( argc, argv ).options( desc ).run(), vm );
        po::notify( vm );


        if ( vm.count( "help" ) )
        {
            cout << "Usage: options_description [options]\n";
            cout << desc;
            return 0;
        }

        if ( vm.count( "verbose" ) )
        {
            cout << "Verbosity enabled.  Level is " << vm[ "verbose" ].as< int >() << "\n";
        }
    }
    catch( std::exception& e )
    {
        cout << e.what() << "\n";
        return 1;
    }    
    return 0;
}

When I run this with a --help I get this output:

Usage: options_description [options]
Allowed options:
  --help                   produce help message
  --optimization arg (=10) optimization level
  --verbose [=arg(=1)]     enable verbosity (optionally specify level)

Is there a way to get rid of the = in the options column or possibly just drop the display of default and implicit args?

Foi útil?

Solução

You can adapt the technique of hidden options to provide one set of options for parsing and another set for printing the help. This will require you to maintain two copies of the option list, though.

For example, start with your current desc definition, and then below it add another one:

po::options_description display_options("Allowed options");
display_options.add_options()
    ("help", "produce help message")
    ("optimization", po::value<int>(), "optimization level")
    ("verbose", "enable verbosity (optionally specify level)")
    ;

Continue to use desc for parsing the command-line options:

po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
po::notify(vm);

But use the new option description for showing the documentation:

if (vm.count("help"))
{
    cout << "Usage: options_description [options]\n";
    cout << display_options; // not desc
    return 0;
}

That should show that optimization has an argument, but it won't reveal what its default value is, and it won't acknowledge that verbose accepts any argument at all.

Outras dicas

If it's not built-in, you could adapt options_description and implement your own operator<< based on what's in the boost header.

You might be able to do this using the print method.

operator<< just calls through to print so you are on your own afai can see.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top