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
29
votes
3 answers

how to pass argparse arguments to a class

I'm very new to coding in general and Python in particular. I'm trying to learn how to pass argparse arguments I have created into a class for use the right/recommend way. In addition to learning python, I'm trying to learn how to do things in an…
some_noob
  • 391
  • 1
  • 3
  • 4
29
votes
3 answers

Sphinx and argparse - autodocumenting command line scripts?

I'm building a Python package, and using Sphinx to create the docs. Aside from my package code, I also include a lot of command line Python scripts, which use argparse. I was wondering if there is a way to get Sphinx to autodocument these scripts?…
jeremiahbuddha
  • 9,701
  • 5
  • 28
  • 34
28
votes
2 answers

argparse help without duplicate ALLCAPS

I'd like to display argparse help for my options the same way the default -h,--help and -v,--version are, without the ALLCAPS text after the option, or at least without the duplicated CAPS. import argparse p = argparse.ArgumentParser("a foo bar…
matt wilkie
  • 17,268
  • 24
  • 80
  • 115
28
votes
2 answers

Python argparse custom actions with additional arguments passed

import argparse class customAction(argparse.Action): def __call__(self, parser, args, values, option_string=None): setattr(args, self.dest, values) parser = argparse.ArgumentParser() parser.add_argument('-e', '--example',…
user880248
28
votes
2 answers

argparse add_argument alias

Is there a way to create an alias using argparse? For example, I want to do something like this: parser.add_argument('--foo' ...) parser.add_argument_alias('--bar', '--foo') That is, using --bar should be equivalent to using --foo.
walkerlala
  • 1,599
  • 1
  • 19
  • 32
28
votes
5 answers

How can I automatically display the help on command line arguments error?

Currently when I enter invalid options or omit positional arguments, argparse kicks me back to the prompt and displays the usage for my app. This is ok, but I would rather automatically display the full help listing (that explains the options, etc.)…
jpswain.w
  • 283
  • 1
  • 3
  • 4
28
votes
4 answers

Move "help" to a different Argument Group in python argparse

Currently I'm creating a directory reader program using Python. I'm using 'argparse' to parse the arguments from command line. I have the following code: parser = argparse.ArgumentParser(prog = "LS.py", usage =…
Night Train
  • 761
  • 1
  • 11
  • 19
27
votes
6 answers

Argparse: ignore multiple positional arguments when optional argument is specified

I'm trying to make argparse ignore the fact that two normally required positional arguments shouldn't be evaluated when an optional argument (-l) is specified. Basically I'm trying to replicate the behavior of --help: when you specify the -h, all…
user402250
27
votes
3 answers

How to use python argparse with args other than sys.argv?

Is there a way to use argparse with any list of strings, instead of only with sys.argv? Here's my problem: I have a program which looks something like this: # This file is program1.py import argparse def main(argv): parser =…
ClydeTheGhost
  • 1,473
  • 2
  • 17
  • 31
27
votes
2 answers

Display pydoc's description as part of argparse '--help'

I am using argparse.ArgumentParser() in my script, I would like to display the pydoc description of my script as part of the '--help' option of the argparse. One possibly solution can be to use the formatter_class or the description attribute of…
baky
  • 631
  • 1
  • 8
  • 17
26
votes
3 answers

Disable unique prefix matches for argparse and optparse

When I use Python's argparse or optparse command line argument parser, any unique prefix of an argument is considered valid, e.g. $ ./buildall.py --help usage: buildall.py [-h] [-f] Build all repositories optional arguments: -h, --help show…
Simon Warta
  • 10,850
  • 5
  • 40
  • 78
26
votes
2 answers

Difference between --default and --store_const in argparse

I read the following in the argparse documentation: 'store_const' - This stores the value specified by the const keyword argument. (Note that the const keyword argument defaults to the rather unhelpful None.) The 'store_const' action is most…
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
26
votes
5 answers

Python,argparse: how to have nargs=2 with type=str and type=int

I spent some times on the argparse documentation, but I'm still struggling with this module for one option in my program: parser.add_argument("-r", "--rmsd", dest="rmsd", nargs=2, help="extract the poses that are close from a ref according…
Macrocyle
  • 363
  • 1
  • 3
  • 5
26
votes
7 answers

ImportError: No module named argparse

I am trying to run a Python program but get the error ImportError: No module named argparse I found the question “argparse Python modules in cli” here on StackOverflow and tried the first comment, i.e. running the command python -c "import…
alex
  • 2,252
  • 4
  • 23
  • 34
26
votes
5 answers

Sort argparse help alphabetically

I am using Python's (2.7) argparse facility and would like to automatically sort the help it produces alphabetically by option. By default help entries are sorted in the order they are added*, as in: p = argparse.ArgumentParser(description='Load…
Bryan P
  • 5,900
  • 5
  • 34
  • 49