From ed6a5db45221d990eea8713bab18184f6f7c157e Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 21 Nov 2023 20:48:44 -0600 Subject: [PATCH] Add `Subcommand.make_arg_parser()` method split that out for clarity --- docs/conf.py | 1 + src/wuttjamaican/commands/base.py | 30 ++++++++++++++++++++++++++---- tests/commands/test_base.py | 4 +++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 683ba15..6abeee2 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -17,6 +17,7 @@ release = '0.1' extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.intersphinx', + 'sphinx.ext.viewcode', 'sphinx.ext.todo', ] diff --git a/src/wuttjamaican/commands/base.py b/src/wuttjamaican/commands/base.py index 0a974f9..c844679 100644 --- a/src/wuttjamaican/commands/base.py +++ b/src/wuttjamaican/commands/base.py @@ -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. diff --git a/tests/commands/test_base.py b/tests/commands/test_base.py index bc40a81..2776453 100644 --- a/tests/commands/test_base.py +++ b/tests/commands/test_base.py @@ -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()