import argparse class FooAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): if values=='Q': setattr(namespace, self.dest, {'type':values,'quick':True,'slow':False}) else: setattr(namespace, self.dest, {'type':values,'quick':False,'slow':True}) def bar(string): return string.upper()[0] parser = argparse.ArgumentParser() parser.add_argument('-a','--algorithm', choices=['Q','S'], default='s', action=FooAction, type=bar) parser.add_argument('foo', choices=['Q','S'], default='s', action=FooAction, nargs='?', type=bar) print(parser.parse_args([])) # Namespace(algorithm='S', # foo={'slow': True, 'type': 'S', 'quick': False}) print(parser.parse_args(['quick'])) # Namespace(algorithm='S', # foo={'type': 'Q', 'quick': True, 'slow': False}) print(parser.parse_args(['-a', 'quick'])) # Namespace(algorithm={'type': 'Q', 'quick': True, 'slow': False}, # foo={'type': 'S', 'quick': False, 'slow': True}) print(parser.parse_args())