Questions tagged [argparse]

A Python module for implementing command-line interfaces

argparse is a module for implementing command-line interfaces.

From the module documentation:

The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

argparse was added to the stdlib in 2.7/3.2, deprecating .

Resources:

3535 questions
112
votes
6 answers

How do I create a Python namespace (argparse.parse_args value)?

To interactively test my python script, I would like to create a Namespace object, similar to what would be returned by argparse.parse_args(). The obvious way, >>> import argparse >>> parser = argparse.ArgumentParser() >>>…
sds
  • 58,617
  • 29
  • 161
  • 278
108
votes
12 answers

How to parse multiple nested sub-commands using python argparse?

I am implementing a command line program which has interface like this: cmd [GLOBAL_OPTIONS] {command [COMMAND_OPTS]} [{command [COMMAND_OPTS]} ...] I have gone through the argparse documentation. I can implement GLOBAL_OPTIONS as optional argument…
Vikas
  • 8,790
  • 4
  • 38
  • 48
104
votes
2 answers

Argparse with required subparser

I'm using Python 3.4, I'm trying to use argparse with subparsers, and I want to have a similar behavior to the one in Python 2.x where if I don't supply a positional argument (to indicate the subparser/subprogram) I'll get a helpful error message. …
ukrutt
  • 2,240
  • 3
  • 20
  • 29
100
votes
4 answers

How to make python argparse mutually exclusive group arguments without prefix?

Python2.7 argparse only accepts optional arguments (prefixed) in mutually exclusive groups: parser = argparse.ArgumentParser(prog='mydaemon') action = parser.add_mutually_exclusive_group(required=True) action.add_argument('--start',…
Carlo Pires
  • 4,606
  • 7
  • 32
  • 32
100
votes
4 answers

Argparse: how to handle variable number of arguments (nargs='*')

I thought that nargs='*' was enough to handle a variable number of arguments. Apparently it's not, and I don't understand the cause of this error. The code: p =…
rubik
  • 8,814
  • 9
  • 58
  • 88
96
votes
4 answers

Case insensitive argparse choices

Is it possible to check argparse choices in case-insensitive manner? import argparse choices = ["win64", "win32"] parser = argparse.ArgumentParser() parser.add_argument("-p", choices=choices) print(parser.parse_args(["-p", "Win32"])) results…
Peter Petrik
  • 9,701
  • 5
  • 41
  • 65
91
votes
6 answers

How to open file using argparse?

I want to open file for reading using argparse. In cmd it must look like: my_program.py /filepath That's my try: parser = argparse.ArgumentParser() parser.add_argument('file', type = file) args = parser.parse_args() This gives me [edit: added later…
nuT707
  • 1,543
  • 4
  • 17
  • 27
90
votes
4 answers

Python argparse - Add argument to multiple subparsers

My script defines one main parser and multiple subparsers. I want to apply the -p argument to some subparsers. So far the code looks like this: parser = argparse.ArgumentParser(prog="myProg") subparsers =…
rahmu
  • 5,708
  • 9
  • 39
  • 57
87
votes
2 answers

How do I parse a date as an argument with argparse?

Im trying to write a python program that I will run at the command line. I'd like the program to take a single input variable. Specifically, I would use a date in the form of 2014-01-28 (yyyy-mm-dd): e.g. python my_script.py 2014-01-28 It seems…
Chase CB
  • 1,561
  • 1
  • 13
  • 20
86
votes
7 answers

How to iterate over arguments

I have such script: import argparse parser = argparse.ArgumentParser( description='Text file conversion.' ) parser.add_argument("inputfile", help="file to process", type=str) parser.add_argument("-o", "--out",…
user3654650
  • 5,283
  • 10
  • 27
  • 28
84
votes
2 answers

python argparse choices with a default choice

I'm trying to use argparse in a Python 3 application where there's an explicit list of choices, but a default if none are specified. The code I have is: parser.add_argument('--list', default='all', choices=['servers', 'storage', 'all'], help='list…
MVanOrder
  • 1,281
  • 1
  • 10
  • 19
83
votes
3 answers

Optional stdin in Python with argparse

I found the very useful syntax parser.add_argument('-i', '--input-file', type=argparse.FileType('r'), default='-') for specifying an input file or using stdin—both of which I want in my program. However, the input file is not always required. If…
Justin Force
  • 6,203
  • 5
  • 29
  • 39
83
votes
7 answers

Can't get argparse to read quoted string with dashes in it?

Is there a way to make argparse recognize anything between two quotes as a single argument? It seems to keep seeing the dashes and assuming that it's the start of a new option I have something like: mainparser = argparse.ArgumentParser() subparsers…
sfendell
  • 5,415
  • 8
  • 25
  • 26
79
votes
1 answer

How to use argparse subparsers correctly?

I've been searching through a lot of the subparser examples on here and in general but can't seem to figure this seemingly simple thing out. I have two var types of which one has constraints so thought subparser was the way to go. e.g. -t allows for…
user1571144
  • 825
  • 1
  • 7
  • 5
78
votes
12 answers

Setting options from environment variables when using argparse

I have a script which has certain options that can either be passed on the command line, or from environment variables. The CLI should take precedence if both are present, and an error occur if neither are set. I could check that the option is…
Russell Heilling
  • 1,829
  • 1
  • 12
  • 10