cli: Add function cli_flag_name and use it

It can convert the underscore to minus for cli_arg_name in help
output or error message.

Signed-off-by: Lin Ma <lma@suse.com>
This commit is contained in:
Lin Ma 2019-01-14 17:21:33 +08:00 committed by Cole Robinson
parent 7228867480
commit bbe4866127
2 changed files with 15 additions and 11 deletions

View File

@ -167,7 +167,7 @@ def check_xmlopt_collision(options):
if len(collisions) != 1:
fail(_("Only one change operation may be specified "
"(conflicting options %s)") %
["--" + c.cli_arg_name for c in collisions])
[c.cli_flag_name() for c in collisions])
return collisions[0]

View File

@ -501,7 +501,7 @@ def autocomplete(parser):
import argcomplete
parsernames = ["--" + pclass.cli_arg_name for pclass in
parsernames = [pclass.cli_flag_name() for pclass in
_get_completer_parsers()]
# pylint: disable=protected-access
for action in parser._actions:
@ -1198,12 +1198,16 @@ class VirtCLIParser(metaclass=InitClass):
None, "clearxml", cb=cls._clearxml_cb, is_onoff=True)]
cls._virtargs.append(_VirtCLIArgumentStatic(*args, **kwargs))
@classmethod
def cli_flag_name(cls):
return "--" + cls.cli_arg_name.replace("_", "-")
@classmethod
def print_introspection(cls):
"""
Print out all _param names, triggered via ex. --disk help
"""
print("--%s options:" % cls.cli_arg_name)
print("%s options:" % cls.cli_flag_name())
for arg in sorted(cls._virtargs, key=lambda p: p.cliname):
print(" %s" % arg.cliname)
print("")
@ -1249,8 +1253,8 @@ class VirtCLIParser(metaclass=InitClass):
Callback that handles virt-xml clearxml=yes|no magic
"""
if not self.propname:
raise RuntimeError("Don't know how to clearxml --%s" %
self.cli_arg_name)
raise RuntimeError("Don't know how to clearxml %s" %
self.cli_flag_name())
if val is not True:
return
@ -1314,8 +1318,8 @@ class VirtCLIParser(metaclass=InitClass):
passed an invalid argument such as --disk idontexist=foo
"""
if optdict:
fail(_("Unknown --%s options: %s") %
(self.cli_arg_name, list(optdict.keys())))
fail(_("Unknown %s options: %s") %
(self.cli_flag_name(), list(optdict.keys())))
def _parse(self, inst):
"""
@ -1369,8 +1373,8 @@ class VirtCLIParser(metaclass=InitClass):
except Exception as e:
logging.debug("Exception parsing inst=%s optstr=%s",
inst, self.optstr, exc_info=True)
fail(_("Error: --%(cli_arg_name)s %(options)s: %(err)s") %
{"cli_arg_name": self.cli_arg_name,
fail(_("Error: %(cli_flag_name)s %(options)s: %(err)s") %
{"cli_flag_name": self.cli_flag_name(),
"options": self.optstr, "err": str(e)})
return ret
@ -1400,8 +1404,8 @@ class VirtCLIParser(metaclass=InitClass):
except Exception as e:
logging.debug("Exception parsing inst=%s optstr=%s",
inst, self.optstr, exc_info=True)
fail(_("Error: --%(cli_arg_name)s %(options)s: %(err)s") %
{"cli_arg_name": self.cli_arg_name,
fail(_("Error: %(cli_flag_name)s %(options)s: %(err)s") %
{"cli_flag_name": self.cli_flag_name(),
"options": self.optstr, "err": str(e)})
return ret