Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

Question/Feature request: Any way to do required_ifs "flags exists". #84

Open
epage opened this issue Dec 6, 2021 · 3 comments
Open

Comments

@epage
Copy link
Owner

epage commented Dec 6, 2021

Issue by 8176135
Tuesday Dec 12, 2017 at 04:54 GMT
Originally opened as clap-rs/clap#1126


Clap version: 2.29.0

I'll start with the problem I am trying to solve:
I have the flags -A, -B, -C, which are all optional by themselves. However, if both -A, and -B are used, -C must also be used. (-C takes some values that -A and -B need)

  • Clap has required_unless_all(name), but doesn't have the opposite of that e.g. required_if_all(name)?

  • I know clap has required_ifs(), but that compares the input from an argument with a value, but the value doesn't matter in this case, only that the flags are present or not.

  • Having requires() on -A and -B also won't work in this case, as only having one of them will make -C required.

  • requires_all() does the opposite of what I want, requiring both -A and -B to be used -C can be used

None of the above is unexpected, (the documentation is really good at explaining each option 👍 ), but there doesn't seem to be a required_if_all([names]). Any ideas?

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by kbknapp
Tuesday Jan 09, 2018 at 16:51 GMT


I'm not opposed to adding something like that, however I'll explain why it doesn't exist (yet).

When dealing with flags and requirements, since they are a simple on/off switch, if you know -A and -B requires -C (as the programmer), you could just act as if -C was used. I'd recommend telling the user via a help message or something, ("using -A and -B together implies -C", etc.).

From a UX standpoint, the user isn't supplying any additional information by being forced to pass -C since it's a flag. The only thing they're saying is essentially, "I know the -C switch is required."

The only time requiring an additional flag is a good idea is when the task is destructive (like deleting, etc.) and the user needs to confirm that they're sure they know what they're doing (by including the flag) such as --force usually, or -D in git branch.

However, in these circumstances it's far better to have a custom error message telling the user what is about to happen and why the extra flag is required. A generic clap error of, "-C is required but wasn't used" could actually be harmful here, since the user will almost always just blindly copy the -C argument and not know why.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by kbknapp
Sunday Jul 22, 2018 at 02:49 GMT


Closing for now to get 3.x out the door. We can re-address after release if needed.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by CreepySkeleton
Sunday Feb 02, 2020 at 02:18 GMT


I'm leaving it closed for the time being

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant