Pass user command text to the Command object

Previously the Command objects did not know what command text was entered
by the user.  That mapping is done in entry points and was thrown away in
App.run_subcommand().

This adds cmd_name as an optional keyword arg to Command.__init__() and
saves it as an attribute.  App.run_subcommand() passes in the original
command text to cmd_factory() and all is well.

Closes-bug: #1411095
Change-Id: I36c42b0447652ed3bd71ad6ae0d70db7d27abbad
This commit is contained in:
Dean Troyer 2015-01-14 22:50:19 -06:00
parent ed6ab06d27
commit 91f5ad385b
4 changed files with 15 additions and 4 deletions

View File

@ -3,6 +3,7 @@
import argparse
import codecs
import inspect
import locale
import logging
import logging.handlers
@ -281,7 +282,10 @@ class App(object):
self.LOG.error(err)
return 2
cmd_factory, cmd_name, sub_argv = subcommand
cmd = cmd_factory(self, self.options)
kwargs = {}
if 'cmd_name' in inspect.getargspec(cmd_factory.__init__).args:
kwargs['cmd_name'] = cmd_name
cmd = cmd_factory(self, self.options, **kwargs)
err = None
result = 1
try:

View File

@ -16,9 +16,10 @@ class Command(object):
deprecated = False
def __init__(self, app, app_args):
def __init__(self, app, app_args, cmd_name=None):
self.app = app
self.app_args = app_args
self.cmd_name = cmd_name
return
def get_description(self):

View File

@ -27,8 +27,9 @@ class DisplayCommandBase(Command):
"""Command base class for displaying data about a single object.
"""
def __init__(self, app, app_args):
super(DisplayCommandBase, self).__init__(app, app_args)
def __init__(self, app, app_args, cmd_name=None):
super(DisplayCommandBase, self).__init__(app, app_args,
cmd_name=cmd_name)
self._formatter_plugins = self._load_formatter_plugins()
@abc.abstractproperty

View File

@ -20,3 +20,8 @@ def test_get_parser():
cmd = TestCommand(None, None)
parser = cmd.get_parser('NAME')
assert parser.prog == 'NAME'
def test_get_name():
cmd = TestCommand(None, None, cmd_name='object action')
assert cmd.cmd_name == 'object action'