Argparse - Custom Action update to an dest dict
<p>I want to collect some options as a dict using <code>argparse</code>. I have wrote a custom Action class as follows. The problem is that the <code>__call__</code> method is never invoked. </p>

<pre><code>#!/usr/bin/env python3

import argparse

class UpdateDict(argparse.Action):
    def __init__(self, option_strings, dest, nargs=None, **kwargs):
        print('UpdateDict', option_strings, dest, nargs)
        if nargs != 1:
            raise ValueError("nargs must be 1")
        super(UpdateDict, self).__init__(option_strings, dest, nargs=nargs, **kwargs)
        print(self)

    def __call__(self, parser, namespace, values, option_string=None):
        print('%r %r %r' % (namespace, values, option_string))
        dest = getattr(namespace, self.dest)
        print(dest)
        key = option_string[1]
        dest.update(key = values[0])

def parse_args():
    parser = argparse.ArgumentParser(description='formula alpha')
    parser.add_argument('formula', nargs=1, type=str, help='alpha formula')
    parser.set_defaults(mydict={})

    parser.add_argument('-a', '--alpha', nargs=1, type=str, default=['alas'], action=UpdateDict, dest='mydict')
    parser.add_argument('-b', '--beta', nargs=1, type=int, default=[0], action=UpdateDict, dest='mydict')

if __name__ == '__main__':                                                                                                                                                                                        
    args = parse_args()
    print(args)
</code></pre>