Skip to content

Commit a14f67a

Browse files
committed
feat: subparser return a dictionary instead of a list
1 parent 6c49564 commit a14f67a

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

powerapi/cli/parser.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,10 @@ def subparse(self, token_list):
322322
parsed argument and the result of the parsing
323323
324324
"""
325-
# print(vars(self.actions))
326325
local_result = deepcopy(self.default_values)
327326
if token_list == []:
328327
return token_list, local_result
329328

330-
local_result['type'] = self.name
331-
332329
return self._parse(token_list, local_result)
333330

334331

@@ -453,11 +450,11 @@ def add_component_subparser(self, component_name, subparser, help_str=''):
453450
"""
454451
def _action(arg, val, args, acc):
455452
if arg not in acc:
456-
acc[arg] = []
453+
acc[arg] = {}
457454

458455
subparser = self.subparsers_group[arg].get_subparser(val)
459456
args, subparse_result = subparser.subparse(args)
460-
acc[arg].append(subparse_result)
457+
acc[arg][subparser.name] = subparse_result
461458
return args, acc
462459

463460
if component_name not in self.subparsers_group:

tests/unit/cli/test_parser.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def test_subparser():
200200
- "" : {}
201201
- "-z" : UnknowArgException(z)
202202
- "-a" : {a: True}
203-
- "-a --sub toto -b" : {a:True, sub: [{type : toto, b: True}]}
203+
- "-a --sub toto -b" : {a:True, sub: {'toto' : {b: True}}}
204204
- "-b" : BadContextException(b, [toto])
205205
206206
Parser description :
@@ -223,7 +223,7 @@ def test_subparser():
223223
check_parsing_result(parser, '-a', {'a': True})
224224

225225
check_parsing_result(parser, '-a --sub toto -b',
226-
{'a': True, 'sub': [{'type': 'toto', 'b': True}]})
226+
{'a': True, 'sub': {'toto': { 'b': True}}})
227227

228228
with pytest.raises(BadContextException):
229229
check_parsing_result(parser, '-b', None)
@@ -364,7 +364,7 @@ def test_add_component_subparser_with_two_name():
364364
subparser = ComponentSubParser('titi')
365365
subparser.add_argument('a', 'aaa', flag=True, action=store_true, default=False)
366366
parser.add_component_subparser('sub', subparser)
367-
check_parsing_result(parser, '--sub titi -a', {'sub': [{'type': 'titi', 'aaa': True}]})
367+
check_parsing_result(parser, '--sub titi -a', {'sub': {'titi': {'aaa': True}}})
368368

369369

370370
def test_parse_empty_string_default_value():
@@ -404,25 +404,24 @@ def test_component_subparser_normal_token_list(component_subparser):
404404
test component_subparser, parse a token list which contain only subparser
405405
argument [('a', '')].
406406
407-
must return return a dictionary {'type': 'test', 'a':''} as parse result
407+
must return return a dictionary {'a':''} as parse result
408408
and a empty token list
409409
410410
"""
411-
assert component_subparser.subparse([('a', '')]) == ([], {'type': 'test',
412-
'a': None})
411+
assert component_subparser.subparse([('a', '')]) == ([], {'a': None})
413412

414413

415414
def test_component_subparser_full_token_list(component_subparser):
416415
"""
417416
test component_subparser, parse a token list which contain subparser
418417
argument and arguments from other parser[('a', ''), ('b', '')].
419418
420-
must return return a dictionary {'type': 'test', 'a':''} as parse result
419+
must return return a dictionary {'a':''} as parse result
421420
and a token list that contains the unparsed arguments : [('b', '')].
422421
423422
"""
424423
assert component_subparser.subparse([('a', ''), ('b', '')]) == ([('b', '')],
425-
{'type': 'test', 'a': None})
424+
{'a': None})
426425

427426

428427
def test_subparser_empty_token_list_default_value():

0 commit comments

Comments
 (0)