Skip to content

Commit d61cb05

Browse files
committed
Add further tests for aliased subcommands
* Traversal fails when subcommands have aliases. * Add test `test_aliased_traversal` for this case. * Add copy of `test_parse_nested` as `test_parse_nested_with_alias` modified to include an alias for a subcommand.
1 parent 08f52d3 commit d61cb05

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

test/test_parser.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,79 @@ def test_parse_nested():
186186
}
187187
]
188188

189+
def test_parse_nested_with_alias():
190+
parser = argparse.ArgumentParser()
191+
parser.add_argument('foo', default=False, help='foo help')
192+
parser.add_argument('bar', default=False)
193+
194+
subparsers = parser.add_subparsers()
195+
196+
subparser = subparsers.add_parser('install', aliases=['i'], help='install help')
197+
subparser.add_argument('ref', type=str, help='foo1 help')
198+
subparser.add_argument('--upgrade', action='store_true', default=False, help='foo2 help')
199+
200+
data = parse_parser(parser)
201+
202+
assert data['action_groups'][0]['options'] == [
203+
{
204+
'name': ['foo'],
205+
'help': 'foo help',
206+
'default': False
207+
}, {
208+
'name': ['bar'],
209+
'help': '',
210+
'default': False
211+
}
212+
]
213+
214+
assert data['children'] == [
215+
{
216+
'name': 'install (i)',
217+
'help': 'install help',
218+
'usage': 'usage: py.test install [-h] [--upgrade] ref',
219+
'bare_usage': 'py.test install [-h] [--upgrade] ref',
220+
'action_groups': [
221+
{
222+
'title': 'Positional Arguments',
223+
'description': None,
224+
'options': [
225+
{
226+
'name': ['ref'],
227+
'help': 'foo1 help',
228+
'default': None
229+
}
230+
]
231+
},
232+
{
233+
'description': None,
234+
'title': 'Named Arguments',
235+
'options': [
236+
{
237+
'name': ['--upgrade'],
238+
'default': False,
239+
'help': 'foo2 help'
240+
}
241+
]
242+
}
243+
]
244+
}
245+
]
246+
247+
def test_aliased_traversal():
248+
parser = argparse.ArgumentParser()
249+
250+
subparsers1 = parser.add_subparsers()
251+
subparsers1.add_parser('level1', aliases=['l1'])
252+
253+
data = parse_parser(parser)
254+
255+
data2 = parser_navigate(data, 'level1')
256+
257+
assert(data2 == {
258+
'bare_usage': 'py.test level1 [-h]',
259+
'help': '',
260+
'usage': 'usage: py.test level1 [-h]',
261+
'name': 'level1'})
189262

190263
def test_parse_nested_traversal():
191264
parser = argparse.ArgumentParser()

0 commit comments

Comments
 (0)