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
46
votes
1 answer

How to pass on argparse argument to function as kwargs?

I have a class defined as follows class M(object): def __init__(self, **kwargs): ...do_something and I have the result of argparse.parse_args(), for example: > args = parse_args() > print args Namespace(value=5, message='test',…
Alex
  • 41,580
  • 88
  • 260
  • 469
45
votes
2 answers

Python argument parser list of list or tuple of tuples

I'm trying to use argument parser to parse a 3D coordinate so I can use --cord 1,2,3 2,4,6 3,6,9 and get ((1,2,3),(2,4,6),(3,6,9)) My attempt is import argparse parser = argparse.ArgumentParser() parser.add_argument('--cord', help="Coordinate",…
joedborg
  • 17,651
  • 32
  • 84
  • 118
45
votes
5 answers

Conditional command line arguments in Python using argparse

I'd like to have a program that takes a --action= flag, where the valid choices are dump and upload, with upload being the default. If (and only if) dump is selected, I'd like there to also be a --dump-format= option. Is there a way to express…
Alex Gaynor
  • 14,353
  • 9
  • 63
  • 113
45
votes
1 answer

Why in argparse, a 'True' is always 'True'?

Here is the simplest Python script, named test.py: import argparse parser = argparse.ArgumentParser() parser.add_argument('--bool', default=True, type=bool, help='Bool type') args = parser.parse_args() print(args.bool) But when I run this code on…
Lu Zhang
  • 471
  • 1
  • 4
  • 5
45
votes
7 answers

Call function based on argparse

I'm new to python and currently playing with it. I have a script which does some API Calls to an appliance. I would like to extend the functionality and call different functions based on the arguments given when calling the script. Currently I have…
f0rd42
  • 1,429
  • 4
  • 19
  • 30
45
votes
1 answer

Print program usage example with argparse module

I am trying to learn how to use python's argparse module. Currently my python script is: parser = argparse.ArgumentParser(description='My first argparse attempt', add_help=True) parser.add_argument("-q", action…
RanRag
  • 48,359
  • 38
  • 114
  • 167
43
votes
5 answers

Control formatting of the argparse help argument list?

import argparse parser = argparse.ArgumentParser(prog='tool') args = [('-u', '--upf', 'ref. upf', dict(required='True')), ('-s', '--skew', 'ref. skew', {}), ('-m', '--model', 'ref. model', {})] for args1, args2, desc, options in…
43
votes
4 answers

Multiple positional arguments with Python and argparse

I'm trying to use argparse to parse the command line arguments for a program I'm working on. Essentially, I need to support multiple positional arguments spread within the optional arguments, but cannot get argparse to work in this situation. In the…
Blair
  • 15,356
  • 7
  • 46
  • 56
43
votes
3 answers

path to a directory as argparse argument

I want to accept a directory path as user input in an add_argument() of ArgumentParser(). So far, I have written this: import argparse parser = argparse.ArgumentParser() parser.add_argument('path', option = os.chdir(input("paste here path to…
user3698773
  • 929
  • 2
  • 8
  • 15
42
votes
13 answers

type=dict in argparse.add_argument()

I want to use the standard library argparse module to parse command line arguments to my program, and have the program accept an optional argument -i (or --image) which is a dictionary. I tried configuring the parser like…
user975296
  • 531
  • 1
  • 4
  • 3
42
votes
2 answers

'required' is an invalid argument for positionals in python command

I want to implement import feature with required and optional parameters, to run this in this way: python manage.py import --mode archive where --mode is required and archive also. I'm using argparse library. class Command(BaseCommand): help =…
Kai
  • 553
  • 3
  • 7
  • 15
42
votes
2 answers

Multiple files for one argument in argparse Python 2.7

Trying to make an argument in argparse where one can input several file names that can be read. In this example, i'm just trying to print each of the file objects to make sure it's working correctly but I get the error: error: unrecognized…
O.rka
  • 29,847
  • 68
  • 194
  • 309
41
votes
2 answers

Python Argparse - How can I add text to the default help message?

I'm using python's argparse to handle parsing of arguments. I get a default help message structured like so: usage: ProgramName [-h] ... Description positional arguments: ... optional arguments: -h, --help show this help message…
Yoavhayun
  • 499
  • 1
  • 8
  • 16
41
votes
4 answers

Python: Argument Parsing Validation Best Practices

Is it possible when using the argparse module to add validation when parsing arguments? from argparse import ArgumentParser parser = ArgumentParser(description='Argument parser for PG restore') parser.add_argument('--database', dest='database', …
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
41
votes
3 answers

Passing a tuple as command line argument

My requirement is to pass a tuple as command line argument like --data (1,2,3,4) I tried to use the argparse module, but if I pass like this it is receiving as the string '(1,2,3,4)'. I tried by giving type=tuple for argparse.add_argument, but is…
user1423015
  • 593
  • 1
  • 4
  • 12