Skip to content
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

Add support for exclusive/dependant arguments #232

Open
f-michaut opened this issue Nov 6, 2022 · 3 comments
Open

Add support for exclusive/dependant arguments #232

f-michaut opened this issue Nov 6, 2022 · 3 comments

Comments

@f-michaut
Copy link

f-michaut commented Nov 6, 2022

Some arguments are exclusive.
Current workaround is to implement checks after the parsing like so :

[...]
program.parse_args(ac, av);

if (program.is_used("--foo") && program.is_used("--bar")) {
    throw "--foo and --bar are exclusive !";
}

Furthermore, some parameters often require others to be present in order to be meaningfull.
Once again, we currently need to check manualy that both are given.


It would be nice to be able to use constructs like this :

program.add_argument("--line").needs("--file");
program.add_argument("--file").exclusive("--ip");
program.add_argument("--ip"); // No need to specify again that they are exclusive
program.add_argument("--port").needs("--ip").default_value(443);

try {
  program.parse_args(ac, av);
} catch (const std::runtime_error& err) {
  std::cerr << "Wrong arguments: " << err.what() << std::endl;
}

Usage :

# Good
./program --file /tmp/file
./program --file /tmp/file --line 25
./program --ip 8.8.8.8
./program --ip 8.8.8.8 --port 80

# Bad
./program --ip 8.8.8.8 --line 25
./program --line 25
./program --port 80
./program --file /tmp/file --port 80
./program --file /tmp/file --ip 8.8.8.8
@p-ranav
Copy link
Owner

p-ranav commented Nov 4, 2023

Added support for mutually exclusive arguments here: #301

@f-michaut
Copy link
Author

Thank you for adding exclusive arguments ! That's very helpfull.

Do you intend to add dependant arguments as well ?

@p-ranav
Copy link
Owner

p-ranav commented Nov 12, 2023

I think so, although I don't have a complete design yet.

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

No branches or pull requests

2 participants