python - Can argparse parse arguments conditionally? -
I use argparse in Python 2.7 to parse command line arguments. Will some predefined conditional parsing handle the following examples?
- If
-x1is specified, then-x2 -x3should be specified, but-x4Is optional. - If specified
-x5then-x4should be specified, but-x2is optional.
Is there a way to do this without parser.parse_args ()
There is a Python bug issue called 'Python Inclusive' groups on argparse Based on the mutually exclusive group approach
proposed combination of rules like yours before you exit the proposed main idea
parse_args . At this point there is a list (or set) of arguments that has been seen. The main challenge of designing a user interface is that it is broad, logical and intuitive. Writing a usage line is also challenging, which reflects the conditions.
But without that patch, I'm scared that you are stuck in your own tests based on your values that you get in the ARGs namespace. If you pick up your mistake properly then it should not be difficult.
Another possibility is to use sub-birds to give you the status of the -x1 , -x5 in the situational options, x1 And x5 , and of course they will be mutually exclusive.
A simple test example:
If there is no argsoption1 and no args.option2 is none: parser. Panic ('at least one option 1 and option 2 is required')
Comments
Post a Comment