[QUESTION] How to handle mutually exclusive options
See original GitHub issueFirst check
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the Typer documentation, with the integrated search.
- I already searched in Google “How to X in Typer” and didn’t find any information.
- I already searched in Google “How to X in Click” and didn’t find any information.
Description
I often find myself making mutually exclusive options. I’m used to argparse, which has nice support for that. What is the best way to do this in Typer?
I.e. how to (best) achieve something like this argparse code:
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("--opt-A, action="store_true")
group.add_argument("--opt-B, action="store_true")
I found several mentions of ways to do it with Click, but none that were “built-in”, nor I’m I clear on how I’d use that with Typer.
Any help would be much appreciated. I really like the feel of Typer!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:33
- Comments:17 (3 by maintainers)
Top Results From Across the Web
Mutually Exclusive & Collectively Exhaustive Categories - Cvent
First, the categories (response options) must be mutually exclusive, which means they do not overlap with one another.
Read more >Mutually Exclusive: What It Means, With Examples
If considering mutually exclusive options, a company must weigh the opportunity cost, or what it would be giving up by choosing each option....
Read more >mutually exclusive options with argp in C - Stack Overflow
Options -a and -b are mutually exclusive because one cannot specify them together, i.e. specifying -a excludes the use of -b . The...
Read more >Should I use mutually exclusive options on my command line ...
POSIX just says (regarding mutually exclusive command line options in POSIX utilities):. The use of conflicting mutually-exclusive arguments ...
Read more >Solved Question 5 (15 Marks) Two mutually exclusive options
Question : Question 5 (15 Marks) Two mutually exclusive options are considered for a bus terminal undertaken by the city government. Option A...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey my fellow Pythonistas, I often comeback to this issue from time to time because it’s still a problem for me so I took @daddycocoaman answer and came up with this:
Using the function
MutuallyExclusiveGroupI return another function within a closure where there is state, a set calledgroupas typer invokes the callbacks the logic will check if more than one option I have added theexclusivity_callbackwas called in the command line and raises an exception.If you need to ensure at least one of the options are passed to the command line then manually check it in the body of your function for the command like so
Just adding another “I’d like to see this too” comment.