mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-28 04:24:26 +01:00
cli_function: handle nargs="*" in config file
nargs="*" leads to default [] and not None
This commit is contained in:
@@ -71,6 +71,7 @@ class _Argument:
|
|||||||
self.longname = longname
|
self.longname = longname
|
||||||
self.key = key
|
self.key = key
|
||||||
self.is_option = is_option
|
self.is_option = is_option
|
||||||
|
self.nargs = nargs
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.args) + ' ' + str(self.kwargs)
|
return str(self.args) + ' ' + str(self.kwargs)
|
||||||
@@ -158,7 +159,11 @@ class CliFunction:
|
|||||||
for key in config_configs:
|
for key in config_configs:
|
||||||
if key not in self._arguments:
|
if key not in self._arguments:
|
||||||
raise Exception('Unknown key in config file: ' + key)
|
raise Exception('Unknown key in config file: ' + key)
|
||||||
if (not key in args_with_defaults) or args_with_defaults[key] is None:
|
if (
|
||||||
|
not key in args_with_defaults or
|
||||||
|
args_with_defaults[key] is None or
|
||||||
|
self._arguments[key].nargs == '*' and args_with_defaults[key] == []
|
||||||
|
):
|
||||||
args_with_defaults[key] = config_configs[key]
|
args_with_defaults[key] = config_configs[key]
|
||||||
# Add missing args from hard-coded defaults.
|
# Add missing args from hard-coded defaults.
|
||||||
for key in self._arguments:
|
for key in self._arguments:
|
||||||
@@ -364,6 +369,10 @@ amazing function!
|
|||||||
# Pick another config file.
|
# Pick another config file.
|
||||||
assert one_cli_function.cli(['--config-file', 'cli_function_test_config_2.py', '1'])['bool_cli'] is False
|
assert one_cli_function.cli(['--config-file', 'cli_function_test_config_2.py', '1'])['bool_cli'] is False
|
||||||
|
|
||||||
|
# Extra config file for '*'.
|
||||||
|
assert one_cli_function.cli(['--config-file', 'cli_function_test_config_2.py', '1', '2', '3', '4'])['args_star'] == ['3', '4']
|
||||||
|
assert one_cli_function.cli(['--config-file', 'cli_function_test_config_2.py', '1', '2'])['args_star'] == ['asdf', 'qwer']
|
||||||
|
|
||||||
# get_cli
|
# get_cli
|
||||||
assert one_cli_function.get_cli(pos_mandatory=1, asdf='B') == [('--asdf', 'B'), ('--bool-cli',), ('1',)]
|
assert one_cli_function.get_cli(pos_mandatory=1, asdf='B') == [('--asdf', 'B'), ('--bool-cli',), ('1',)]
|
||||||
assert one_cli_function.get_cli(pos_mandatory=1, asdf='B', qwer='R') == [('--asdf', 'B'), ('--bool-cli',), ('--qwer', 'R'), ('1',)]
|
assert one_cli_function.get_cli(pos_mandatory=1, asdf='B', qwer='R') == [('--asdf', 'B'), ('--bool-cli',), ('--qwer', 'R'), ('1',)]
|
||||||
|
|||||||
@@ -3,3 +3,4 @@ def set_args(args):
|
|||||||
:type args: Dict[str, Any]
|
:type args: Dict[str, Any]
|
||||||
'''
|
'''
|
||||||
args['bool_cli'] = False
|
args['bool_cli'] = False
|
||||||
|
args['args_star'] = ['asdf', 'qwer']
|
||||||
|
|||||||
Reference in New Issue
Block a user