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
1046
votes
27 answers

Parsing boolean values with argparse

I would like to use argparse to parse boolean command-line arguments written as "--foo True" or "--foo False". For example: my_program --my_boolean_flag False However, the following test code does not do what I would like: import argparse parser =…
SuperElectric
  • 17,548
  • 10
  • 52
  • 69
883
votes
5 answers

Argparse optional positional arguments?

I have a script which is meant to be used like this: usage: installer.py dir [-h] [-v] dir is a positional argument which is defined like this: parser.add_argument('dir', default=os.getcwd()) I want the dir to be optional: when it's not specified…
Waldo Bronchart
  • 10,152
  • 4
  • 23
  • 29
765
votes
13 answers

How can I pass a list as a command-line argument with argparse?

I am trying to pass a list as an argument to a command line program. Is there an argparse option to pass a list as option? parser.add_argument('-l', '--list', type=list, action='store', dest='list', …
carte blanche
  • 10,796
  • 14
  • 46
  • 65
669
votes
17 answers

Simple argparse example wanted: 1 argument, 3 results

The documentation for the argparse python module, while excellent I'm sure, is too much for my tiny beginner brain to grasp right now. I don't need to do math on the command line or meddle with formatting lines on the screen or change option…
matt wilkie
  • 17,268
  • 24
  • 80
  • 115
501
votes
5 answers

Python argparse command line flags without arguments

How do I add an optional flag to my command line args? eg. so I can write python myprog.py or python myprog.py -w I tried parser.add_argument('-w') But I just get an error message saying Usage [-w W] error: argument -w: expected one…
interstar
  • 26,048
  • 36
  • 112
  • 180
482
votes
15 answers

How to insert newlines on argparse help text?

I'm using argparse in Python 2.7 for parsing input options. One of my options is a multiple choice. I want to make a list in its help text, e.g. from argparse import ArgumentParser parser =…
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
442
votes
4 answers

Argparse: Way to include default values in '--help'?

Suppose I have the following argparse snippet: diags.cmdln_parser.add_argument( '--scan-time', action = 'store', nargs = '?', type = int, default = 5, …
JS.
  • 14,781
  • 13
  • 63
  • 75
356
votes
7 answers

Argparse: Required arguments listed under "optional arguments"?

I use the following simple code to parse some arguments; note that one of them is required. Unfortunately, when the user runs the script without providing the argument, the displayed usage/help text does not indicate that there is a non-optional…
mort
  • 12,988
  • 14
  • 52
  • 97
328
votes
5 answers

Why use argparse rather than optparse?

I noticed that the Python 2.7 documentation includes yet another command-line parsing module. In addition to getopt and optparse we now have argparse. Why has yet another command-line parsing module been created? Why should I use it instead of…
fmark
  • 57,259
  • 27
  • 100
  • 107
322
votes
18 answers

Display help message with Python argparse when script is called without any arguments

Assume I have a program that uses argparse to process command line arguments/options. The following will print the 'help' message: ./myprogram -h or: ./myprogram --help But, if I run the script without any arguments whatsoever, it doesn't do…
musashiXXX
  • 4,192
  • 4
  • 22
  • 24
309
votes
2 answers

Python argparse: default value or specified value

I would like to have a optional argument that will default to a value if only the flag is present with no value specified, but store a user-specified value instead of the default if the user specifies a value. Is there already an action available…
Rob
  • 7,377
  • 7
  • 36
  • 38
287
votes
3 answers

Python argparse ignore unrecognised arguments

Optparse, the old version just ignores all unrecognised arguments and carries on. In most situations, this isn't ideal and was changed in argparse. But there are a few situations where you want to ignore any unrecognised arguments and parse the…
joedborg
  • 17,651
  • 32
  • 84
  • 118
268
votes
2 answers

Require either of two arguments using argparse

Given: import argparse pa = argparse.ArgumentParser() pa.add_argument('--foo') pa.add_argument('--bar') print pa.parse_args('--foo 1'.split()) how do I make at least one of "foo, bar" mandatory: --foo x, --bar y and --foo x --bar y are fine make…
georg
  • 211,518
  • 52
  • 313
  • 390
259
votes
11 answers

How do you write tests for the argparse portion of a python module?

I have a Python module that uses the argparse library. How do I write tests for that section of the code base?
pydanny
  • 7,954
  • 6
  • 34
  • 42
242
votes
6 answers

Having options in argparse with a dash

I want to have some options in argparse module such as --pm-export however when I try to use it like args.pm-export I get the error that there is not attribute pm. How can I get around this issue? Is it possible to have - in command line options?
Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169
1
2 3
99 100