config.BaseOption: add no_arg parameter

This commit is contained in:
Benjamin Renard 2021-11-06 15:14:06 +01:00
parent 73c19816f7
commit f8d78f6fe2

View file

@ -28,12 +28,13 @@ class BaseOption:
""" Base configuration option class """ """ Base configuration option class """
def __init__(self, config, section, name, default=None, comment=None, def __init__(self, config, section, name, default=None, comment=None,
arg=None, short_arg=None, arg_help=None): arg=None, short_arg=None, arg_help=None, no_arg=False):
self.config = config self.config = config
self.section = section self.section = section
self.name = name self.name = name
self.default = default self.default = default
self.comment = comment self.comment = comment
self.no_arg = no_arg
self.arg = arg self.arg = arg
self.short_arg = short_arg self.short_arg = short_arg
self.arg_help = arg_help if arg_help else comment self.arg_help = arg_help if arg_help else comment
@ -43,6 +44,7 @@ class BaseOption:
""" Check if option is defined in registered arguments parser options """ """ Check if option is defined in registered arguments parser options """
return ( return (
self.config.options self.config.options
and not self.no_arg
and self._from_options != self.default and self._from_options != self.default
) )
@ -51,7 +53,7 @@ class BaseOption:
""" Get option from arguments parser options """ """ Get option from arguments parser options """
value = ( value = (
getattr(self.config.options, self.parser_dest) getattr(self.config.options, self.parser_dest)
if self.config.options else None if self.config.options and not self.no_arg else None
) )
log.debug( log.debug(
'_from_options(%s, %s) = %s', '_from_options(%s, %s) = %s',
@ -139,6 +141,8 @@ class BaseOption:
def add_option_to_parser(self, section_opts): def add_option_to_parser(self, section_opts):
""" Add option to arguments parser """ """ Add option to arguments parser """
if self.no_arg:
return
args = [self.parser_argument_name] args = [self.parser_argument_name]
if self.short_arg: if self.short_arg:
args.append(self.short_arg) args.append(self.short_arg)