2
0
Fork 0

Add Subcommand.make_arg_parser() method

split that out for clarity
This commit is contained in:
Lance Edgar 2023-11-21 20:48:44 -06:00
parent 4641e24afd
commit ed6a5db452
3 changed files with 30 additions and 5 deletions

View file

@ -17,6 +17,7 @@ release = '0.1'
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.intersphinx',
'sphinx.ext.viewcode',
'sphinx.ext.todo',
]

View file

@ -357,6 +357,16 @@ class Subcommand:
cd /path/to/venv
bin/poser hello --help
bin/wutta-poser hello --help
.. attribute:: stdout
Reference to file-like object which should be used for writing
to STDOUT. This is inherited from :attr:`Command.stdout`.
.. attribute:: stderr
Reference to file-like object which should be used for writing
to STDERR. This is inherited from :attr:`Command.stderr`.
"""
name = 'UNDEFINED'
description = "TODO: not defined"
@ -366,16 +376,28 @@ class Subcommand:
command,
):
self.command = command
self.stdout = self.command.stdout
self.stderr = self.command.stderr
self.config = self.command.config
if self.config:
self.app = self.config.get_app()
self.parser = argparse.ArgumentParser(
# build arg parser
self.parser = self.make_arg_parser()
self.add_args()
def __repr__(self):
return f"Subcommand(name={self.name})"
def make_arg_parser(self):
"""
Must return a new :class:`argparse.ArgumentParser` instance
for use by the subcommand.
"""
return argparse.ArgumentParser(
prog=f'{self.command.name} {self.name}',
description=self.description)
self.add_args()
def add_args(self):
"""
Configure additional args for the subcommand arg parser.

View file

@ -169,9 +169,11 @@ class TestCommandArgumentParser(TestCase):
class TestSubcommand(TestCase):
def test_run(self):
def test_basic(self):
cmd = base.Command()
subcmd = base.Subcommand(cmd)
subcmd.name = 'foobar'
self.assertEqual(repr(subcmd), 'Subcommand(name=foobar)')
# TODO: this doesn't really test anything per se, but at least
# gives us the coverage..
subcmd._run()