#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Defining custom action callbacks """ #end_pymotw_header import argparse class CustomAction(argparse.Action): def __init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None): argparse.Action.__init__(self, option_strings=option_strings, dest=dest, nargs=nargs, const=const, default=default, type=type, choices=choices, required=required, help=help, metavar=metavar, ) print print 'Initializing CustomAction' for name,value in sorted(locals().items()): if name == 'self' or value is None: continue print ' %s = %r' % (name, value) return def __call__(self, parser, namespace, values, option_string=None): print print 'Processing CustomAction for "%s"' % self.dest print ' parser = %s' % id(parser) print ' values = %r' % values print ' option_string = %r' % option_string # Do some arbitrary processing of the input values if isinstance(values, list): values = [ v.upper() for v in values ] else: values = values.upper() # Save the results in the namespace using the destination # variable given to our constructor. setattr(namespace, self.dest, values) parser = argparse.ArgumentParser() parser.add_argument('-a', action=CustomAction) parser.add_argument('-m', nargs='*', action=CustomAction) parser.add_argument('positional', action=CustomAction) results = parser.parse_args(['-a', 'value', '-m' 'multi-value', 'positional-value']) print print results