Required and Optional Arguments Using Boost Library Program Options

后端 未结 4 984
情话喂你
情话喂你 2020-12-12 15:07

I\'m using Boost Program Options Library to parse the command line arguments.

I have the following requirements:

  1. Once \"help\" is provided, all the oth
4条回答
  •  一整个雨季
    2020-12-12 15:08

    I've run into this issue myself. The key to a solution is that the function po::store populates the variables_map while po::notify raises any errors encountered, so vm can be used prior to any notifications being sent.

    So, as per Tim, set each option to required, as desired, but run po::notify(vm) after you've dealt with the help option. This way it will exit without any exceptions thrown. Now, with the options set to required, a missing option will cause a required_option exception to be thrown and using its get_option_name method you can reduce your error code to a relatively simple catch block.

    As an additional note, your option variables are set directly via the po::value< -type- >( &var_name ) mechanism, so you don't have to access them through vm["opt_name"].as< -type- >().

    A code example is provided in Peters answer

提交回复
热议问题