summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_argparse.py
diff options
context:
space:
mode:
authorSteven Bethard <steven.bethard@gmail.com>2010-12-18 11:19:23 (GMT)
committerSteven Bethard <steven.bethard@gmail.com>2010-12-18 11:19:23 (GMT)
commitfd311a712d5876c3a3efff265978452eea759f85 (patch)
tree6222b825a28cbe2e94f167eb4f1ae519c6dada52 /Lib/test/test_argparse.py
parent04129748ae4f794dffbcf07bf53be27dc79c2d09 (diff)
downloadcpython-fd311a712d5876c3a3efff265978452eea759f85.zip
cpython-fd311a712d5876c3a3efff265978452eea759f85.tar.gz
cpython-fd311a712d5876c3a3efff265978452eea759f85.tar.bz2
Add subparser aliases for argparse. Resolves issue 9324. Approved by Georg for beta2 on the tracker.
Diffstat (limited to 'Lib/test/test_argparse.py')
-rw-r--r--Lib/test/test_argparse.py51
1 files changed, 49 insertions, 2 deletions
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 0b7ed5e..d536be9 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -1708,7 +1708,8 @@ class TestAddSubparsers(TestCase):
def assertArgumentParserError(self, *args, **kwargs):
self.assertRaises(ArgumentParserError, *args, **kwargs)
- def _get_parser(self, subparser_help=False, prefix_chars=None):
+ def _get_parser(self, subparser_help=False, prefix_chars=None,
+ aliases=False):
# create a parser with a subparsers argument
if prefix_chars:
parser = ErrorRaisingArgumentParser(
@@ -1724,13 +1725,21 @@ class TestAddSubparsers(TestCase):
'bar', type=float, help='bar help')
# check that only one subparsers argument can be added
- subparsers = parser.add_subparsers(help='command help')
+ subparsers_kwargs = {}
+ if aliases:
+ subparsers_kwargs['metavar'] = 'COMMAND'
+ subparsers_kwargs['title'] = 'commands'
+ else:
+ subparsers_kwargs['help'] = 'command help'
+ subparsers = parser.add_subparsers(**subparsers_kwargs)
self.assertArgumentParserError(parser.add_subparsers)
# add first sub-parser
parser1_kwargs = dict(description='1 description')
if subparser_help:
parser1_kwargs['help'] = '1 help'
+ if aliases:
+ parser1_kwargs['aliases'] = ['1alias1', '1alias2']
parser1 = subparsers.add_parser('1', **parser1_kwargs)
parser1.add_argument('-w', type=int, help='w help')
parser1.add_argument('x', choices='abc', help='x help')
@@ -1947,6 +1956,44 @@ class TestAddSubparsers(TestCase):
-y {1,2,3} y help
'''))
+ def test_alias_invocation(self):
+ parser = self._get_parser(aliases=True)
+ self.assertEqual(
+ parser.parse_known_args('0.5 1alias1 b'.split()),
+ (NS(foo=False, bar=0.5, w=None, x='b'), []),
+ )
+ self.assertEqual(
+ parser.parse_known_args('0.5 1alias2 b'.split()),
+ (NS(foo=False, bar=0.5, w=None, x='b'), []),
+ )
+
+ def test_error_alias_invocation(self):
+ parser = self._get_parser(aliases=True)
+ self.assertArgumentParserError(parser.parse_args,
+ '0.5 1alias3 b'.split())
+
+ def test_alias_help(self):
+ parser = self._get_parser(aliases=True, subparser_help=True)
+ self.maxDiff = None
+ self.assertEqual(parser.format_help(), textwrap.dedent("""\
+ usage: PROG [-h] [--foo] bar COMMAND ...
+
+ main description
+
+ positional arguments:
+ bar bar help
+
+ optional arguments:
+ -h, --help show this help message and exit
+ --foo foo help
+
+ commands:
+ COMMAND
+ 1 (1alias1, 1alias2)
+ 1 help
+ 2 2 help
+ """))
+
# ============
# Groups tests
# ============