[Tutor] Argparse functions with N parameters

Danilo Chilene bicofino at gmail.com
Tue May 7 16:57:27 CEST 2013


Hello,

I have the code below:

import argparse
class Myclass(object):

    def foo(self):
        print 'foo'

    def bar(self):
        print 'bar'

    def test(self,name,place):
        print name, place

class Main(Myclass):
    def __init__(self):
        foo_parser = argparse.ArgumentParser()
        foo_parser.add_argument('method')
        self.args = foo_parser.parse_args()

    def __call__(self, *args, **kws):
        method = self.args.method
        return getattr(self, method)(*args, **kws)

if __name__ == "__main__":
    main = Main()

    main()

It's working for functions foo and bar:

5: python arg.py foo
foo

5: python arg.py bar
bar

5: python arg.py test a b
usage: arg.py [-h] method
arg.py: error: unrecognized arguments: a b


But for function test I guess I'm missing something, the idea is to
have N functions and N param.

Any ideas?

Best Regards,

Danilo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130507/b094249b/attachment.html>


More information about the Tutor mailing list