Mercurial > hg > lxmldump
comparison lxmldump.py @ 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 |
comparison
equal
deleted
inserted
replaced
66:6083f11c80bc | 67:9833df4573a7 |
---|---|
10 ### | 10 ### |
11 ### Python 3.7+ required! | 11 ### Python 3.7+ required! |
12 ### | 12 ### |
13 import sys | 13 import sys |
14 import signal | 14 import signal |
15 import functools | |
15 import re | 16 import re |
16 import xml.etree.ElementTree as xmlET | 17 import xml.etree.ElementTree as xmlET |
17 import unicodedata | 18 import unicodedata |
18 import argparse | 19 import argparse |
19 | 20 |
207 def pkk_signal_handler(signal, frame): | 208 def pkk_signal_handler(signal, frame): |
208 print(u"\nQuitting due to SIGINT / Ctrl+C!") | 209 print(u"\nQuitting due to SIGINT / Ctrl+C!") |
209 sys.exit(1) | 210 sys.exit(1) |
210 | 211 |
211 | 212 |
213 def pkk_arg_range(vstr, vmin, vmax): | |
214 value = int(vstr) | |
215 if value < vmin or value > vmax: | |
216 raise argparse.ArgumentTypeError("value {} not in range {}-{}".format(value, vmin, vmax)) | |
217 else: | |
218 return value | |
219 | |
220 | |
212 class pkk_set_mode(argparse.Action): | 221 class pkk_set_mode(argparse.Action): |
213 def __call__(self, parser, namespace, value, option_string=None): | 222 def __call__(self, parser, namespace, value, option_string=None): |
214 lvalue = value.strip().lower() | 223 lvalue = value.strip().lower() |
215 for mode in pkk_modes_list: | 224 for mode in pkk_modes_list: |
216 if pkk_modes_list[mode] == lvalue: | 225 if pkk_modes_list[mode] == lvalue: |
496 action="store_true", | 505 action="store_true", |
497 help="annotate strings") | 506 help="annotate strings") |
498 | 507 |
499 optparser.add_argument("-i", "--indent", | 508 optparser.add_argument("-i", "--indent", |
500 dest="indent", | 509 dest="indent", |
501 type=int, choices=range(0, 32), default=4, | 510 type=functools.partial(pkk_arg_range, vmin=0, vmax=32), default=4, |
502 metavar="N", | 511 metavar="N", |
503 help='set indentation level (default: %(default)s)') | 512 help='set indentation width (default: %(default)s)') |
504 | 513 |
505 optparser.add_argument("-p", "--debug", | 514 optparser.add_argument("-p", "--debug", |
506 dest="debug", | 515 dest="debug", |
507 action="store_true", | 516 action="store_true", |
508 help=argparse.SUPPRESS) | 517 help=argparse.SUPPRESS) |