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 --help
. Now, my issue is that I want to call my own Python script as well as pass it some arguments. I'm using argparse and wanting to be consistent with the rest of the config, I created a -h
flag. To my surprise, argparse just gives me argparse.ArgumentError: argument -h/--help: conflicting option string(s): -h
instead of minding its own business.
Is there a way to stop this from happening?
I am well aware that most people expect -h
to give help but it's my own script and I think I know better what I want to use a flag for than the argparse devs.
Look in the argparse documentation for the ArgumentParser arguments. There's one called add_help, which defaults to True.
parser = argparse.ArgumentParser('Cool', add_help=False) parser.add_argument('-h', '--hi', action='store_true', dest='hi')
This works as expected.
If you give the ArgumentParser a conflict_handler="resolve" argument, adding your own -h will override the existing one, while keeping --help functional.
#!/usr/bin/env python3 import argparse parse = argparse.ArgumentParser(conflict_handler="resolve") parse.add_argument("-h", "--hello") print(parse.parse_args())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With