From ee9dea6cbf9879208ca88786e7f3a944d479e9ed Mon Sep 17 00:00:00 2001 From: Jan Fajerski Date: Thu, 16 Apr 2020 11:49:31 +0200 Subject: [PATCH] pybing/mgr/mgr_module: allow keyword arguments If a mgr module encounters an argument values containing a '=' parsing switches to kwargs style parsing independent of arg position. If a non-kw argument is encountered after the first kw argument an EINVAL error is returned. Signed-off-by: Jan Fajerski --- src/pybind/mgr/mgr_module.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 8b4c3bb82c9e5..6cdff082301ab 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -307,10 +307,21 @@ class CLICommand(object): def call(self, mgr, cmd_dict, inbuf): kwargs = {} + kwargs_switch = False for a, d in self.args_dict.items(): if 'req' in d and d['req'] == "false" and a not in cmd_dict: continue - kwargs[a.replace("-", "_")] = cmd_dict[a] + if kwargs_switch or (isinstance(cmd_dict[a], str) and '=' in cmd_dict[a]): + mgr.log.debug('found kwarg, assuming all following args are kw style') + kwargs_switch = True + try: + k, arg = cmd_dict[a].split('=') + except ValueError as e: + mgr.log.error('found positional arg after switching to kwarg parsing') + return -errno.EINVAL, '', 'Error EINVAL: postitional arg not allowed after kwarg' + kwargs[k.replace("-", "_")] = arg + else: + kwargs[a.replace("-", "_")] = cmd_dict[a] if inbuf: kwargs['inbuf'] = inbuf assert self.func -- 2.39.5