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

Python's argparse to show program's version with prog and version string formatting

What's the preferred way of specifying program name and version info within argparse? __version_info__ = ('2013','03','14') __version__ = '-'.join(__version_info__) ... parser.add_argument('-V', '--version', action='version', version="%(prog)s…
type
  • 1,137
  • 2
  • 9
  • 16
60
votes
1 answer

How to make an optional value for argument using argparse?

I am creating a python script where I want to have an argument that manipulates how many search results you get as output. I've currently named the argument --head. This is the functionality I'd like it to have: When --head is not passed at the…
totokaka
  • 2,244
  • 1
  • 21
  • 33
59
votes
4 answers

Customize argparse help message

I have written the following sample code to demonstrate my issue. import argparse parser = argparse.ArgumentParser() parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') parser.parse_args() This…
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
59
votes
2 answers

Argparse"ArgumentError: argument -h/--help: conflicting option string(s): -h, --help"

Recently, I am learning argparse module, Argument error occurred below the code import argparse import sys class ExecuteShell(object): def create(self, args): """aaaaaaa""" print('aaaaaaa') return args def…
changzhi
  • 2,641
  • 9
  • 36
  • 46
58
votes
8 answers

Verbose level with argparse and multiple -v options

I'd like to be able to specify different verbose level, by adding more -v options to the command line. For example: $ myprogram.py $ myprogram.py -v $ myprogram.py -vv $ myprogram.py -v -v -v would lead to verbose=0, verbose=1, verbose=2, and…
Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
57
votes
2 answers

TypeError: __init__() got an unexpected keyword argument 'type' in argparse

Hey so I'm using argparse to try and generate a quarterly report. This is what the code looks like: parser = argparse.ArgumentParser() parser.add_argument('-q', "--quarter", action='store_true', type=int, help="Enter a Quarter number: 1,2,3, or 4…
Big_VAA
  • 754
  • 1
  • 6
  • 11
57
votes
2 answers

Custom tab completion in python argparse

How to get shell tab completion cooperating with argparse in a Python script? #!/usr/bin/env python import argparse def main(**args): pass if __name__ == '__main__': parser = argparse.ArgumentParser() …
wim
  • 338,267
  • 99
  • 616
  • 750
53
votes
1 answer

Restricting values of command line options

How do I restrict the values of the argparse options? In the below code sau option should only accept a number of 0 or 1 and bg should only allow an integer. How can I implement this? import os import sys, getopt import argparse def main (): …
user1934146
  • 2,905
  • 10
  • 25
  • 25
49
votes
3 answers

Print command line arguments with argparse?

I am using argparse to parse command line arguments. To aid debugging, I would like to print a line with the arguments which with the Python script was called. Is there a simple way to do this within argparse?
a06e
  • 18,594
  • 33
  • 93
  • 169
49
votes
3 answers

What is a namespace object?

import argparse parser = argparse.ArgumentParser(description='sort given numbers') parser.add_argument('-s', nargs = '+', type = int) args = parser.parse_args() print(args) On command line when I run the command python3 file_name.py -s 9 8 76 It…
Shashank Garg
  • 703
  • 1
  • 6
  • 14
49
votes
2 answers

I want Python argparse to throw an exception rather than usage

I don't think this is possible, but I want to handle exceptions from argparse myself. For example: import argparse parser = argparse.ArgumentParser() parser.add_argument('--foo', help='foo help', required=True) try: args =…
joedborg
  • 17,651
  • 32
  • 84
  • 118
49
votes
5 answers

How can I require my python script's argument to be a float in a range using argparse?

I'd like to use argparse on Python 2.7 to require that one of my script's parameters be between the range of 0.0 and 1.0. Does argparse.add_argument() support this?
Dolan Antenucci
  • 15,432
  • 17
  • 74
  • 100
47
votes
2 answers

Argparse optional boolean

I am trying to get the following behaviour: python test.py ⟹ store foo=False python test.py --foo ⟹ store foo=True python test.py --foo bool ⟹ store foo=bool It works when I use parser.add_argument('--foo', nargs='?', default=False,…
Hyperplane
  • 1,422
  • 1
  • 14
  • 28
47
votes
8 answers

SystemExit: 2 error when calling parse_args() within ipython

I'm learning basics of Python and got already stuck at the beginning of argparse tutorial. I'm getting the following error: import argparse parser = argparse.ArgumentParser() args = parser.parse_args() usage: __main__.py [-h] echo __main__.py:…
Haik
  • 573
  • 1
  • 4
  • 5
46
votes
10 answers

Default sub-command, or handling no sub-command with argparse

How can I have a default sub-command, or handle the case where no sub-command is given using argparse? import argparse a = argparse.ArgumentParser() b = a.add_subparsers() b.add_parser('hi') a.parse_args() Here I'd like a command to be selected,…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526