I'd like to have a program that takes a --action=
flag, where the valid choices are dump
and upload
, with upload
being the default. If (and only if) dump
is selected, I'd like there to also be a --dump-format=
option. Is there a way to express this using argparse, or do I need to just accept all the arguments and do the logic myself.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You could use
parser.error
:Another way to approach the problem is by using subcommands (a'la git) with "action" as the first argument:
The argparse module offers a way to do this without implementing your own requiredness checks. The example below uses "subparsers" or "sub commands". I've implemented a subparser for "dump" and one for "format".
That looks like this on the command line:
More info: http://docs.python.org/dev/library/argparse.html#argparse.ArgumentParser.add_subparsers