Commit 552c8dff authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

cli: Merge ikv_option and keyval_option into cli_option



Merging them gets rid of two public functions in cli.py and optparse explicitly
supports more than one option type per class.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent ed2fe731
......@@ -44,7 +44,7 @@ from optparse import (OptionParser, make_option, TitledHelpFormatter,
__all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain",
"SubmitOpCode", "GetClient",
"cli_option", "ikv_option", "keyval_option",
"cli_option",
"GenerateTable", "AskUser",
"ARGS_NONE", "ARGS_FIXED", "ARGS_ATLEAST", "ARGS_ANY", "ARGS_ONE",
"USEUNITS_OPT", "FIELDS_OPT", "FORCE_OPT", "SUBMIT_OPT",
......@@ -233,15 +233,6 @@ def check_unit(option, opt, value):
raise OptionValueError("option %s: %s" % (opt, err))
class CliOption(Option):
"""Custom option class for optparse.
"""
TYPES = Option.TYPES + ("unit",)
TYPE_CHECKER = copy.copy(Option.TYPE_CHECKER)
TYPE_CHECKER["unit"] = check_unit
def _SplitKeyVal(opt, data):
"""Convert a KeyVal string into a dict.
......@@ -280,7 +271,10 @@ def _SplitKeyVal(opt, data):
def check_ident_key_val(option, opt, value):
"""Custom parser for the IdentKeyVal option type.
"""Custom parser for ident:key=val,key=val options.
This will store the parsed values as a tuple (ident, {key: val}). As such,
multiple uses of this option via action=append is possible.
"""
if ":" not in value:
......@@ -304,40 +298,32 @@ def check_ident_key_val(option, opt, value):
return retval
class IdentKeyValOption(Option):
"""Custom option class for ident:key=val,key=val options.
This will store the parsed values as a tuple (ident, {key: val}). As
such, multiple uses of this option via action=append is possible.
"""
TYPES = Option.TYPES + ("identkeyval",)
TYPE_CHECKER = copy.copy(Option.TYPE_CHECKER)
TYPE_CHECKER["identkeyval"] = check_ident_key_val
def check_key_val(option, opt, value):
"""Custom parser for the KeyVal option type.
"""Custom parser class for key=val,key=val options.
This will store the parsed values as a dict {key: val}.
"""
return _SplitKeyVal(opt, value)
class KeyValOption(Option):
"""Custom option class for key=val,key=val options.
This will store the parsed values as a dict {key: val}.
class CliOption(Option):
"""Custom option class for optparse.
"""
TYPES = Option.TYPES + ("keyval",)
TYPE_CHECKER = copy.copy(Option.TYPE_CHECKER)
TYPES = Option.TYPES + (
"identkeyval",
"keyval",
"unit",
)
TYPE_CHECKER = Option.TYPE_CHECKER.copy()
TYPE_CHECKER["identkeyval"] = check_ident_key_val
TYPE_CHECKER["keyval"] = check_key_val
TYPE_CHECKER["unit"] = check_unit
# optparse.py sets make_option, so we do it for our own option class, too
cli_option = CliOption
ikv_option = IdentKeyValOption
keyval_option = KeyValOption
def _ParseArgs(argv, commands, aliases):
......
......@@ -214,13 +214,13 @@ import_opts = [
make_option("-n", "--node", dest="node",
help="Target node and optional secondary node",
metavar="<pnode>[:<snode>]"),
keyval_option("-B", "--backend", dest="beparams",
type="keyval", default={},
help="Backend parameters"),
cli_option("-B", "--backend", dest="beparams",
type="keyval", default={},
help="Backend parameters"),
make_option("-t", "--disk-template", dest="disk_template",
help="Custom disk setup (diskless, file, plain, drbd)",
default=None, metavar="TEMPL"),
ikv_option("--disk", help="Disk information",
cli_option("--disk", help="Disk information",
default=[], dest="disks",
action="append",
type="identkeyval"),
......@@ -228,7 +228,7 @@ import_opts = [
" single-disk configuration, when not using the --disk option,"
" in MiB unless a suffix is used",
default=None, type="unit", metavar="<size>"),
ikv_option("--net", help="NIC information",
cli_option("--net", help="NIC information",
default=[], dest="nics",
action="append",
type="identkeyval"),
......@@ -252,7 +252,7 @@ import_opts = [
metavar="<DIR>"),
make_option("--file-driver", dest="file_driver", help="Driver to use"
" for image files", default="loop", metavar="<DRIVER>"),
ikv_option("-H", "--hypervisor", dest="hypervisor",
cli_option("-H", "--hypervisor", dest="hypervisor",
help="Hypervisor and hypervisor options, in the format"
" hypervisor:option=value,option=value,...", default=None,
type="identkeyval"),
......
......@@ -583,19 +583,19 @@ commands = {
help="Comma-separated list of hypervisors",
type="string",
default=constants.DEFAULT_ENABLED_HYPERVISOR),
ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
cli_option("-H", "--hypervisor-parameters", dest="hvparams",
help="Hypervisor and hypervisor options, in the"
" format"
" hypervisor:option=value,option=value,...",
default=[],
action="append",
type="identkeyval"),
keyval_option("-B", "--backend-parameters", dest="beparams",
type="keyval", default={},
help="Backend parameters"),
keyval_option("-N", "--nic-parameters", dest="nicparams",
type="keyval", default={},
help="NIC parameters"),
cli_option("-B", "--backend-parameters", dest="beparams",
type="keyval", default={},
help="Backend parameters"),
cli_option("-N", "--nic-parameters", dest="nicparams",
type="keyval", default={},
help="NIC parameters"),
make_option("-C", "--candidate-pool-size",
default=constants.MASTER_POOL_SIZE_DEFAULT,
help="Set the candidate pool size",
......@@ -672,19 +672,19 @@ commands = {
make_option("--enabled-hypervisors", dest="enabled_hypervisors",
help="Comma-separated list of hypervisors",
type="string", default=None),
ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
cli_option("-H", "--hypervisor-parameters", dest="hvparams",
help="Hypervisor and hypervisor options, in the"
" format"
" hypervisor:option=value,option=value,...",
default=[],
action="append",
type="identkeyval"),
keyval_option("-B", "--backend-parameters", dest="beparams",
type="keyval", default={},
help="Backend parameters"),
keyval_option("-N", "--nic-parameters", dest="nicparams",
type="keyval", default={},
help="NIC parameters"),
cli_option("-B", "--backend-parameters", dest="beparams",
type="keyval", default={},
help="Backend parameters"),
cli_option("-N", "--nic-parameters", dest="nicparams",
type="keyval", default={},
help="NIC parameters"),
make_option("-C", "--candidate-pool-size", default=None,
help="Set the candidate pool size",
dest="candidate_pool_size", type="int"),
......
......@@ -1345,9 +1345,9 @@ add_opts = [
help="Target node and optional secondary node",
metavar="<pnode>[:<snode>]"),
os_opt,
keyval_option("-B", "--backend", dest="beparams",
type="keyval", default={},
help="Backend parameters"),
cli_option("-B", "--backend", dest="beparams",
type="keyval", default={},
help="Backend parameters"),
make_option("-t", "--disk-template", dest="disk_template",
help="Custom disk setup (diskless, file, plain or drbd)",
default=None, metavar="TEMPL"),
......@@ -1355,11 +1355,11 @@ add_opts = [
" single-disk configuration, when not using the --disk option,"
" in MiB unless a suffix is used",
default=None, type="unit", metavar="<size>"),
ikv_option("--disk", help="Disk information",
cli_option("--disk", help="Disk information",
default=[], dest="disks",
action="append",
type="identkeyval"),
ikv_option("--net", help="NIC information",
cli_option("--net", help="NIC information",
default=[], dest="nics",
action="append",
type="identkeyval"),
......@@ -1382,7 +1382,7 @@ add_opts = [
make_option("-I", "--iallocator", metavar="<NAME>",
help="Select nodes for the instance automatically using the"
" <NAME> iallocator plugin", default=None, type="string"),
ikv_option("-H", "--hypervisor", dest="hypervisor",
cli_option("-H", "--hypervisor", dest="hypervisor",
help="Hypervisor and hypervisor options, in the format"
" hypervisor:option=value,option=value,...", default=None,
type="identkeyval"),
......@@ -1522,17 +1522,17 @@ commands = {
"Replaces all disks for the instance"),
'modify': (SetInstanceParams, ARGS_ONE,
[DEBUG_OPT, FORCE_OPT,
keyval_option("-H", "--hypervisor", type="keyval",
default={}, dest="hypervisor",
help="Change hypervisor parameters"),
keyval_option("-B", "--backend", type="keyval",
default={}, dest="beparams",
help="Change backend parameters"),
ikv_option("--disk", help="Disk changes",
cli_option("-H", "--hypervisor", type="keyval",
default={}, dest="hypervisor",
help="Change hypervisor parameters"),
cli_option("-B", "--backend", type="keyval",
default={}, dest="beparams",
help="Change backend parameters"),
cli_option("--disk", help="Disk changes",
default=[], dest="disks",
action="append",
type="identkeyval"),
ikv_option("--net", help="NIC changes",
cli_option("--net", help="NIC changes",
default=[], dest="nics",
action="append",
type="identkeyval"),
......@@ -1550,12 +1550,12 @@ commands = {
m_node_opt, m_pri_node_opt, m_sec_node_opt,
m_clust_opt, m_inst_opt,
SUBMIT_OPT,
keyval_option("-H", "--hypervisor", type="keyval",
default={}, dest="hvparams",
help="Temporary hypervisor parameters"),
keyval_option("-B", "--backend", type="keyval",
default={}, dest="beparams",
help="Temporary backend parameters"),
cli_option("-H", "--hypervisor", type="keyval",
default={}, dest="hvparams",
help="Temporary hypervisor parameters"),
cli_option("-B", "--backend", type="keyval",
default={}, dest="beparams",
help="Temporary backend parameters"),
],
"<instance>", "Starts an instance"),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment