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
22
votes
6 answers

argparse argument order

I have a little problem. I use argparse to parse my arguments, and it's working very well. To have the args, I do : p_args = parser.parse_args(argv) args = dict(p_args._get_kwargs()) But the problem with p_args is that I don't know how to get these…
flopit
  • 247
  • 1
  • 2
  • 6
22
votes
3 answers

Why does argparse not accept "--" as argument?

My script takes -d, --delimiter as argument: parser.add_argument('-d', '--delimiter') but when I pass it -- as delimiter, it is empty script.py --delimiter='--' I know -- is special in argument/parameter parsing, but I am using it in the form…
400 the Cat
  • 266
  • 3
  • 23
22
votes
3 answers

argparse: flatten the result of action='append'

I'd like to make a script that supports an argument list of the form ./myscript --env ONE=1,TWO=2 --env THREE=3 Here's my attempt: import argparse parser = argparse.ArgumentParser() parser.add_argument( '--env', type=lambda s:…
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
22
votes
3 answers

Unpacking arguments from argparse

I've been writing some command line python programs and using argparse to do it. I've been structuring my code somewhat as follows. def main(arg1, arg2): # magic pass if __name__ == '__main__': parser = argparse.ArgumentParser() …
Ben Hoff
  • 1,058
  • 1
  • 11
  • 24
22
votes
2 answers

Argparse - do not catch positional arguments with `nargs`.

I am trying to write a function wo which you can parse a variable amount of arguments via argparse - I know I can do this via nargs="+". Sadly, the way argparse help works (and the way people generally write arguments in the CLI) puts the positional…
TheChymera
  • 17,004
  • 14
  • 56
  • 86
21
votes
7 answers

argparse optional subparser (for --version)

I have the following code (using Python 2.7): # shared command line options, like --version or --verbose parser_shared = argparse.ArgumentParser(add_help=False) parser_shared.add_argument('--version', action='store_true') # the main parser,…
miku
  • 181,842
  • 47
  • 306
  • 310
21
votes
2 answers

Argparse - How to Specify a Default Subcommand

I am using the argparse package of Python 2.7 to write some option-parsing logic for a command-line tool. The tool should accept one of the following arguments: "ON": Turn a function on. "OFF": Turn a function off. [No arguments provided]: Echo the…
Will Shipley
  • 217
  • 1
  • 3
  • 8
21
votes
4 answers

Using argparse to parse arguments of form "arg= val"

I want to use argparse to parse command lines of form "arg=val" For example, the usage would be: script.py conf_dir=/tmp/good_conf To achieve it, I am doing this: desc = "details" parser = argparse.ArgumentParser(description=desc,…
21
votes
2 answers

python argparse - pass values WITHOUT command line

I think I'm not understanding something basic about python's argparse. I am trying to use the Google YouTube API for python script, but I am not understanding how to pass values to the script without using the command line. For example, here is the…
sean
  • 3,484
  • 5
  • 27
  • 45
21
votes
3 answers

Python argparse conditional requirements

How do I set up argparse as follows: if -2 is on the command line, no other arguments are required if -2 is not on the command line, -3 and -4 arguments are required For example, -2 [good] -3 a -4 b [good] -3 a [not good, -4 required] -2 -5 c…
foosion
  • 7,619
  • 25
  • 65
  • 102
21
votes
2 answers

argparse: setting optional argument with value of mandatory argument

With Python's argparse, I would like to add an optional argument that, if not given, gets the value of another (mandatory) argument. parser.add_argument('filename', metavar = 'FILE', type = str, …
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
20
votes
5 answers

python argparse - optional append argument with choices

I have a script where I ask the user for a list of pre-defined actions to perform. I also want the ability to assume a particular list of actions when the user doesn't define anything. however, it seems like trying to do both of these together is…
Steve
  • 534
  • 1
  • 4
  • 13
20
votes
3 answers

How to split a string into command line arguments like the shell in python?

I have command line arguments in a string and I need to split it to feed to argparse.ArgumentParser.parse_args. I see that the documentation uses string.split() plentifully. However in complex cases, this does not work, such as --foo "spaces in…
P-Gn
  • 23,115
  • 9
  • 87
  • 104
20
votes
3 answers

Difference between single dash and double dash in argparse

Does anyone know what is the main difference between specifying an argparse agument with a single dash -r and a double dash --r? I came across this Julia language argparse which classifies them as short and long but doesn't say exactly why you would…
Moataz Elmasry
  • 542
  • 1
  • 5
  • 18
20
votes
1 answer

python argparse: arg with no flag

i've the following code: parser.add_argument('file', help='file to test') parser.add_argument('-revs', help='range of versions', nargs='+', default=False) Is there a way to not use the flag -revs when use it, like this: ./somescript.py…
Marco Herrarte
  • 1,540
  • 5
  • 21
  • 42