Custom 'usage' function in argparse?
Yes, The default message can be overridden with the usage= keyword argument like this,
def msg(name=None):
return '''program.py
[-a, Pass argument a]
[-b, Pass argument b]
[-c, Pass argument c]
[-d, Pass argument d]
comment
more comment
'''
and using
import argparse
parser = argparse.ArgumentParser(description='Sample argparse py', usage=msg())
parser.add_argument("-arg_1", help='with_some_message')
parser.print_help()
the above output is like this,
usage: program.py
[-a, Pass argument a]
[-b, Pass argument b]
[-c, Pass argument c]
[-d, Pass argument d]
comment
more comment
Sample argparse py
optional arguments:
-h, --help show this help message and exit
-arg_1 ARG_1 with_some_message
Note: Refer here
You can also call a custom function based on argument input using action=
keyword argument:
>>> class FooAction(argparse.Action):
... def __call__(self, parser, namespace, values, option_string=None):
... print '%r %r %r' % (namespace, values, option_string)
... setattr(namespace, self.dest, values)
...
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action=FooAction)
>>> parser.add_argument('bar', action=FooAction)
>>> args = parser.parse_args('1 --foo 2'.split())
Namespace(bar=None, foo=None) '1' None
Namespace(bar='1', foo=None) '2' '--foo'
>>> args
Namespace(bar='1', foo='2')
Yes, use the usage
option. From the docs:
>>> parser = argparse.ArgumentParser(prog='PROG', usage='%(prog)s [options]')
>>> parser.add_argument('--foo', nargs='?', help='foo help')
>>> parser.add_argument('bar', nargs='+', help='bar help')
>>> parser.print_help()
usage: PROG [options]
positional arguments:
bar bar help
optional arguments:
-h, --help show this help message and exit
--foo [FOO] foo help