Python argparse
How I use argparse.
Basic main() stub
import argparse
import sys
= '''examples:
_examples
# Run x, y, z
python %(prog)s --output=tmp
'''
def main():
= argparse.ArgumentParser(
parser ='Argparse test',
description=_examples,
epilog=argparse.RawDescriptionHelpFormatter
formatter_class
)'--output', help='Specify output dir')
parser.add_argument(= parser.parse_args()
args print (vars(args))
if __name__ == "__main__":
main()
Argument default values
Use default=
to set a default value for an argument.
'--format', default='html', help='Specify output format', metavar='FMT') parser.add_argument(
To show default values in your help strings, use %(default)s
interpolation:
'--format', default='html', help='Specify output format (default: %(default)s)') parser.add_argument(
If the default value is type str
, it will be run through your type
callback, otherwise it’s returned directly. This can be helpful when authoring help strings containing %(default)s
:
'--enable-x', type=_str_to_bool, default=True,
parser.add_argument(help='Enable feature x (default: %(default)s)')
# outputs: --enable-x ENABLE_X Enable feature x (default: True)
'--enable-y', type=_str_to_bool, default='y',
parser.add_argument(help='Enable feature y (default: %(default)s)')
# outputs: --enable-y ENABLE_Y Enable feature y (default: y)
Argument types
Use type=
to set your own type conversion function. A type
is just a function that converts a string to some output type.
Numbers.
'--int-val', type=int, help='Specify integer', metavar='N')
parser.add_argument('--flt-val', type=float, help='Specify float', metavar='N') parser.add_argument(
Booleans. You can’t use type=bool
for booleans (try running bool('True')
in the REPL if you want to know why.) Do this instead:
def _str_to_bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
'--enable-output', type=_str_to_bool, default=False, metavar='BOOL') parser.add_argument(
Comma-separated list of values. How to specify a list of strings like python foo.py --tags=apple,fruit
:
def _parse_comma_sep(s):
if (s is None) or (s == ''):
return []
return s.split(',')
'--tags', type=_parse_comma_sep, default='', metavar='TAGS') parser.add_argument(
Subcommands
Add a subcommand called log
:
import argparse
import sys
= '''examples:
_examples
# Show log in html format
python %(prog)s log --format=html
'''
def main():
= argparse.ArgumentParser(
parser ='Argparse test',
description=_examples,
epilog=argparse.RawDescriptionHelpFormatter
formatter_class
)
= parser.add_subparsers(help='Sub-commands', dest='command')
subparsers = subparsers.add_parser('log', help='Show log')
parser_log '--format', help='Specify format', choices=['raw', 'html'])
parser_log.add_argument(
= parser.parse_args()
args print (vars(args))
if __name__ == "__main__":
main()