I have the following python code set up, in which the program would return "something" whenever the user pass -- as an argument:
#script.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--",
help="Prints something",
action="store_true",
dest="print_something",
)
args = parser.parse_args()
if args.print_something:
print("something")
The output is as follows:
$ python .\script.py --
usage: script.py [-h] [--]
playground.py: error: unrecognized arguments: --
Argparse is not able to recognise the -- argument.
I tried using escape sequences, like putting -\-\- under parser.add_argument(, yet the program is not behaving the way it should.
There is, of course, a workaround using sys.arg, which goes something like:
import sys
if "--" in sys.argv:
print("something")
But the above approach is impractical for projects with alot of arguments -- especially for those containing both functional and positional argument.
Therefore, is there anyway to parse the -- argument using argparser?
--conventionally has a very specific meaning. In isolation, you don't seem to need an option at all; if the program doesn't do anything without the option, it's not optional.--having a special meaning, namely "end of options". It's unconvential, probably unwise, and likely impossible, to override that behaviour.