config: Fix configuration interactive mode

This commit is contained in:
Benjamin Renard 2022-01-19 17:11:55 +01:00
parent 558da5c815
commit 0823353c0a

View file

@ -202,11 +202,11 @@ class BaseOption: # pylint: disable=too-many-instance-attributes
lines.append('') lines.append('')
return '\n'.join(lines) return '\n'.join(lines)
def _ask_value(self, prompt=None): def _ask_value(self, prompt=None, **kwargs):
""" Ask to user to enter value of this option and return it """ """ Ask to user to enter value of this option and return it """
if self.comment: if self.comment:
print('# ' + self.comment) print('# ' + self.comment)
default_value = self.get() default_value = kwargs.get('default_value', self.get())
if not prompt: if not prompt:
prompt = "%s: " % self.name prompt = "%s: " % self.name
if default_value is not None: if default_value is not None:
@ -261,7 +261,7 @@ class BooleanOption(BaseOption):
).lower().replace('_', '-') ).lower().replace('_', '-')
) )
def _ask_value(self, prompt=None): def _ask_value(self, prompt=None, **kwargs):
""" Ask to user to enter value of this option and return it """ """ Ask to user to enter value of this option and return it """
default_value = self.get() default_value = self.get()
prompt = "%s: " % self.name prompt = "%s: " % self.name
@ -270,12 +270,12 @@ class BooleanOption(BaseOption):
else: else:
prompt += '[y/N] ' prompt += '[y/N] '
while True: while True:
value = super()._ask_value(prompt).lower() value = super()._ask_value(prompt, **kwargs)
if value in ['', None]: if value in ['', None, default_value]:
return default_value return default_value
if value == 'y': if value.lower() == 'y':
return True return True
if value == 'n': if value.lower() == 'n':
return False return False
print('Invalid answer. Possible values: Y or N (case insensitive)') print('Invalid answer. Possible values: Y or N (case insensitive)')
@ -293,12 +293,12 @@ class FloatOption(BaseOption):
def parser_type(self): def parser_type(self):
return float return float
def _ask_value(self, prompt=None): def _ask_value(self, prompt=None, **kwargs):
""" Ask to user to enter value of this option and return it """ """ Ask to user to enter value of this option and return it """
default_value = self.get() default_value = self.get()
while True: while True:
value = super()._ask_value() value = super()._ask_value(prompt, **kwargs)
if value in ['', None]: if value in ['', None, default_value]:
return default_value return default_value
try: try:
return float(value) return float(value)
@ -323,12 +323,12 @@ class IntegerOption(BaseOption):
def parser_type(self): def parser_type(self):
return int return int
def _ask_value(self, prompt=None): def _ask_value(self, prompt=None, **kwargs):
""" Ask to user to enter value of this option and return it """ """ Ask to user to enter value of this option and return it """
default_value = self.get() default_value = kwargs.pop('default_value', self.get())
while True: while True:
value = super()._ask_value() value = super()._ask_value(prompt, default_value=default_value, **kwargs)
if value in ['', None]: if value in ['', None, default_value]:
return default_value return default_value
try: try:
return int(value) return int(value)
@ -399,11 +399,11 @@ class PasswordOption(StringOption):
value = self.keyring_value value = self.keyring_value
super().set(value) super().set(value)
def _ask_value(self, prompt=None): def _ask_value(self, prompt=None, **kwargs):
""" Ask to user to enter value of this option and return it """ """ Ask to user to enter value of this option and return it """
if self.comment: if self.comment:
print('# ' + self.comment) print('# ' + self.comment)
default_value = self.get() default_value = kwargs.pop('default_value', self.get())
if not prompt: if not prompt:
prompt = '%s: ' % self.name prompt = '%s: ' % self.name
if default_value is not None: if default_value is not None: