Skip to content

Commit 13ba028

Browse files
committed
feat: replace SimpleOption by a ConfigArgParser wrapper class
1 parent 5869c21 commit 13ba028

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lib/vsc/utils/script_tools.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
import os
3535
import sys
3636

37+
from configargparse import ConfigArgParser
3738
from copy import deepcopy
3839

3940
import logging
4041
from vsc.utils import fancylogger
4142
from vsc.utils.availability import proceed_on_ha_service
42-
from vsc.utils.generaloption import SimpleOption
43+
from vsc.utils.generaloption import SimpleOption as GOSimpleOption
4344
from vsc.utils.lock import lock_or_bork, release_or_bork, LOCKFILE_DIR, LOCKFILE_FILENAME_TEMPLATE
4445
from vsc.utils.nagios import (
4546
SimpleNagios, NAGIOS_CACHE_DIR, NAGIOS_CACHE_FILENAME_TEMPLATE, exit_from_errorcode,
@@ -58,6 +59,7 @@
5859
MAX_RTT = 2 * MAX_DELTA + 1
5960

6061

62+
6163
def _script_name(full_name):
6264
"""Return the script name without .py extension if any. This assumes that the script name does not contain a
6365
dot in case of lacking an extension.
@@ -102,7 +104,26 @@ def _merge_options(options):
102104
return opts
103105

104106

105-
class ExtendedSimpleOption(SimpleOption):
107+
class ConfigOption:
108+
"""
109+
Allow using ConfigArgParser instead of GeneralOption but with the same
110+
options-specifying syntax
111+
"""
112+
113+
def __init__(self, options, config_files=None):
114+
self.parser = ConfigArgParser(auto_env_var_prefix='')
115+
116+
if config_files:
117+
self.parser.config_file_parser(config_files)
118+
119+
for option, (help_str, type_, action, default_value) in options.items():
120+
self.parser.add_argument(f'--{option}', help=help_str, type=type_, action=action, default=default_value)
121+
122+
self.options = self.parser.parse_args()
123+
124+
125+
126+
class ExtendedSimpleOption(ConfigOption):
106127
"""
107128
Extends the SimpleOption class to allow other checks to occur at script prologue and epilogue.
108129
@@ -227,6 +248,8 @@ def critical_exception_handler(self, tp, value, traceback):
227248
self.critical(message)
228249

229250

251+
252+
230253
class CLI:
231254
"""
232255
Base class to implement cli tools that require timestamps, nagios checks, etc.

0 commit comments

Comments
 (0)