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