mirror of
https://github.com/openSUSE/osc.git
synced 2025-02-03 18:16:17 +01:00
cmdln: Handle multiple signatures of do_* handlers
This commit is contained in:
parent
9d11493f33
commit
4ee3df3bbc
29
osc/cmdln.py
29
osc/cmdln.py
@ -4,6 +4,7 @@ A modern, lightweight alternative to cmdln.py from https://github.com/trentm/cmd
|
||||
|
||||
|
||||
import argparse
|
||||
import inspect
|
||||
import sys
|
||||
|
||||
|
||||
@ -227,7 +228,33 @@ class Cmdln:
|
||||
# find the `do_*` function to call by its name
|
||||
cmd = self.cmd_map[self.options.command]
|
||||
# run the command with parsed args
|
||||
cmd(self.options.command, self.options, *self.args)
|
||||
|
||||
sig = inspect.signature(cmd)
|
||||
arg_names = list(sig.parameters.keys())
|
||||
if arg_names == ["subcmd", "opts"]:
|
||||
# positional args specified manually via @cmdln.option
|
||||
if self.args:
|
||||
self.argparser.error(f"unrecognized arguments: " + " ".join(self.args))
|
||||
cmd(self.options.command, self.options)
|
||||
elif arg_names == ["subcmd", "opts", "args"]:
|
||||
# positional args are the remaining (unrecognized) args
|
||||
cmd(self.options.command, self.options, *self.args)
|
||||
else:
|
||||
# positional args are the remaining (unrecongnized) args
|
||||
# and the do_* handler takes other arguments than "subcmd", "opts", "args"
|
||||
import warnings
|
||||
warnings.warn(
|
||||
f"do_{self.options.command}() handler has deprecated signature. "
|
||||
f"It takes the following args: {arg_names}, while it should be taking ['subcmd', 'opts'] "
|
||||
f"and handling positional arguments explicitly via @cmdln.option.",
|
||||
FutureWarning
|
||||
)
|
||||
try:
|
||||
cmd(self.options.command, self.options, *self.args)
|
||||
except TypeError as e:
|
||||
if e.args[0].startswith("do_"):
|
||||
sys.exit(str(e))
|
||||
raise
|
||||
|
||||
@alias("?")
|
||||
def do_help(self, subcmd, opts, *args):
|
||||
|
Loading…
Reference in New Issue
Block a user