Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown options are silently ignored and mixed with positional arguments #58

Closed
lahwaacz opened this issue Nov 27, 2021 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@lahwaacz
Copy link

When there are options as well as positional arguments, some of which may be optional, there is a problem with unknown options. Consider this repro case:

int foo = 0;
std::string positional;
std::string optional = "default";

auto cli = lyra::cli();
cli |= lyra::opt(foo, "foo")["--foo"];
cli |= lyra::arg(positional, "positional");
cli |= lyra::arg(optional, "optional").optional();

auto result = cli.parse(lyra::args({ argv[0], "--foo", "1", "--bar", "a" }));
if (!result) {
    std::cerr << "Error in command line: " << result.errorMessage() << std::endl;
    std::cerr << cli << std::endl;
    return EXIT_FAILURE;
}

std::cout << "foo = " << foo << std::endl;
std::cout << "positional = " << positional << std::endl;
std::cout << "optional = " << optional << std::endl;

which leads to this output:

foo = 1
positional = --bar
optional = a

This is a problem, because --bar as is not reported as unknown/invalid option, but passed to the next positional argument. It is pretty unlikely that the user intended to use --bar as a positional argument -- most likely they made a typo or another mistake and need to read the help page.

@grafikrobot grafikrobot added the bug Something isn't working label Dec 6, 2021
@grafikrobot
Copy link
Member

This is related to #24

@lahwaacz
Copy link
Author

lahwaacz commented Dec 6, 2021

GNU tools and many others use the -- argument as separator indicating that all following arguments are positionals rather than options. See e.g. grep example.

@grafikrobot
Copy link
Member

GNU tools and many others use the -- argument as separator indicating that all following arguments are positionals rather than options. See e.g. grep example.

Right. There are two choices.. You do not treat "--xxx" as a special prefix that only means an option. Which is what Lyra does currently. Or you do make "--xxx" special and then need the special token, i.e. "--"by itself, to turn off the restriction. The only way for a CLI to support that would be have two different, mutually exclusive, parsing modes.

@bfgroup bfgroup locked and limited conversation to collaborators Dec 8, 2021
@grafikrobot grafikrobot converted this issue into discussion #60 Dec 8, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants