summaryrefslogtreecommitdiff
path: root/openstackclient/common
diff options
context:
space:
mode:
Diffstat (limited to 'openstackclient/common')
-rw-r--r--openstackclient/common/parseractions.py24
-rw-r--r--openstackclient/common/utils.py5
2 files changed, 28 insertions, 1 deletions
diff --git a/openstackclient/common/parseractions.py b/openstackclient/common/parseractions.py
index f111c264..644472d8 100644
--- a/openstackclient/common/parseractions.py
+++ b/openstackclient/common/parseractions.py
@@ -32,3 +32,27 @@ class KeyValueAction(argparse.Action):
getattr(namespace, self.dest, {}).update([values.split('=', 1)])
else:
getattr(namespace, self.dest, {}).pop(values, None)
+
+
+class RangeAction(argparse.Action):
+ """A custom action to parse a single value or a range of values."""
+ def __call__(self, parser, namespace, values, option_string=None):
+ range = values.split(':')
+ if len(range) == 0:
+ # Nothing passed, return a zero default
+ setattr(namespace, self.dest, (0, 0))
+ elif len(range) == 1:
+ # Only a single value is present
+ setattr(namespace, self.dest, (int(range[0]), int(range[0])))
+ elif len(range) == 2:
+ # Range of two values
+ if int(range[0]) <= int(range[1]):
+ setattr(namespace, self.dest, (int(range[0]), int(range[1])))
+ else:
+ msg = "Invalid range, %s is not less than %s" % \
+ (range[0], range[1])
+ raise argparse.ArgumentError(self, msg)
+ else:
+ # Too many values
+ msg = "Invalid range, too many values"
+ raise argparse.ArgumentError(self, msg)
diff --git a/openstackclient/common/utils.py b/openstackclient/common/utils.py
index fd504ea1..4d2afd15 100644
--- a/openstackclient/common/utils.py
+++ b/openstackclient/common/utils.py
@@ -16,11 +16,13 @@
"""Common client utilities"""
import os
+import six
import sys
import time
import uuid
from openstackclient.common import exceptions
+from openstackclient.openstack.common import strutils
def find_resource(manager, name_or_id):
@@ -84,7 +86,8 @@ def format_dict(data):
output = ""
for s in data:
- output = output + s + "='" + data[s] + "', "
+ output = output + s + "='" + \
+ strutils.safe_encode(six.text_type(data[s])) + "', "
return output[:-2]