argparse icon indicating copy to clipboard operation
argparse copied to clipboard

Custom `help` displays error of not given required argument

Open Hind-M opened this issue 3 years ago • 1 comments

For a program that has at least one required argument, using a custom help - as the following - gives an error which shouldn't be taken into account when running the help command:

program.add_argument("-h", "--help")
    .action([&](const std::string & /*unused*/)
    {
        std::cout << program.help().str();
    })
    .default_value(false)
    .help("shows help message")
    .implicit_value(true)
    .nargs(0);

The error is:

1 argument(s) expected. 0 provided.
No value provided for 'required_arg'.

Hind-M avatar Dec 02 '22 13:12 Hind-M

If you replace the default --help handler and have required arguments, you'll need to catch errors during parse_args and add a custom way to recover from the missing required argument.

Or don't require any arguments and check that a value was parsed and assigned to your formerly required arguments. Then you can handle the missing value with whatever feedback mechanism fits your UI.

skrobinson avatar Dec 07 '22 15:12 skrobinson