]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybing/mgr/mgr_module: allow keyword arguments
authorJan Fajerski <jfajerski@suse.com>
Thu, 16 Apr 2020 09:49:31 +0000 (11:49 +0200)
committerJan Fajerski <jfajerski@suse.com>
Thu, 27 Aug 2020 13:55:45 +0000 (15:55 +0200)
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 <jfajerski@suse.com>
src/pybind/mgr/mgr_module.py

index 8b4c3bb82c9e5d4051662d060e3c6fff6f880780..6cdff082301ab74b5c3450cb7d52ea3606a60eeb 100644 (file)
@@ -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