Fix subparsers add_parser() regression

With SubCommandOpt, the handler() callback gets invoked with a
subparsers object which has a add_parser() method. The arguments to
this method match the arguments to the ArgumentParser constructor and
the arguments are passed to the class for our ArgumentParser instance.

However, we added a ArgumentParser subclass (_CachedArgumentParser) so
that we could sort the output of --help ... but the subclass doesn't
support all the arguments of the parent class.

glance-control is the only known example of a SubCommandOpt user which
passes unusual arguments to subparsers.add_parser(). It uses the parent
arg and blows up at startup with:

   File "./bin/glance-control", line 276, in add_command_parsers
     parser = subparsers.add_parser(server, parents=[cmd_parser])
   File "/usr/lib64/python2.7/argparse.py", line 1064, in add_parser
     parser = self._parser_class(**kwargs)
 TypeError: __init__() got an unexpected keyword argument 'parents'

It appears we broke glance-control back in June (commit 2951391), but
nobody noticed it until 1.2.0 was released and broke stable/grizzly
because we stopped using glance-control in the Havana glance unit tests
before this regression.

Closes-Bug: #1230416
Change-Id: I8d1b52e5390295726eb49af32eb7cab14c29592c
This commit is contained in:
Mark McLoughlin 2013-09-25 20:07:03 +01:00
parent 9dabbd0ff7
commit bf70519359
2 changed files with 18 additions and 2 deletions

View File

@ -1462,8 +1462,8 @@ class _CachedArgumentParser(argparse.ArgumentParser):
order.
"""
def __init__(self, prog=None, usage=None):
super(_CachedArgumentParser, self).__init__(prog, usage)
def __init__(self, prog=None, usage=None, **kwargs):
super(_CachedArgumentParser, self).__init__(prog, usage, **kwargs)
self._args_cache = {}
def add_parser_argument(self, container, *args, **kwargs):

View File

@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import os
import shutil
import sys
@ -2627,6 +2628,21 @@ class SubCommandTestCase(BaseTestCase):
self.assertEqual(self.conf.cmd.name, 'a')
self.assertEqual(self.conf.cmd.bar, 10)
def test_sub_command_with_parent(self):
def add_parsers(subparsers):
parent = argparse.ArgumentParser(add_help=False)
parent.add_argument('bar', type=int)
subparsers.add_parser('a', parents=[parent])
self.conf.register_cli_opt(
cfg.SubCommandOpt('cmd', handler=add_parsers))
self.assertTrue(hasattr(self.conf, 'cmd'))
self.conf(['a', '10'])
self.assertTrue(hasattr(self.conf.cmd, 'name'))
self.assertTrue(hasattr(self.conf.cmd, 'bar'))
self.assertEqual(self.conf.cmd.name, 'a')
self.assertEqual(self.conf.cmd.bar, 10)
def test_sub_command_with_dest(self):
def add_parsers(subparsers):
subparsers.add_parser('a')