The output of the funcitonality function is determined in the method argparse._HelpAction __call__.
class _HelpAction(Action):
def __init__(self,
option_strings,
dest=SUPPRESS,
default=SUPPRESS,
help=None):
super(_HelpAction, self).__init__(
option_strings=option_strings,
dest=dest,
default=default,
nargs=0,
help=help)
def __call__(self, parser, namespace, values, option_string=None):
parser.print_help()
parser.exit()
You need to either use the action help:
parser.add_argument('-t', '--test', action='help',
help='Should work just like "-h", "--help"')
Or create your own argparse action.
class MyCustomAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print("argument found, I should exit")
self.exit()
parser.add_argument('-t', '--test', action= MyCustomAction,
help='Should work just like "-h", "--help"')
docs.