annotate example/argparse_undefined.py @ 142:efd2d3330296

wip
author Jeff Hammel <k0scist@gmail.com>
date Mon, 01 Dec 2014 18:08:58 -0800
parents c6aea14a3e2b
children 2825b0fdf80e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
139
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 illustrate which options are parsed with argparse
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import argparse
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import sys
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9
141
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
10 class Undefined(object):
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
11 """singleton"""
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
12
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
13 Undefined = Undefined()
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
14
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
15 class UndefinedArgumentParser(argparse.ArgumentParser):
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
16
142
Jeff Hammel <k0scist@gmail.com>
parents: 141
diff changeset
17 def __init__(self, *args, **kwargs):
Jeff Hammel <k0scist@gmail.com>
parents: 141
diff changeset
18
141
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
19 def add_argument(self, *args, **kwargs):
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
20 kwargs.setdefault('default', Undefined)
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
21 return argparse.ArgumentParser.add_argument
139
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 def main(args=sys.argv[1:]):
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 parser = UndefinedArgumentParser()
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 parser.add_argument('--foo', dest='foo',
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 help="foo")
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 if __name__ == '__main__':
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 main()