argparse

Handle invalid arguments with argparse in Python

半城伤御伤魂 提交于 2020-01-13 10:34:26
问题 I am using argparse to parse command line arguments and by default on receiving invalid arguments it prints help message and exit. Is it possible to customize the behavior of argparse when it receives invalid arguments? Generally I want to catch all invalid arguments and do stuff with them. I am looking for something like: parser = argparse.ArgumentParser() # add some arguments here try: parser.parse_args() except InvalidArgvsError, iae: print "handle this invalid argument '{arg}' my way!"

Python Optional Argument Pair

北慕城南 提交于 2020-01-13 09:03:57
问题 I'm using the argparse module to get two optional command line arguments: parser.add_argument('start_date', nargs='?', metavar='START DATE', help='start date in YYYY-MM-DD') parser.add_argument('end_date', nargs='?', metavar='END DATE', help='end date in YYYY-MM-DD') which gives > python test_arg.py -h usage: test_arg.py [-h] [START DATE] [END DATE] However I want the pair of optional arguments ( START DATE and END DATE ), if provided at all, to be provided together. Something like along this

Python Optional Argument Pair

╄→гoц情女王★ 提交于 2020-01-13 09:02:53
问题 I'm using the argparse module to get two optional command line arguments: parser.add_argument('start_date', nargs='?', metavar='START DATE', help='start date in YYYY-MM-DD') parser.add_argument('end_date', nargs='?', metavar='END DATE', help='end date in YYYY-MM-DD') which gives > python test_arg.py -h usage: test_arg.py [-h] [START DATE] [END DATE] However I want the pair of optional arguments ( START DATE and END DATE ), if provided at all, to be provided together. Something like along this

Can two Python argparse objects be combined?

半世苍凉 提交于 2020-01-13 08:39:05
问题 I have an object A which contains parserA - an argparse.ArgumentParser object There is also object B which contains parserB - another argparse.ArgumentParser Object A contains an instance of object B, however object B's arguments now need to be parsed by the parser in object A (since A is the one being called from the command line with the arguments, not B) Is there a way to write in Python object A: parserA += B.parserB? 回答1: argparse was developed around objects. Other than a few constants

Python - Difference between docopt and argparse

為{幸葍}努か 提交于 2020-01-12 12:09:56
问题 I have to write a command-line interface and I've seen I can use docopt and argparse . I would like to know what are the main differences between the two so that I can make an enlightened choice. Please stick to the facts. I don't want Wow. docopt. So beautiful. Very useful. 回答1: Docopt parses a doc string, whereas argparse constructs its parsing by creating an object instance and adding behaviour to it by function calls. Example for argparse: parser = argparse.ArgumentParser() parser.add

Chaining in a command line several tranformations with options

让人想犯罪 __ 提交于 2020-01-11 13:12:10
问题 My command line utility should accept several filters attached to each other (similar to Unix pipeline). Each filter has a number of options. For example chain filter currently has the following options: -t NAMESPACE, --target NAMESPACE target namespace(s) -s {precedence,doc}, --next-script {precedence,doc} "next script" algorithm ("precedence" is not supported) -n {ignore,remove,error}, --not-in-target {ignore,remove,error} what if a result is not in target NS -u URL, --universal-precedence

argparse subparser --help output doesn't show the subparser's description

独自空忆成欢 提交于 2020-01-11 11:28:43
问题 If I create a subparser with a specific help string, this string is not displayed when the user runs myprog command --help : parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(help="sub-command help") parser_command = subparsers.add_parser("command", help="Issue a command") parser.parse_args() The top-level help shows this command subcommand with the description "Issue a command" alongside: $ python prog.py --help usage: prog.py [-h] {command} ... positional arguments:

Python argparse: type inconsistencies when combining 'choices', 'nargs' and 'default'

最后都变了- 提交于 2020-01-11 01:26:27
问题 I have the following python program: #!/usr/bin/env python import argparse parser = argparse.ArgumentParser() parser.add_argument('arg', choices=['foo', 'bar', 'baz'], default='foo', nargs='*') args = parser.parse_args() print(args) If I invoke the program like this: ./prog.py the output is Namespace(arg='foo') But if I invoke the program with foo as an argument: ./prog.py foo the output is Namespace(arg=['foo']) Question How can I get arg 's default value to become a list ? I've tried I've

Getting output from Python script in Python tests

情到浓时终转凉″ 提交于 2020-01-07 05:27:06
问题 I've got a simple python script in file 'bin/test': #!/usr/bin/env python import argparse PROGRAM_NAME = "name" PROGRAM_VERSION = "0.0.1" PROGRAM_DESCRIPTION = "desc" parser = argparse.ArgumentParser(prog=PROGRAM_NAME, description=PROGRAM_DESCRIPTION) parser.add_argument('--version', action='version', version='%(prog)s ' + PROGRAM_VERSION) args = parser.parse_args() When I run it with the --version param, or --help , it prints everything OK: $ bin/test --version name 0.0.1 $ bin/test --help

Open compressed file directly with argparse

走远了吗. 提交于 2020-01-07 03:06:28
问题 Can I open a gzip file directly with argparse by changing the type=argparse.FileType() to some gzip type? It's not in the docs, so I'm not sure if argparse even suppoets compressed file types... 回答1: First, the type parameter is a function or other callable that converts a string into something else. That's all. argparse.FileType is factory class that ends up doing something close to: def filetype(astring): mode = 'r' # or 'w' etc. try: f = open(astring, mode) except IOError: raise argparse