changeset 67:9833df4573a7

Improve handling of value range commandline options.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 11 Oct 2021 07:19:02 +0300
parents 6083f11c80bc
children d53f46341864
files lxmldump.py
diffstat 1 files changed, 11 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/lxmldump.py	Mon Jul 12 23:01:18 2021 +0300
+++ b/lxmldump.py	Mon Oct 11 07:19:02 2021 +0300
@@ -12,6 +12,7 @@
 ###
 import sys
 import signal
+import functools
 import re
 import xml.etree.ElementTree as xmlET
 import unicodedata
@@ -209,6 +210,14 @@
     sys.exit(1)
 
 
+def pkk_arg_range(vstr, vmin, vmax):
+    value = int(vstr)
+    if value < vmin or value > vmax:
+        raise argparse.ArgumentTypeError("value {} not in range {}-{}".format(value, vmin, vmax))
+    else:
+        return value
+
+
 class pkk_set_mode(argparse.Action):
     def __call__(self, parser, namespace, value, option_string=None):
         lvalue = value.strip().lower()
@@ -498,9 +507,9 @@
 
 optparser.add_argument("-i", "--indent",
     dest="indent",
-    type=int, choices=range(0, 32), default=4,
+    type=functools.partial(pkk_arg_range, vmin=0, vmax=32), default=4,
     metavar="N",
-    help='set indentation level (default: %(default)s)')
+    help='set indentation width (default: %(default)s)')
 
 optparser.add_argument("-p", "--debug",
     dest="debug",