Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python argparse: Leading dash in argument

I'm using Python's argparse module to parse command line arguments. Consider the following simplified example,

# File test.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-s', action='store')
parser.add_argument('-a', action='append')
args = parser.parse_args()
print(args)

which can successfully be called like

python test.py -s foo -a bar -a baz

A single argument is required after -s and after each -a, which may contain spaces if we use quotation. If however an argument starts with a dash (-) and does not contain any spaces, the code crashes:

python test.py -s -begins-with-dash -a bar -a baz

error: argument -s: expected one argument

I get that it interprets -begins-with-dash as the beginning of a new option, which is illegal as -s has not yet received its required argument. It's also pretty clear though that no option with the name -begins-with-dash has been defined, and so it should not interpret it as an option in the first place. How can I make argparse accept arguments with one or more leading dashes?

like image 738
jmd_dk Avatar asked Oct 03 '18 22:10

jmd_dk


People also ask

How do you pass a command line argument in Python Argparse?

To add your arguments, use parser. add_argument() . Some important parameters to note for this method are name , type , and required . The name is exactly what it sounds like — the name of the command line field.

Why click instead of Argparse?

Click actually implements its own parsing of arguments and does not use optparse or argparse following the optparse parsing behavior. The reason it's not based on argparse is that argparse does not allow proper nesting of commands by design and has some deficiencies when it comes to POSIX compliant argument handling.

What is Argparse ArgumentParser ()?

The argparse module provides a convenient interface to handle command-line arguments. It displays the generic usage of the program, help, and errors. The parse_args() function of the ArgumentParser class parses arguments and adds value as an attribute dest of the object.

What is parser Add_argument?

parser. add_argument('indir', type=str, help='Input dir for videos') created a positional argument. For positional arguments to a Python function, the order matters. The first value passed from the command line becomes the first positional argument. The second value passed becomes the second positional argument.


2 Answers

You can force argparse to interpret an argument as a value by including an equals sign: python test.py -s=-begins-with-dash -a bar -a baz Namespace(a=['bar', 'baz'], s='-begins-with-dash')

like image 114
Joey Harrington Avatar answered Oct 16 '22 14:10

Joey Harrington


If you are instead trying to provide multiple values to one argument:

parser.add_argument('-a', action='append', nargs=argparse.REMAINDER)

will grab everything after -a on the command line and shove it in a.

python test.py -toe -a bar fi -fo -fum -s fee -foo
usage: test.py [-h] [-s S] [-a ...]
test.py: error: unrecognized arguments: -toe

python test.py -a bar fi -fo -fum -s fee -foo
Namespace(a=[['bar', 'fi', '-fo', '-fum', '-s', 'fee', '-foo']], s=None)

Note that even though -s is a recognized argument, argparse.REMAINDER adds it to the list of args found by -a since it is after -a on the command line

like image 27
jeremysprofile Avatar answered Oct 16 '22 12:10

jeremysprofile