program_options - ambiguous option
Code snippet is below...basically I have 2 options I want to process "host,s" & "help,h". I would like to process options like this: for host: --host, -host, -s for help: --help, -help, -h Maybe I setup the opt_style variable wrong or is this correct behavior? I'm using boost 1.33.1 when i run the program:
po.exe -host localhost -h *** Command line processing error: ambiguous option h
thanks for any help, -Santosh ====================================================== #include <iostream> #include <string> #include "boost/program_options.hpp" using namespace std; namespace po = boost::program_options; int main(int, char *[]) { po::variables_map vm; po::options_description desc("Options"); try { string strHostname; desc.add_options() ("host,s", po::value<string>(&strHostname), "") ("help,h", "Show this help message"); po::options_description cmdline_options; cmdline_options.add(desc); int opt_style = (po::command_line_style::unix_style | po::command_line_style::allow_long_disguise | po::command_line_style::allow_slash_for_short); po::store(po::parse_command_line(argc, argv, cmdline_options, opt_style), vm); po::notify(vm); cout << "host: " << strHostname << endl; if( vm.count("help") ) cout << "help is true" << endl; } catch(exception &e) { cout << endl << "*** Command line processing error: " << e.what() << endl; } return 0; } __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Santosh Joseph wrote:
Code snippet is below...basically I have 2 options I want to process "host,s" & "help,h". I would like to process options like this: for host: --host, -host, -s for help: --help, -help, -h Maybe I setup the opt_style variable wrong or is this correct behavior? I'm using boost 1.33.1
when i run the program:
po.exe -host localhost -h *** Command line processing error: ambiguous option h
..... int opt_style = (po::command_line_style::unix_style | po::command_line_style::allow_long_disguise | po::command_line_style::allow_slash_for_short); The important points here are that 1. 'unix_style' includes 'allow_guessing' 2. you explicitly specify allow_long_disguise So, when program_options sees '-h' it first tries to parse it as long option, and due to 'allow_guessing' it checks for all long options starting with 'h'. There are two such options, so you get an ambiguity. If we first tried to parse every token as short option, then -help will be parsed as option -h with value 'elp' -- probably not what you want. I'd suggest that you mask out the allow_guessing style option. - Volodya
participants (2)
-
Santosh Joseph
-
Vladimir Prus