-
-
Save agusmakmun/b30cc10dfc059841832e96529a68448b to your computer and use it in GitHub Desktop.
argparse action
This file contains hidden or 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
#!/usr/bin/env python | |
# encoding: utf-8 | |
# | |
# Copyright (c) 2010 Doug Hellmann. All rights reserved. | |
# | |
"""Show the built-in argument actions. | |
""" | |
#end_pymotw_header | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-s', action='store', | |
dest='simple_value', | |
help='Store a simple value') | |
parser.add_argument('-c', action='store_const', | |
dest='constant_value', | |
const='value-to-store', | |
help='Store a constant value') | |
parser.add_argument('-t', action='store_true', | |
default=False, | |
dest='boolean_switch', | |
help='Set a switch to true') | |
parser.add_argument('-f', action='store_false', | |
default=False, | |
dest='boolean_switch', | |
help='Set a switch to false') | |
parser.add_argument('-a', action='append', | |
dest='collection', | |
default=[], | |
help='Add repeated values to a list') | |
parser.add_argument('-A', action='append_const', | |
dest='const_collection', | |
const='value-1-to-append', | |
default=[], | |
help='Add different values to list') | |
parser.add_argument('-B', action='append_const', | |
dest='const_collection', | |
const='value-2-to-append', | |
help='Add different values to list') | |
parser.add_argument('--version', action='version', | |
version='%(prog)s 1.0') | |
results = parser.parse_args() | |
print 'simple_value = %r' % results.simple_value | |
print 'constant_value = %r' % results.constant_value | |
print 'boolean_switch = %r' % results.boolean_switch | |
print 'collection = %r' % results.collection | |
print 'const_collection = %r' % results.const_collection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment