Python Argparse – How can I add text to the default help message?

You can quite do it using epilog. Here is an example below: import argparse import textwrap parser = argparse.ArgumentParser( prog=’ProgramName’, formatter_class=argparse.RawDescriptionHelpFormatter, epilog=textwrap.dedent(”’\ additional information: I have indented it exactly the way I want it ”’)) parser.add_argument(‘–foo’, nargs=”?”, help=’foo help’) parser.add_argument(‘bar’, nargs=”+”, help=’bar help’) parser.print_help() Result : usage: ProgramName [-h] [–foo [FOO]] bar [bar …] positional … Read more

Multiple positional arguments with Python and argparse

You can’t interleave the switches (i.e. -a and -b) with the positional arguments (i.e. fileone, filetwo and filethree) in this way. The switches must appear before or after the positional arguments, not in-between. Also, in order to have multiple positional arguments, you need to specify the nargs parameter to add_argument. For example: parser.add_argument(‘input’, nargs=”+”) This … Read more

‘required’ is an invalid argument for positionals in python command

You created a positional argument (no — option in front of the name). Positional arguments are always required. You can’t use required=True for such options, just drop the required. Drop the default too; a required argument can’t have a default value (it would never be used anyway): parser.add_argument(‘archive’, help=’Make import archive events’ ) If you … Read more

Multiple files for one argument in argparse Python 2.7

If your goal is to read one or more readable files, you can try this: parser.add_argument(‘file’, type=argparse.FileType(‘r’), nargs=”+”) nargs=”+” gathers all command line arguments into a list. There must also be one or more arguments or an error message will be generated. type=argparse.FileType(‘r’) tries to open each argument as a file for reading. It will … Read more

path to a directory as argparse argument

One can ensure the path is a valid directory with something like: import argparse, os def dir_path(string): if os.path.isdir(string): return string else: raise NotADirectoryError(string) parser = argparse.ArgumentParser() parser.add_argument(‘–path’, type=dir_path) # … Check is possible for files using os.path.isfile() instead, or any of the two using os.path.exists().

type=dict in argparse.add_argument()

Necroing this: json.loads works here, too. It doesn’t seem too dirty. import json import argparse test=”{“name”: “img.png”,”voids”: “#00ff00ff”,”0″: “#ff00ff00″,”100%”: “#f80654ff”}” parser = argparse.ArgumentParser() parser.add_argument(‘-i’, ‘–input’, type=json.loads) args = parser.parse_args([‘-i’, test]) print(args.input) Returns: {u’0′: u’#ff00ff00′, u’100%’: u’#f80654ff’, u’voids’: u’#00ff00ff’, u’name’: u’img.png’}

Argparse optional boolean [duplicate]

Are you sure you need that pattern? –foo and –foo <value>, together, for a boolean switch, is not a common pattern to use. As for your issue, remember that the command line value is a string and, type=bool means that you want bool(entered-string-value) to be applied. For –foo False that means bool(“False”), producing True; all … Read more

Using the same option multiple times in Python’s argparse

Here’s a parser that handles a repeated 2 argument optional – with names defined in the metavar: parser=argparse.ArgumentParser() parser.add_argument(‘-i’,’–input’,action=’append’,nargs=2, metavar=(‘url’,’name’),help=’help:’) In [295]: parser.print_help() usage: ipython2.7 [-h] [-i url name] optional arguments: -h, –help show this help message and exit -i url name, –input url name help: In [296]: parser.parse_args(‘-i one two -i three four’.split()) Out[296]: … Read more

How can I constrain a value parsed with argparse (for example, restrict an integer to positive values)?

This should be possible utilizing type. You’ll still need to define an actual method that decides this for you: def check_positive(value): ivalue = int(value) if ivalue <= 0: raise argparse.ArgumentTypeError(“%s is an invalid positive int value” % value) return ivalue parser = argparse.ArgumentParser(…) parser.add_argument(‘foo’, type=check_positive) This is basically just an adapted example from the perfect_square … Read more

Control formatting of the argparse help argument list?

You could supply formatter_class argument: parser = argparse.ArgumentParser(prog=’tool’, formatter_class=lambda prog: argparse.HelpFormatter(prog,max_help_position=27)) args = [(‘-u’, ‘–upf’, ‘ref. upf’, dict(required=’True’)), (‘-s’, ‘–skew’, ‘ref. skew’, {}), (‘-m’, ‘–model’, ‘ref. model’, {})] for args1, args2, desc, options in args: parser.add_argument(args1, args2, help=desc, **options) parser.print_help() Note: Implementation of argparse.HelpFormatter is private only the name is public. Therefore the code might … Read more