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
41
votes
1 answer

argparse "compulsory" optional arguments

Python's argparse module has what are called 'optional' arguments. All arguments whose name starts with - or -- are optional by default. Typically, compulsory arguments are positional, and hence when running the program, they are not explicitly…
ArjunShankar
  • 23,020
  • 5
  • 61
  • 83
41
votes
2 answers

argparse default option based on another option

Suppose I have an argparse python script: import argparse parser = argparse.ArgumentParser() parser.add_argument("--foo", required=True) Now I want to add another option --bar, which would default to appending "_BAR" to whatever was specified…
paroxyzm
  • 1,422
  • 2
  • 13
  • 29
40
votes
9 answers

In Python argparse, is it possible to have paired --no-something/--something arguments?

I'm writing a program in which I would like to have arguments like this: --[no-]foo Do (or do not) foo. Default is do. Is there a way to get argparse to do this for me?
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
40
votes
2 answers

Is it possible to use argparse to capture an arbitrary set of optional arguments?

Is it possible to use argparse to capture an arbitrary set of optional arguments? For example both the following should be accepted as inputs: python script.py required_arg1 --var1 value1 --var2 value2 --var3 value3 python script.py required_arg1…
saladi
  • 3,103
  • 6
  • 36
  • 61
40
votes
4 answers

Arguments that are dependent on other arguments with Argparse

I want to accomplish something like this: -LoadFiles -SourceFile "" -DestPath "" -SourceFolder "" -DestPath "" -GenericOperation -SpecificOperation -Arga "" -Argb "" -OtherOperation -Argc "" -Argb "" -Argc "" A user should be able…
user3715648
  • 1,498
  • 3
  • 16
  • 25
40
votes
5 answers

How can I get argparse in Python 2.6?

I have some Python 2.7 code written that uses the argparse module. Now I need to run it on a Python 2.6 machine and it won't work since argparse was added in 2.7. Is there anyway I can get argparse in 2.6? I would like to avoid rewriting the code,…
Bitwise
  • 7,577
  • 6
  • 33
  • 50
39
votes
1 answer

Python argparse: Is there a way to specify a range in nargs?

I have an optional argument that supports a list of arguments itself. I mean, it should support: -f 1 2 -f 1 2 3 but not: -f 1 -f 1 2 3 4 Is there a way to force this within argparse ? Now I'm using nargs="*", and then checking the list…
Doppelganger
  • 20,114
  • 8
  • 31
  • 29
39
votes
4 answers

argparse with required subcommands

With python's argparse, how do I make a subcommand a required argument? I want to do this because I want argparse to error out if a subcommand is not specified. I override the error method to print help instead. I have 3-deep nested subcommands, so…
PonyEars
  • 2,144
  • 4
  • 25
  • 30
39
votes
6 answers

Passing integer lists to python

I want to pass 2 lists of integers as input to a python program. For e.g, (from command line) python test.py --a 1 2 3 4 5 -b 1 2 The integers in this list can range from 1-50, List 2 is subset of List1. Any help/suggestions ? Is argparse the…
Swati
  • 405
  • 1
  • 4
  • 9
39
votes
3 answers

python argparse - either both optional arguments or else neither one

I have a program that uses a default name and password. I'm using argparse to allow the user to specify command line options, and I would like to enable the user to provide the program with a different name and password to use. So I have the…
tadasajon
  • 14,276
  • 29
  • 92
  • 144
38
votes
7 answers

Python Argparse: Issue with optional arguments which are negative numbers

I'm having a small issue with argparse. I have an option xlim which is the xrange of a plot. I want to be able to pass numbers like -2e-5. However this does not work - argparse interprets this is a positional argument. If I do -0.00002 it works:…
Ger
  • 958
  • 1
  • 8
  • 11
38
votes
8 answers

argparse subparser monolithic help output

My argparse has only 3 flags (store_true) on the top level, everything else is handled through subparsers. When I run myprog.py --help, the output shows a list of all subcommands like normal, {sub1, sub2, sub3, sub4, ...}. So, the default is working…
user2097818
  • 1,821
  • 3
  • 16
  • 34
38
votes
2 answers

ArgumentParser epilog and description formatting in conjunction with ArgumentDefaultsHelpFormatter

I'm using argparse to take in command line input and also to produce help text. I want to use ArgumentDefaultsHelpFormatter as the formatter_class, however this prevents me from also using RawDescriptionHelpFormatter which would allow me to add…
Spycho
  • 7,698
  • 3
  • 34
  • 55
38
votes
2 answers

how to make argument optional in python argparse

I would like to make these invocations of myprog work, and no others. $ python3 myprog.py -i infile -o outfile $ python3 myprog.py -o outfile $ python3 myprog.py -o $ python3 myprog.py In particular I want to make it illegal to specify the infile…
user1416227
  • 636
  • 2
  • 11
  • 20
36
votes
1 answer

argparse add example usage

I use argparse to deal with input parameters, and it outputs the following with parser.print_help(): optional arguments: -h, --help show this help message and exit -t TEMPLATES, --templates TEMPLATES template names…
linpingta
  • 2,324
  • 2
  • 18
  • 36