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 -x1 is specified, then -x2 -x3 should be specified, but -x4 Is optional.
  • If specified -x5 then -x4 should be specified, but -x2 is optional.

Is there a way to do this without parser.parse_args ()

without writing conditions?

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

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -