Argparse argument named "print"
You can use reserved keywords togetattr() access attributes :
if getattr(args, 'print'):
However, you will make it much simpler by simply avoiding that name as your destination; use print_possibly (via destargument ):
arg_parser.add_argument('--print', dest='print_', action='store_true', help="print stuff")
# ...
if args.print_:
or, a more common synonym, for example verbose:
arg_parser.add_argument('--print', dest='verbose', action='store_true', help="print stuff")
# ...
if args.verbose:
Quick demo:
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--print', dest='print_', action='store_true', help="print stuff")
_StoreTrueAction(option_strings=['--print'], dest='print_', nargs=0, const=True, default=False, type=None, choices=None, help='print stuff', metavar=None)
>>> args = parser.parse_args(['--print'])
>>> args.print_
True