summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCole Robinson <crobinso@redhat.com>2016-06-14 13:54:37 -0400
committerCole Robinson <crobinso@redhat.com>2016-06-14 14:08:50 -0400
commitdc49d46f623cdb945f4af20326007d53ba96c116 (patch)
tree3a4329067d6aa6869d6ea40bef5a033cf69540cc
parent8532dacde027a29530b9fedda766ecc86682973f (diff)
downloadvirt-manager-dc49d46f623cdb945f4af20326007d53ba96c116.tar.gz
cli: Separate out can_comma handling
Clarify things by taking it out of the main parse loop
-rw-r--r--virtinst/cli.py63
1 files changed, 33 insertions, 30 deletions
diff --git a/virtinst/cli.py b/virtinst/cli.py
index e6dbf6c7..8f0b8225 100644
--- a/virtinst/cli.py
+++ b/virtinst/cli.py
@@ -949,56 +949,60 @@ def _parse_optstr_to_dict(optstr, virtargs, remove_first):
So for --disk path=foo,size=5, optstr is 'path=foo,size=5', and
we return {"path": "foo", "size": "5"}
"""
- optsdict = collections.OrderedDict()
+ optdict = collections.OrderedDict()
opttuples = parse_optstr_tuples(optstr)
def _add_opt(virtarg, cliname, val):
- if (cliname not in optsdict and
- virtarg and
+ if (cliname not in optdict and
virtarg.is_list):
- optsdict[cliname] = []
+ optdict[cliname] = []
- if type(optsdict.get(cliname)) is list:
- optsdict[cliname].append(val)
+ if type(optdict.get(cliname)) is list:
+ optdict[cliname].append(val)
else:
- optsdict[cliname] = val
+ optdict[cliname] = val
def _lookup_virtarg(cliname):
for virtarg in virtargs:
if virtarg.match_name(cliname):
return virtarg
+ def _consume_comma_arg(commaopt):
+ while opttuples:
+ cliname, val = opttuples[0]
+ if _lookup_virtarg(cliname):
+ # Next tuple is for an actual virtarg
+ break
+
+ # Next tuple is a continuation of the comma argument,
+ # sum it up
+ opttuples.pop(0)
+ commaopt[1] += "," + cliname
+ if val:
+ commaopt[1] += "=" + val
+
+ return commaopt
+
# Splice in remove_first names upfront
- remove_first = util.listify(remove_first)[:]
for idx, (cliname, val) in enumerate(opttuples):
if val is not None or not remove_first:
break
opttuples[idx] = (remove_first.pop(0), cliname)
- commaopt = []
- virtarg = None
- for cliname, val in opttuples:
+ while opttuples:
+ cliname, val = opttuples.pop(0)
virtarg = _lookup_virtarg(cliname)
- if commaopt:
- if not virtarg:
- commaopt[1] += "," + cliname
- if val:
- commaopt[1] += "=" + val
- continue
-
- _add_opt(virtarg, commaopt[0], commaopt[1])
- commaopt = []
-
- if (virtarg and virtarg.can_comma):
- commaopt = [cliname, val]
+ if not virtarg:
+ optdict[cliname] = val
continue
- _add_opt(virtarg, cliname, val)
-
- if commaopt:
- _add_opt(virtarg, commaopt[0], commaopt[1])
+ if virtarg.can_comma:
+ commaopt = _consume_comma_arg([cliname, val])
+ _add_opt(virtarg, commaopt[0], commaopt[1])
+ else:
+ _add_opt(virtarg, cliname, val)
- return optsdict
+ return optdict
class VirtCLIParser(object):
@@ -1061,8 +1065,7 @@ class VirtCLIParser(object):
self.guest = guest
self.optstr = optstr
self.optdict = _parse_optstr_to_dict(self.optstr,
- self._virtargs,
- self.remove_first)
+ self._virtargs, util.listify(self.remove_first)[:])
def _clearxml_cb(self, inst, val, virtarg):
"""