I need to use following syntax for a program:
myprogram config.ini --option1 value --option2 value2
I'm using something like following:
namespace po = boost::program_options;
po::options_description desc("Allowed options");
desc.add_options()
("option1", po::value<std::string>()->required(), "option 1")
("option2", po::value<uint16_t>()->required(), "option 2")
("help", "this message");
po::variables_map opts;
po::store(po::command_line_parser(argc, argv).options(desc).run(), opts);
if (opts.count("help")) {
show_help(desc);
return 0;
}
po::notify(opts);
Can Boost.Program_options be used for catching first parameter (config.ini
)? Or any value without option specifier?
According to the documentation, these can be handled with positional arguments.
You can find another nice example here, under Specifying Positional Options.
If I understand your intended functionality, here's how you would bring it together to work in your sample above.
namespace po = boost::program_options;
po::options_description desc( "Allowed options" );
desc.add_options( )
( "option1", po::value<std::string>( )->required( ), "option 1" )
( "option2", po::value<uint16_t>( )->required( ), "option 2" )
// this flag needs to be added to catch the positional options
( "config-file", po::value<std::string>( ), ".ini file" )
( "help", "this message" );
po::positional_options_description positionalDescription;
// given the syntax, "config.ini" will be set in the flag "config-file"
positionalDescription.add( "config-file", -1 );
po::variables_map opts;
po::store(
po::command_line_parser( argc, argv )
.options( desc )
// we chain the method positional with our description
.positional( positionalDescription )
.run( ),
opts
);
if (opts.count( "help" ))
{
show_help( desc );
return 0;
}
po::notify( opts );