summaryrefslogtreecommitdiff
path: root/openstackclient/common/parseractions.py
diff options
context:
space:
mode:
Diffstat (limited to 'openstackclient/common/parseractions.py')
-rw-r--r--openstackclient/common/parseractions.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/openstackclient/common/parseractions.py b/openstackclient/common/parseractions.py
index 8f6008e2..fd90369a 100644
--- a/openstackclient/common/parseractions.py
+++ b/openstackclient/common/parseractions.py
@@ -65,3 +65,18 @@ class RangeAction(argparse.Action):
# Too many values
msg = "Invalid range, too many values"
raise argparse.ArgumentError(self, msg)
+
+
+class NonNegativeAction(argparse.Action):
+ """A custom action to check whether the value is non-negative or not
+
+ Ensures the value is >= 0.
+ """
+
+ def __call__(self, parser, namespace, values, option_string=None):
+ try:
+ assert(int(values) >= 0)
+ setattr(namespace, self.dest, values)
+ except Exception:
+ msg = "%s expected a non-negative integer" % (str(option_string))
+ raise argparse.ArgumentTypeError(self, msg)