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
34
votes
9 answers

Python argparse dict arg

I want to receive a dict(str -> str) argument from the command line. Does argparse.ArgumentParser provide it? Or any other library? For the command line: program.py --dict d --key key1 --value val1 --key key2 --value val2 I expect the following…
orion_tvv
  • 1,681
  • 4
  • 17
  • 29
34
votes
5 answers

Using Argparse and Json together

I am a beginner to Python. I wanted to know if Argparse and JSON could be used together. Say, I have variables p,q,r I could add them to argparse as - parser.add_argument('-p','--param1',help='x variable',…
Raj
  • 3,300
  • 8
  • 39
  • 67
33
votes
5 answers

Disable/Remove argument in argparse

Is it possible to remove or disable an argument in argparse, such that it does not show in the help? How? It is easy to add new arguments: parser = argparse.ArgumentParser() parser.add_argument('--arg1', help='Argument…
Bryan P
  • 5,900
  • 5
  • 34
  • 49
32
votes
3 answers

Multiple lines in python argparse help display

I'm using argparse in Python2.7 and I would like to display multiple lines in the help text of an argument. My codes look like this: import argparse parser = argparse.ArgumentParser(description='details', usage='use "%(prog)s --help" for…
Wang Zong'an
  • 1,492
  • 5
  • 24
  • 29
32
votes
2 answers

Python argparse fails to parse hex formatting to int type

I have the following code which attempts to get the DUT VID from the invoked command line: parser = argparse.ArgumentParser(description='A Test', formatter_class=argparse.ArgumentDefaultsHelpFormatter …
devanl
  • 1,232
  • 1
  • 10
  • 20
31
votes
1 answer

Python argparse regex

Is it possible to use a regex for parsing an argument? For example, I want to accept an argument if only it is a 32 length hex (i.e. matches /[a-f0-9A-F]{32}/) I tried p.add_argument('hex', type=str, nargs="[a-f0-9A-F]{32}") without success
Kousha
  • 32,871
  • 51
  • 172
  • 296
31
votes
2 answers

Python argparse - Mutually exclusive group with default if no argument is given

I'm writing a Python script to process a machine-readable file and output a human-readable report on the data contained within. I would like to give the option of outputting the data to stdout (-s) (by default) or to a txt (-t) or csv (-c) file. I…
Alex B.
  • 412
  • 1
  • 4
  • 10
31
votes
4 answers

Python argparse integer condition (>=12)

I need to request that an argument is >= 12 using argparse. I cannot find a way to obtain this result using argparse, it seems there's no way to set rules to a given value but only full sets of accepted values like choices=['rock', 'paper',…
giuspen
  • 1,365
  • 3
  • 20
  • 33
30
votes
4 answers

How can I use python's argparse with a predefined argument string?

I want to use the pythons argparse module to parse my cli parameter string. This works for the parameters a pass from terminal, but not with a given string. import argparse parser = argparse.ArgumentParser(description='Argparse Test…
thorink
  • 704
  • 1
  • 8
  • 21
30
votes
1 answer

argparse module not working in Python

I'm trying to get the argparse module working in Python. My problem is that on a fresh install, I get the following: File "test.py", line 3, in import argparse File "/home/jon/Pythons/realmine/argparse.py", line 3, in parser =…
Darakian
  • 619
  • 2
  • 9
  • 22
30
votes
4 answers

how to set logging level from command line

I'm using argparse to get the logging level from the command line and then passing it as input for logging.basicConfig. However, the way I'm trying to implement this is not working. Any suggestion? Desire behavior, from command line: python main.py…
Tesla001
  • 493
  • 1
  • 5
  • 7
30
votes
2 answers

Python argparse: name parameters

I'm writing a program that use argparse, for parsing some arguments that I need. for now I have this: parser.add_argument('--rename', type=str, nargs=2, help='some help') when I run this script I see this: optional arguments: -h, --help …
Wolfy
  • 4,213
  • 8
  • 35
  • 42
30
votes
3 answers

Python: argparse optional arguments without dashes

I would like to have the following syntax: python utility.py file1 FILE1 file2 FILE2 where file1 and file2 are optional arguments. It is simple to make it working with this syntax: python utility.py --file1 FILE1 --file2…
jvm
  • 349
  • 1
  • 3
  • 7
29
votes
1 answer

Why does argparse give me a list-in-a-list?

I just noticed a behavior in argparse that puzzled me (guess I'd never used it for a dumb list of files before): import argparse parser = argparse.ArgumentParser() parser.add_argument('multi', action='append',…
Nicholas Knight
  • 15,774
  • 5
  • 45
  • 57
29
votes
1 answer

Python argparse: Mutually exclusive required group with a required option

I am trying to have a required mutually exclusive group with one required parameter. Below is the code which I have put #!/usr/bin/python import argparse import sys # Check for the option provided as part of arguments def parseArgv(): parser =…
Abhinav
  • 975
  • 4
  • 18
  • 35