background I have a script where I am using parseargs
to partially parse the input.
parser = argparse.ArgumentParser()
parser.add_argument("-c", action="store_true")
nspc = parser.parse_known_args()
The reason for using parseargs is to allow the -c option to occur anywhere in the input. I only want to parse this option, and then later in the code I do additional parsing on my own. The reason for this is, the script accepts a large number of arguments from a configuration file, ie, myscript.py doesn't know the arguments until runtime.
problem The problem I am running into is, some of the arguments may start with the letter "c.", so if I say
$ myscript.py "some argument" -cdef
I get the error
myscript.py: error: argument -c: ignored explicit argument 'ef'
Since I said parser.add_argument("-c", ...
I would expect that -c
would be parsed but -cdef
would not. Obviously, -c
is an optional argument.
How can I tell the parser that -c
is an option but -cdef
is not?
Use --
for multi-character argument names.
It's fairly common in UNIX for -cdef
to mean -c -d -e -f
, whereas --cdef
is a single option. argparse
follows this convention.
(As an example: ls -la
is the equivalent of ls -l -a
, whereas ls --color
is just a single option.)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments