annotate example/argparse_undefined.py @ 145:a95e7f218bd2 default tip

python3.5
author Jeff Hammel <k0scist@gmail.com>
date Sun, 19 Feb 2017 20:42:57 -0800
parents df5350aee9c3
children
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):
143
Jeff Hammel <k0scist@gmail.com>
parents: 142
diff changeset
18 argparse.ArgumentParser.__init__(self, *args, **kwargs)
144
df5350aee9c3 not sure what i did here
Jeff Hammel <k0scist@gmail.com>
parents: 143
diff changeset
19 self.__defaults = {}
142
Jeff Hammel <k0scist@gmail.com>
parents: 141
diff changeset
20
141
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
21 def add_argument(self, *args, **kwargs):
144
df5350aee9c3 not sure what i did here
Jeff Hammel <k0scist@gmail.com>
parents: 143
diff changeset
22 default = kwargs.pop('default', self.argument_default)
141
Jeff Hammel <k0scist@gmail.com>
parents: 139
diff changeset
23 return argparse.ArgumentParser.add_argument
139
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 def main(args=sys.argv[1:]):
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 parser = UndefinedArgumentParser()
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 parser.add_argument('--foo', dest='foo',
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 help="foo")
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 if __name__ == '__main__':
30abbd61ea5e example
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 main()