I have a CLI Python application that I am attempting to add an optional command flag to to be handled via argparse. I'd like to be able to pass a value to a flag that starts with a dash -
or potentially two of them.
Naturally, argparse views values like this to be new flags and understandably complains. Is there a way to pass this value in as a raw string or something similar and have it stored as you would in most use cases?
Pseudo code:
parser = argparse.ArgumentParser(
prog = 'foo',
description="bar"
)
parser.add_argument(
"-a",
"--arguments",
required=False,
type=str,
action='store',
default='',
help="hi mom"
)
args = parser.parse_args()
Shell is zsh, but something portable would be preferred if possible.
Workaround attempts:
prog.py -a '--fizz'
foo: error: argument -a/--arguments: expected one argument
prog.py -a "--fizz"
foo: error: argument -a/--arguments: expected one argument
prog.py -a "\-\-fizz"
foo: error: argument -a/--arguments: expected one argument
buzz="--fizz" && prog.py -a $buzz
foo: error: argument -a/--arguments: expected one argument
It looks like a workaround can be achieved by changing the prefix characters expected to something other than '-'
/'--'
via the prefix_chars
setting of argparse.ArgumentParser
, but that's a fairly major break from normal conventions. I suspect that I'll have to bypass argparse and pull in the data via a conf file, but I'm hoping to avoid it if I can.