Use 'KeyValueAppendAction' from osc-lib
Does what it says on the tin. This action was added to osc-lib in change If73cab759fa09bddf1ff519923c5972c3b2052b1. Change-Id: I51efaa096bb26e297d99634c5d9cca34c0919074 Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
@@ -201,36 +201,6 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True):
|
|||||||
return info
|
return info
|
||||||
|
|
||||||
|
|
||||||
# TODO(stephenfin): Migrate this to osc-lib
|
|
||||||
class KeyValueAppendAction(argparse.Action):
|
|
||||||
"""A custom action to parse arguments as key=value pairs
|
|
||||||
|
|
||||||
Ensures that ``dest`` is a dict and values are lists of strings.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __call__(self, parser, namespace, values, option_string=None):
|
|
||||||
# Make sure we have an empty dict rather than None
|
|
||||||
if getattr(namespace, self.dest, None) is None:
|
|
||||||
setattr(namespace, self.dest, {})
|
|
||||||
|
|
||||||
# Add value if an assignment else remove it
|
|
||||||
if '=' in values:
|
|
||||||
key, value = values.split('=', 1)
|
|
||||||
# NOTE(qtang): Prevent null key setting in property
|
|
||||||
if '' == key:
|
|
||||||
msg = _("Property key must be specified: %s")
|
|
||||||
raise argparse.ArgumentTypeError(msg % str(values))
|
|
||||||
|
|
||||||
dest = getattr(namespace, self.dest)
|
|
||||||
if key in dest:
|
|
||||||
dest[key].append(value)
|
|
||||||
else:
|
|
||||||
dest[key] = [value]
|
|
||||||
else:
|
|
||||||
msg = _("Expected 'key=value' type, but got: %s")
|
|
||||||
raise argparse.ArgumentTypeError(msg % str(values))
|
|
||||||
|
|
||||||
|
|
||||||
class AddFixedIP(command.Command):
|
class AddFixedIP(command.Command):
|
||||||
_description = _("Add fixed IP address to server")
|
_description = _("Add fixed IP address to server")
|
||||||
|
|
||||||
@@ -719,7 +689,7 @@ class CreateServer(command.ShowOne):
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--hint',
|
'--hint',
|
||||||
metavar='<key=value>',
|
metavar='<key=value>',
|
||||||
action=KeyValueAppendAction,
|
action=parseractions.KeyValueAppendAction,
|
||||||
default={},
|
default={},
|
||||||
help=_('Hints for the scheduler (optional extension)'),
|
help=_('Hints for the scheduler (optional extension)'),
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user