From 44a674e87d4b9b61c3368fbbd7a70147f66a969f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Tue, 22 Jan 2019 00:00:00 +0000 Subject: [PATCH] cli_function: handle nargs="*" in config file nargs="*" leads to default [] and not None --- cli_function.py | 11 ++++++++++- cli_function_test_config_2.py | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cli_function.py b/cli_function.py index c6b7d51..54170c2 100755 --- a/cli_function.py +++ b/cli_function.py @@ -71,6 +71,7 @@ class _Argument: self.longname = longname self.key = key self.is_option = is_option + self.nargs = nargs def __str__(self): return str(self.args) + ' ' + str(self.kwargs) @@ -158,7 +159,11 @@ class CliFunction: for key in config_configs: if key not in self._arguments: 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] # Add missing args from hard-coded defaults. for key in self._arguments: @@ -364,6 +369,10 @@ amazing function! # Pick another config file. 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 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',)] diff --git a/cli_function_test_config_2.py b/cli_function_test_config_2.py index 71a4673..868c642 100644 --- a/cli_function_test_config_2.py +++ b/cli_function_test_config_2.py @@ -3,3 +3,4 @@ def set_args(args): :type args: Dict[str, Any] ''' args['bool_cli'] = False + args['args_star'] = ['asdf', 'qwer']