For my toy project mpu
I want to have the following commands:
# Creates a Python project based on a template
$ mpu init
# Checks a Python project for style
$ mpu stylecheck
So let's say I have a command run_init()
and a command run_stylecheck()
and an argparse.ArgumentParser
object called parser
:
def run_init():
print('init is executed')
def run_stylecheck():
print('stylecheck is executed')
def get_parser(parser=None):
"""Get parser for packaging part."""
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
if parser is None:
parser = ArgumentParser(description=__doc__,
formatter_class=ArgumentDefaultsHelpFormatter)
subparsers = parser.add_subparsers(dest='subparser_name')
pkg_init_parser = subparsers.add_parser('init')
return parser
get_parser().parse_args()
How can I add those two commands to it?
Restrictions
Please no solutions that suggest to parse sys.argv
manually and also not click
. The first is not an option as it is too hard to extend, the second one is not an option as I specifically don't want to use external dependencies - although click
is awesome.
https://stackoverflow.com/a/27529806/562769 I think is also not an option as this will be a lot of different commands and the submodule providing the parser object will not be calling that object.