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

argparse arguments nesting

I have a following code in python: parser = argparse.ArgumentParser(description='Deployment tool') group = parser.add_mutually_exclusive_group() group.add_argument('-a', '--add', dest='name_to_add', help='Add a new group or a role to existing…
alexarsh
  • 5,123
  • 12
  • 42
  • 51
25
votes
4 answers

argparse action or type for comma-separated list

I want to create a command line flag that can be used as ./prog.py --myarg=abcd,e,fg and inside the parser have this be turned into ['abcd', 'e', 'fg'] (a tuple would be fine too). I have done this successfully using action and type, but I feel…
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
25
votes
4 answers

Does argparse (python) support mutually exclusive groups of arguments?

If I have the arguments '-a', '-b', '-c', '-d', with the add_mutually_exclusive_group() function my program will have to use just one of them. Is there a way to combine that, so that the program will accept only either '-a 999 -b 999' or '-c 999 -d…
aeter
  • 11,960
  • 6
  • 27
  • 29
24
votes
2 answers

Namespace, argparse, and usage

This is really a few questions: Is there a reason argparse uses a namespace instead of a dictionary? Assuming I have a class with __init__(self, init_method, *args). The init_method parameter tells the init_function which way I want to initialize…
24
votes
3 answers

How to code argparse combinational options in python

I have been troubled with this small piece of activity to be completed. I did do some experiment, but was not able to achieve the result. Requirement: test2.py [-c/-v] -f Usage or Rules: -c (compare) takes 2 parameter. -v (verify) takes 1…
kumar_m_kiran
  • 3,982
  • 4
  • 47
  • 72
24
votes
1 answer

Python argparse, provide different arguments based on parent argument value

here is what i would like to do : A command that looks like git command behavior. You don't get the same options whether you typed git commit or git checkout. But in my case i want to provide different arguments based on an argument value (a file…
Narthe
  • 492
  • 1
  • 4
  • 16
24
votes
3 answers

how to get argparse to read arguments from a file with an option rather than prefix

I would like to know how to use python's argparse module to read arguments both from the command line and possibly from text files. I know of argparse's fromfile_prefix_chars but that's not exactly what I want. I want the behavior, but I don't want…
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
24
votes
3 answers

How do you get the name of the program using argparse?

I am using argparse to parse command line arguments. While going through the documentation for argparse I could only see a provision to use a different program name. I want to be able to use the default program name without having to import sys.…
VSN
  • 386
  • 1
  • 2
  • 11
24
votes
1 answer

How to get filename with argparse while specifying type=FileType(...) for this argument

Using the type parameter of the argparse.add_argument method, you can require an argument to be a readable file: parser.add_argument('--sqlite-file', type=argparse.FileType('r')) As a benefit of specifying this type, argparse checks whether the…
jack_kerouac
  • 1,482
  • 2
  • 15
  • 30
24
votes
3 answers

Overriding default argparse -h behaviour

I have a certain config file which calls upon its plugins. It's possible to pass arguments to those plugins. This config file also lets me call arbitary commands at runtime. The plugins use many arguments: one of them is -h and it does not stand for…
Mateusz Kowalczyk
  • 2,036
  • 1
  • 15
  • 29
24
votes
1 answer

Python argparse required=True but --version functionality?

In all my scripts I use the standard flags --help and --version, however I cannot seem to figure out how to make a --version with parser.add_argument(..., required=True). import sys, os, argparse parser = argparse.ArgumentParser(description='How to…
Bob Tanner
  • 250
  • 1
  • 2
  • 5
23
votes
4 answers

Python argparse type and choice restrictions with nargs > 1

The title pretty much says it all. If I have nargs greater than 1, is there any way I can set restrictions (such as choice/type) on the individual args parsed? This is some example code: parser = argparse.ArgumentParser() parser.add_argument('-c',…
user880248
23
votes
5 answers

How to find out if argparse argument has been actually specified on command line?

I am using arparse to update a config dict using values specified on the command line. Since i only want to update the values in the config for which a value was explicitly mentioned on the command line. Therefore i try to identify not-specified…
aem
  • 444
  • 1
  • 4
  • 12
23
votes
3 answers

Python argparse errors with '%' in help string

I have a default value that contains a '%' which I also insert in to the help doc of my argument. E.g.: default = "5% foo" animrender_group.add_argument( "--foo", default=default, help="Foo amount. Default: %s" % default, ) args =…
Rafe
  • 1,937
  • 22
  • 31
23
votes
3 answers

Python - Difference between docopt and argparse

I have to write a command-line interface and I've seen I can use docopt and argparse. I would like to know what are the main differences between the two so that I can make an enlightened choice. Please stick to the facts. I don't want Wow. docopt.…
baldurmen
  • 669
  • 2
  • 7
  • 15