'Python argparse require one or the other arguments
I am trying to set the following conditions in argparse:
- If
--startdate
then require--enddate
, - If
--month
then require--year
- If
--week_num
then require--year
Only require one of the above bullet items, i.e, either 1 or 2 or 3, and if more than one is specified then error out. So basically the user can specify the start and end date, or the month and year or the week and year.
For example:
$ myprog --startdate 01-01-2022 --enddate 01-07-2022
OR
$ myprog --month 1 --year 2022
OR
$ myprog --week 1 --year 2022
If a user does something like this, then there should be an error:
$ myprog --week 1 --enddate 01-07-2022
I am not sure how to accomplish this using argparse. I looked into mutually exclusive groups, but that seems to not allow for pair.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|