I'm writing a relatively simple Python script which supports a couple of different commands. The different commands support different options and I want to be able to pass the options parsed by argparse to the correct method for the specified command.

The usage string looks like so:

usage: script.py [-h]

            {a, b, c}
            ...
script.py: error: too few arguments

I can easily call the appropriate method:

def a():
    ...

def b():
    ...

def c():
    ...

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.set_defaults(method = a)
    ...

    arguments = parser.parse_args()
    arguments.method()

However, I have to pass arguments to these methods and they all accept different sets of arguments.

Currently, I just pass the Namespace object returned by argparse, like so:

 def a(arguments):
     arg1 = getattr(arguments, 'arg1', None)
     ...

This seems a little awkward, and makes the methods a little harder to reuse as I have to pass arguments as a dict or namespace rather than as usual parameters.

I would like someway of defining the methods with parameters (as you would a normal function) and still be able to call them dynamically while passing appropriate parameters. Like so:

def a(arg1, arg2):
    ...

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.set_defaults(method = a)
    ...

    arguments = parser.parse_args()
    arguments.method() # <<<< Arguments passed here somehow

Any ideas?

Edit
Report