Skip to content

Fix parsing 0 parameters when using choices#423

Open
ebothmann wants to merge 1 commit intop-ranav:masterfrom
ebothmann:master
Open

Fix parsing 0 parameters when using choices#423
ebothmann wants to merge 1 commit intop-ranav:masterfrom
ebothmann:master

Conversation

@ebothmann
Copy link
Copy Markdown

Before, when an option used choices, but the user passed no argument, instead of giving the too-few-arguments given error, a bad any cast exception was thrown. This is now fixed by not accidentally changing num_args_max to 0 before the number-of-arguments check.

E.g. running ./program --color (i.e. no arguments)

    parser.add_argument("--color")
        .help("configure the use of colors in output")
        .choices("auto", "always", "never");

caused the (unhelpful) exception

libc++abi: terminating due to uncaught exception of type std::bad_any_cast: bad any cast

at the line

auto fn = parser.present("--color");

With the change, the same invocation gives us the much more helpful

Too few arguments for '--color'.

Before, when an option used choices, but the user passed no argument,
instead of giving the too-few-arguments given error, a bad any cast
exception was thrown. This is now fixed by not accidentally changing
`num_args_max` to 0 before the number-of-arguments check.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant