view 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
line wrap: on
line source

#!/usr/bin/env python

"""
illustrate which options are parsed with argparse
"""

import argparse
import sys

class Undefined(object):
    """singleton"""

Undefined = Undefined()

class UndefinedArgumentParser(argparse.ArgumentParser):

    def __init__(self, *args, **kwargs):
        argparse.ArgumentParser.__init__(self, *args, **kwargs)
        self.__defaults = {}

    def add_argument(self, *args, **kwargs):
        default = kwargs.pop('default', self.argument_default)
        return argparse.ArgumentParser.add_argument

def main(args=sys.argv[1:]):

    parser = UndefinedArgumentParser()
    parser.add_argument('--foo', dest='foo',
                        help="foo")

if __name__ == '__main__':
    main()