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
220
votes
2 answers

argparse module How to add option without any argument?

I have created a script using argparse. The script needs to take a configuration file name as an option, and user can specify whether they need to proceed totally the script or only simulate it. The args to be passed: ./script -f config_file -s or…
philippe
  • 2,215
  • 2
  • 15
  • 5
217
votes
2 answers

argparse: identify which subparser was used

I think this must be easy but I do not get it. Assume I have the following arparse parser: import argparse parser = argparse.ArgumentParser( version='pyargparsetest 1.0' ) subparsers = parser.add_subparsers(help='commands') # all all_parser =…
user1062880
  • 2,181
  • 2
  • 13
  • 5
212
votes
6 answers

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

I have this code so far: import argparse parser = argparse.ArgumentParser() parser.add_argument("-g", "--games", type=int, default=162, help="The number of games to simulate") args = parser.parse_args() It does not make sense to…
jgritty
  • 11,660
  • 3
  • 38
  • 60
194
votes
1 answer

Allowing specific values for an Argparse argument

Is it possible to require that an argparse argument be one of a few preset values? My current approach would be to examine the argument manually and if it's not one of the allowed values call print_help() and exit. Here's the current…
Moshe
  • 9,283
  • 4
  • 29
  • 38
178
votes
3 answers

Get selected subcommand with argparse

When I use subcommands with python argparse, I can get the selected arguments. parser = argparse.ArgumentParser() parser.add_argument('-g', '--global') subparsers = parser.add_subparsers() foo_parser =…
Adam Schmideg
  • 10,590
  • 10
  • 53
  • 83
176
votes
11 answers

Check if argparse optional argument is set or not

I would like to check whether an optional argparse argument has been set by the user or not. Can I safely check using isset? Something like this: if(isset(args.myArg)): #do something else: #do something else Does this work the same for…
Madeleine P. Vincent
  • 3,361
  • 5
  • 25
  • 30
165
votes
5 answers

argparse store false if unspecified

parser.add_argument('-auto', action='store_true') How can I store false if -auto is unspecified? I can faintly remember that this way, it stores None if unspecified
siamii
  • 23,374
  • 28
  • 93
  • 143
163
votes
6 answers

Argparse: Required argument 'y' if 'x' is present

I have a requirement as follows: ./xyifier --prox --lport lport --rport rport for the argument prox , I use action='store_true' to check if it is present or not. I do not require any of the arguments. But, if --prox is set I require rport and…
asudhak
  • 2,929
  • 4
  • 22
  • 27
151
votes
3 answers

Specify date format for Python argparse input arguments

I have a Python script that requires some command line inputs and I am using argparse for parsing them. I found the documentation a bit confusing and couldn't find a way to check for a format in the input parameters. What I mean by checking format…
Sohaib
  • 4,556
  • 8
  • 40
  • 68
149
votes
2 answers

Creating hidden arguments with Python argparse

Is it possible to add an Argument to an python argparse.ArgumentParser without it showing up in the usage or help (script.py --help)?
Peter Smit
  • 27,696
  • 33
  • 111
  • 170
140
votes
4 answers

Using the same option multiple times in Python's argparse

I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this ./my_script.py \ -i input1_url input1_name input1_other_var \ -i input2_url input2_name input2_other_var \ -i input3_url…
John Allard
  • 3,564
  • 5
  • 23
  • 42
119
votes
4 answers

Python argparse mutual exclusive group

What I need is: pro [-a xxx | [-b yyy -c zzz]] I tried this but does not work. Could someone help me out? group= parser.add_argument_group('Model 2') group_ex = group.add_mutually_exclusive_group() group_ex.add_argument("-a", type=str, action =…
Sean
  • 4,267
  • 10
  • 36
  • 50
118
votes
13 answers

Python argparse: Make at least one argument required

I've been using argparse for a Python program that can -process, -upload or both: parser = argparse.ArgumentParser(description='Log archiver arguments.') parser.add_argument('-process', action='store_true') parser.add_argument('-upload', …
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
116
votes
6 answers

Support for Enum arguments in argparse

Is there a better way of supporting Enums as types of argparse arguments than this pattern? class SomeEnum(Enum): ONE = 1 TWO = 2 parser.add_argument('some_val', type=str, default='one', choices=[i.name.lower() for i in…
Andrzej Pronobis
  • 33,828
  • 17
  • 76
  • 92
113
votes
2 answers

What does metavar and action mean in argparse in Python?

I am reading through argparse module. I got stuck as what to metavar and action means >>> parser.add_argument('integers', metavar='N', type=int, nargs='+', ... help='an integer for the accumulator') >>>…
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122