Skip to content

Allow for earlier exit with non matching options #986

Open
@levi-de-koning

Description

@levi-de-koning

During testing, we encountered an unexpected error message. Let me introduce this small sample to explain the error message and how it confused us.

CLI::App testApp;
int value;
testApp.add_option("--foo", value);
std::string file;
testApp.add_option("file", file)->check(CLI::ExistingFile);

CLI11_PARSE(testApp);

We will use -bar 5 somefile.txt for the command line arguments. We can already see this command line is invalid because -bar is not a valid option. When we run this, we get the error: file: File does not exist: 5. To a user, this is a super unexpected error as well. They specified some file.txt as their filename. Now, let's say we remove this validator. The error changes to The following arguments were not expected: --bar. This is the actual logical error I would expect for this command line.

The underlying reason for this is that. --bar will fail to match any subcommand and is added to the missing/remaining list. then 5 is parsed next, and this matches the position argument, which now fails the validation.

So, in conclusion, it would be nice if we could specify that any non-matching argument should throw an error. I don't necessarily think this should be the default, as you would lose the ability to get a list of all options that do not match. but at least being able to configure this would greatly clarify the error messages for cases like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions