Package: argdispatch
If your parser has a few subcommands, you can parse them withargparse. If you have more, you still can, but you will get a huge, unreadable code. This module makes this easier by dispatching subcommand calls to functions, modules or binaries.ExamplesExample 1 : Manual definition of subcommandsFor instance, consider the following code formycommand.py:importsysfromargdispatchimportArgumentParserdeffoo(args):"""A function associated to subcommand `foo`."""print("Doing interesting stuff")sys.exit(1)if__name__=="__main__":parser=ArgumentParser()subparser=parser.add_subparsers()subparser.add_function(foo)subparser.add_module("bar")subparser.add_executable("baz")parser.parse_args()With this simple code:mycommand.py foo-v--arg=2is equivalent to the python codefoo(['-v','--arg=2']);mycommand.py bar-v--arg=2is equivalent topython-mbar-v--arg=2;mycommand.py baz-v--arg=2is equivalent tobaz-v--arg=2.Then, each function, module or binary does whatever it wants with the arguments.Example 2 : Automatic definition of subcommandsWith programs likegit, if agit-foobinary exists, then callinggit foo--some=argumentsis equivalent togit-foo--some=arguments. The following code, inmyprogram.pycopies this behaviour:importsysfromargdispatchimportArgumentParserif__name__=="__main__":parser=ArgumentParser()subparser=parser.add_subparsers()subparser.add_submodules("myprogram")subparser.add_prefix_executables("myprogram-")parser.parse_args()With this program, given that binarymyprogram-fooand python modulemyprogram.bar.__main__.pyexist:myprogram foo-v--arg=2is equivalent tomyprogram-foo-v--arg=2;myprogram bar-v--arg=2is equivalent topython-mmyprogram.bar-v--arg=2.Example 3 : Defining subcommands with entry pointsNow that your program is popular, people start writing plugins. Great! You want to allow them to add subcommands to your program. To do so, simply use this code:importsysfromargdispatchimportArgumentParserif__name__=="__main__":parser=ArgumentParser()subparser=parser.add_subparsers()# You probably should only have one of those.subparser.add_entrypoints_functions("myprogram.subcommand.function")subparser.add_entrypoints_modules("myprogram.subcommand.module")parser.parse_args()With this code, plugin writers can add lines like those in theirsetup.cfg:[options.entry_points]myprogram.subcommand.function=foo=mypluginfoo:myfunctionmyprogram.subcommand.module=bar=mypluginbarThen, given than functionmyfunction()exists in modulemypluginfoo, and than modulemypluginbarexists:myprogram foo-v--arg=2is equivalent to the python codemyfunction(['-v','--arg=2']);myprogram bar-v--arg=2is equivalent topython-mmypluginbar-v--arg=2.DocumentationThe complete documentation is available onreadthedocs.To compile it from source, download and run:cd doc && make htmlWhat’s new?Seechangelog.Download and installFrom sources:Download:https://pypi.python.org/pypi/argdispatchInstall (in avirtualenv, if you do not want to mess with your distribution installation system):python3 -m pip install .From pip:pip install argdispatchQuick and dirty Debian (and Ubuntu?) packageThis requiresstdebto be installed:python3 setup.py --command-packages=stdeb.command bdist_deb
sudo dpkg -i deb_dist/argdispatch-<VERSION>_all.deb