aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryn M. Reeves <bmr@redhat.com>2018-05-28 21:42:47 +0100
committerBryn M. Reeves <bmr@redhat.com>2018-06-20 18:01:21 +0100
commit2f0e9911814db669f97dd825eb5262480d1f27ea (patch)
tree648d5d4222dc33ce9399c952f77b8553dfe0ea43
parent833342e766888dbe11f4dc568f9d50192179df5f (diff)
downloadsos-2f0e9911814db669f97dd825eb5262480d1f27ea.tar.gz
[sos] move options-to-args formatting into SoSOptions.to_args()
Move the formatting of SoSOptions into sosreport command line arguments from SoSReport.list_presets() into SoSOptions.to_args(). This allows the function to be simplified since it has access to the raw arguments rather than their string representation. The method returns a list of string '--argument value' pairs, and suppresses null, default and True argument values. Signed-off-by: Bryn M. Reeves <bmr@redhat.com>
-rw-r--r--sos/__init__.py50
-rw-r--r--sos/sosreport.py41
2 files changed, 52 insertions, 39 deletions
diff --git a/sos/__init__.py b/sos/__init__.py
index 155c369d..de39a8d7 100644
--- a/sos/__init__.py
+++ b/sos/__init__.py
@@ -213,4 +213,54 @@ class SoSOptions(object):
odict[arg] = value
return odict
+ def to_args(self):
+ """Return command arguments for this object.
+
+ Return a list of the non-default options of this ``SoSOptions``
+ object in ``sosreport`` command line argument notation:
+
+ ``["--all-logs", "-vvv"]``
+
+ """
+ def has_value(name, value):
+ """ Test for non-null option values.
+ """
+ null_values = ("False", "None", "[]", '""', "''", "0")
+ if not value or value in null_values:
+ return False
+ if name in _arg_defaults:
+ if value == str(_arg_defaults[name]):
+ return False
+ return True
+
+ def filter_opt(name, value):
+ """ Filter out preset and null-valued options.
+ """
+ if name in ("add_preset", "del_preset", "desc", "note"):
+ return False
+ return has_value(name, value)
+
+ def argify(name, value):
+ """ Convert sos option notation to command line arguments.
+ """
+ # Handle --verbosity specially
+ if name.startswith("verbosity"):
+ arg = "-" + int(value) * "v"
+ return arg
+
+ name = name.replace("_", "-")
+
+ value = ",".join(value) if _is_seq(value) else value
+
+ if value is not True:
+ opt = "%s %s" % (name, value)
+ else:
+ opt = name
+
+ arg = "--" + opt if len(opt) > 1 else "-" + opt
+ return arg
+
+ opt_items = sorted(self.dict().items(), key=lambda x: x[0])
+ return [argify(n, v) for (n, v) in opt_items if filter_opt(n, v)]
+
# vim: set et ts=4 sw=4 :
diff --git a/sos/sosreport.py b/sos/sosreport.py
index 90231fab..4f0537a2 100644
--- a/sos/sosreport.py
+++ b/sos/sosreport.py
@@ -870,46 +870,9 @@ class SoSReport(object):
self.ui_log.info("%14s %s" % ("description:", preset.desc))
if preset.note:
self.ui_log.info("%14s %s" % ("note:", preset.note))
+
if self.opts.verbosity > 0:
- def filter_opt(opt):
- """ Filter out preset options.
- """
- opt = opt.split("=")[0]
- if opt in ("add_preset", "del_preset", "desc", "note"):
- return False
- return True
-
- def argify(opt):
- """ Convert sos option notation to command line arguments.
- """
- # Handle --verbosity specially
- if opt.startswith("verbosity"):
- (arg, value) = opt.split("=")
- arg = "-" + int(value) * "v"
- return arg
-
- # Convert "a_name=value" to "--a-name=value" and
- # "name=True" to "--name"
- arg = "--" + opt if len(opt) > 1 else "-" + opt
- arg = arg.replace("_", "-")
- arg = arg[:-len("=True")] if arg.endswith("=True") else arg
- return arg
-
- def has_value(opt):
- """ Test for null option values.
- """
- null_values = ("False", "None", "[]", '""', "''", "0")
- (opt_name, opt_value) = opt.split("=")
- if opt_name in _arg_defaults:
- if opt_value == str(_arg_defaults[opt_name]):
- return False
- if not opt_value or opt_value in null_values:
- return False
- return True
-
- opts = str(preset.opts).split()
- opts = [opt for opt in opts if filter_opt(opt)]
- args = [argify(opt) for opt in opts if has_value(opt)]
+ args = preset.opts.to_args()
options_str = "%14s " % "options:"
lines = _format_list(options_str, args, indent=True, sep=' ')
for line in lines: