diff options
author | Tim Harder <radhermit@gmail.com> | 2019-11-25 15:38:12 -0700 |
---|---|---|
committer | Tim Harder <radhermit@gmail.com> | 2019-11-25 15:38:12 -0700 |
commit | 59902cd999b66ccefde9fd15d118c00cb0fc7f54 (patch) | |
tree | cf79ce904f5dd9781ae8c5c3fe5ba68e35bb8d5a /src/snakeoil/cli | |
parent | cli.arghparse: ArgumentParser: add bind_pre_parse decorator support (diff) | |
download | snakeoil-59902cd999b66ccefde9fd15d118c00cb0fc7f54.tar.gz snakeoil-59902cd999b66ccefde9fd15d118c00cb0fc7f54.tar.bz2 snakeoil-59902cd999b66ccefde9fd15d118c00cb0fc7f54.zip |
cli.arghparse: add initial bounded_integer type
Diffstat (limited to 'src/snakeoil/cli')
-rw-r--r-- | src/snakeoil/cli/arghparse.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/snakeoil/cli/arghparse.py b/src/snakeoil/cli/arghparse.py index 9dfd106..4368123 100644 --- a/src/snakeoil/cli/arghparse.py +++ b/src/snakeoil/cli/arghparse.py @@ -1294,3 +1294,14 @@ def existent_dir(value): return osutils.abspath(value) except EnvironmentError as e: raise ValueError(f'while resolving path {value!r}, encountered error: {e}') from e + + +def bounded_integer(func, desc, x): + """Check if argument is an integer and matches defined bounds.""" + try: + n = int(x) + except ValueError: + raise argparse.ArgumentTypeError('invalid integer value') + if not func(n): + raise argparse.ArgumentTypeError(f'must be {desc}') + return n |