diff --git a/lib/vsc/utils/script_tools.py b/lib/vsc/utils/script_tools.py index 65ada20..fc704df 100644 --- a/lib/vsc/utils/script_tools.py +++ b/lib/vsc/utils/script_tools.py @@ -34,12 +34,12 @@ import os import sys +from configargparse import ArgParser from copy import deepcopy import logging from vsc.utils import fancylogger from vsc.utils.availability import proceed_on_ha_service -from vsc.utils.generaloption import SimpleOption from vsc.utils.lock import lock_or_bork, release_or_bork, LOCKFILE_DIR, LOCKFILE_FILENAME_TEMPLATE from vsc.utils.nagios import ( SimpleNagios, NAGIOS_CACHE_DIR, NAGIOS_CACHE_FILENAME_TEMPLATE, exit_from_errorcode, @@ -58,6 +58,7 @@ MAX_RTT = 2 * MAX_DELTA + 1 + def _script_name(full_name): """Return the script name without .py extension if any. This assumes that the script name does not contain a dot in case of lacking an extension. @@ -102,7 +103,25 @@ def _merge_options(options): return opts -class ExtendedSimpleOption(SimpleOption): +class ConfigOption: + """ + Allow using configargparse.ArgParser instead of GeneralOption but with the same + options-specifying syntax + """ + + def __init__(self, options, config_files=None): + self.parser = ArgParser(auto_env_var_prefix='') + + if config_files: + self.parser.config_file_parser(config_files) + + for option, (help_str, type_, action, default_value) in options.items(): + self.parser.add(f'--{option}', help=help_str, type=type_, action=action, default=default_value) + + self.options = self.parser.parse_args() + + +class ExtendedSimpleOption(ConfigOption): """ Extends the SimpleOption class to allow other checks to occur at script prologue and epilogue. @@ -227,6 +246,8 @@ def critical_exception_handler(self, tp, value, traceback): self.critical(message) + + class CLI: """ Base class to implement cli tools that require timestamps, nagios checks, etc. diff --git a/setup.py b/setup.py index 8fe1c4e..0614ce8 100755 --- a/setup.py +++ b/setup.py @@ -38,10 +38,11 @@ 'lockfile >= 0.9.1', 'netifaces', 'jsonpickle', + 'configargparse', ] PACKAGE = { - 'version': '2.2.6', + 'version': '2.3.0', 'author': [ag, sdw], 'maintainer': [ag, sdw], 'excluded_pkgs_rpm': ['vsc', 'vsc.utils'], # vsc is default, vsc.utils is provided by vsc-base