Skip to content

Commit 178672c

Browse files
authored
Merge pull request #109 from hstock/fix-aliased-subcommands
Fix aliased subcommands
2 parents 08f52d3 + edd52b4 commit 178672c

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

sphinxarg/parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def parser_navigate(parser_result, path, current_path=None):
2020
' '.join(current_path))
2121
next_hop = path.pop(0)
2222
for child in parser_result['children']:
23-
if child['name'] == next_hop:
23+
# identifer is only used for aliased subcommands
24+
identifier = child['identifier'] if 'identifier' in child else child['name']
25+
if identifier == next_hop:
2426
current_path.append(next_hop)
2527
return parser_navigate(child, path, current_path)
2628
raise NavigationException(
@@ -88,6 +90,8 @@ def parse_parser(parser, data=None, **kwargs):
8890
'usage': subaction.format_usage().strip(),
8991
'bare_usage': _format_usage_without_prefix(subaction),
9092
}
93+
if subalias:
94+
subdata['identifier'] = name
9195
parse_parser(subaction, subdata, **kwargs)
9296
data.setdefault('children', []).append(subdata)
9397

test/test_parser.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
from sphinxarg.parser import parse_parser, parser_navigate
3+
import six
34

45

56
def test_parse_options():
@@ -187,6 +188,84 @@ def test_parse_nested():
187188
]
188189

189190

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

0 commit comments

Comments
 (0)