refactor: add regex pattern as parameter to get_string_input for validating input

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-05-25 21:32:15 +02:00
parent 70ad635e3d
commit df45c5955e

View File

@@ -6,7 +6,7 @@
# # # #
# This file may be distributed under the terms of the GNU GPLv3 license # # This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= # # ======================================================================= #
import re
from typing import List, Union from typing import List, Union
from utils import INVALID_CHOICE from utils import INVALID_CHOICE
@@ -84,6 +84,7 @@ def get_number_input(
def get_string_input( def get_string_input(
question: str, question: str,
regex: Union[str, None] = None,
exclude: Union[List, None] = None, exclude: Union[List, None] = None,
allow_special_chars=False, allow_special_chars=False,
default=None, default=None,
@@ -91,6 +92,7 @@ def get_string_input(
""" """
Helper method to get a string input from the user Helper method to get a string input from the user
:param question: The question to display :param question: The question to display
:param regex: An optional regex pattern to validate the input against
:param exclude: List of strings which are not allowed :param exclude: List of strings which are not allowed
:param allow_special_chars: Wheter to allow special characters in the input :param allow_special_chars: Wheter to allow special characters in the input
:param default: Optional default value :param default: Optional default value
@@ -98,11 +100,18 @@ def get_string_input(
""" """
_exclude = [] if exclude is None else exclude _exclude = [] if exclude is None else exclude
_question = format_question(question, default) _question = format_question(question, default)
_pattern = re.compile(regex) if regex is not None else None
while True: while True:
_input = input(_question) _input = input(_question)
print(_input)
if _input.lower() in _exclude: if _input.lower() in _exclude:
Logger.print_error("This value is already in use/reserved.") Logger.print_error("This value is already in use/reserved.")
elif default is not None and _input == "":
return default
elif _pattern is not None and _pattern.match(_input):
return _input
elif allow_special_chars: elif allow_special_chars:
return _input return _input
elif not allow_special_chars and _input.isalnum(): elif not allow_special_chars and _input.isalnum():