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

python argparse: unrecognized arguments

When I run parsePlotSens.py -s bw hehe, it says that hehe is an unrecognized argument. However, if I run parsePlotSens.py hehe -s bw, it's OK. Ideally, I would like it work for both cases. Any tips? The following is my code: if __name__ ==…
Yan Zhu
  • 4,036
  • 3
  • 21
  • 37
76
votes
4 answers

File as command line argument for argparse - error message if argument is not valid

I am currently using argparse like this: import argparse from argparse import ArgumentParser parser = ArgumentParser(description="ikjMatrix multiplication") parser.add_argument("-i", dest="filename", required=True, help="input file with two…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
75
votes
2 answers

Using 'argparse.ArgumentError' in Python

I'd like to use the ArgumentError exception in the argparse module in Python, but I can't figure out how to use it. The signature says that it should be called as ArgumentError(argument, message), but I can't figure out what argument should be. I…
asmeurer
  • 86,894
  • 26
  • 169
  • 240
75
votes
4 answers

Getting the remaining arguments in argparse

I want to get all the remaining unused arguments at once. How do I do it? parser.add_argument('-i', action='store', dest='i', default='i.log') parser.add_argument('-o', action='store', dest='o', default='o.log')
ggoha
  • 1,996
  • 3
  • 23
  • 31
75
votes
8 answers

Argparse: Check if any arguments have been passed

My script should start a demo mode, when the no parameters are given. I tried this: args = parser.parse_args() if len(args) == 0: run_demo() else: # evaluate args Which gives a *** TypeError: object of type 'Namespace' has no len() as args…
Framester
  • 33,341
  • 51
  • 130
  • 192
73
votes
8 answers

Python Argparse conditionally required arguments

I have done as much research as possible but I haven't found the best way to make certain cmdline arguments necessary only under certain conditions, in this case only if other arguments have been given. Here's what I want to do at a very basic…
DJMcCarthy12
  • 3,819
  • 8
  • 28
  • 34
70
votes
1 answer

Accepting a dictionary as an argument with argparse and python

I'm trying to accept an argument of type=dict with argparse but no matter the input it gives an error of invalid dict value. #!/usr/bin/env python import argparse MYDICT = {'key': 'value'} parser =…
user2745896
  • 753
  • 1
  • 6
  • 7
68
votes
8 answers

Python argparse and controlling/overriding the exit status code

Apart from tinkering with the argparse source, is there any way to control the exit status code should there be a problem when parse_args() is called, for example, a missing required switch?
Kev
  • 118,037
  • 53
  • 300
  • 385
66
votes
3 answers

How to pass and parse a list of strings from command line with argparse.ArgumentParser in Python?

I want to pass a list of names into my program written in Python from console. For instance, I would like to use a way similar to this (I know it shouldn't work because of bash): $ python myprog.py -n name1 name2 So, I tried this code: #…
Fomalhaut
  • 8,590
  • 8
  • 51
  • 95
65
votes
7 answers

Python argparse: Lots of choices results in ugly help output

I have this code which I am generally pleased with: import argparse servers = [ "ApaServer", "BananServer", "GulServer", "SolServer", "RymdServer", "SkeppServer", "HavsServer", "PiratServer", "SvartServer", "NattServer", "SovServer"…
Deleted
  • 1,351
  • 3
  • 14
  • 18
65
votes
2 answers

How to make a short and long version of a required argument using Python Argparse?

I want to specify a required argument called inputdir but I also would like to have a shorthand version of it called i. I don't see a concise solution to do this without making both optional arguments and then doing my own check. Is there a…
user3621633
  • 1,681
  • 3
  • 32
  • 46
65
votes
7 answers

Handle spaces in argparse input

Using python and argparse, the user could input a file name with -d as the flag. parser.add_argument("-d", "--dmp", default=None) However, this failed when the path included spaces. E.g. -d C:\SMTHNG\Name with spaces\MORE\file.csv NOTE: the…
ofer.sheffer
  • 5,417
  • 7
  • 25
  • 26
64
votes
2 answers

Python argparse and bash completion

I would like to get auto-completion on my python scripts also in the arguments. I had never really understood how the bash_completion worked (for arguments), but after I digged in I understood that: it uses "complete" to bind a completing function…
andrea_crotti
  • 3,004
  • 2
  • 28
  • 33
64
votes
3 answers

directory path types with argparse

My python script needs to read files from a directory passed on the command line. I have defined a readable_dir type as below to be used with argparse for validating that the directory passed on the command line is existent and…
iruvar
  • 22,736
  • 7
  • 53
  • 82
63
votes
9 answers

Python: Typehints for argparse.Namespace objects

Is there a way to have Python static analyzers (e.g. in PyCharm, other IDEs) pick up on Typehints on argparse.Namespace objects? Example: parser = argparse.ArgumentParser() parser.add_argument('--somearg') parsed =…
Billy
  • 5,179
  • 2
  • 27
  • 53