Created
November 8, 2017 08:20
-
-
Save jn0/83b99300da6813d79bed489205242a23 to your computer and use it in GitHub Desktop.
`argparse.ArgumentParser` is great, but sometimes you want a more handy one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
class AP(argparse.ArgumentParser): | |
def add(self, *av, **kw): | |
if 'default' not in kw: | |
if kw.get('action') == 'count': | |
kw['default'] = 0 | |
self.add_argument(*av, **kw) | |
return self | |
#end class AP | |
arg = AP( | |
description='Sample usage code.' | |
).add( | |
'-v', '--verbose', | |
action='count', dest='verbose', | |
help='more -v more verbose' | |
).add( | |
'-q', '--quiet', | |
action='count', dest='quiet', | |
help='more -q more silence' | |
).parse_args() | |
verbose = arg.verbose - arg.quiet | |
# EOF # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment