Bugfixes in configuration console argument handling

This commit is contained in:
Sven Heidemann 2021-03-09 18:27:09 +01:00
parent b178e884f6
commit c574a44a26

View File

@ -80,6 +80,78 @@ class Configuration(ConfigurationABC):
else: else:
self._config[name] = value self._config[name] = value
def _validate_argument_child(self, argument: str, argument_type: ConsoleArgument, next_arguments: Optional[list[str]]) -> bool:
if argument_type.console_arguments is not None and len(argument_type.console_arguments) > 0:
found = False
for child_argument_type in argument_type.console_arguments:
found = self._validate_argument_by_argument_type(argument, child_argument_type, next_arguments[1:])
if not found:
raise Exception(f'Invalid argument: {argument}')
return found
return True
def _validate_argument_by_argument_type(self, argument: str, argument_type: ConsoleArgument, next_arguments: list[str] = None) -> bool:
if argument_type.token != '' and argument.startswith(argument_type.token) and (argument_type.name == argument.split(argument_type.token)[1] or argument in argument_type.aliases) and argument_type.value_token == '':
# --new
self._additional_arguments.append(argument_type.name)
if next_arguments is not None and len(next_arguments) > 0:
return self._validate_argument_child(next_arguments[0], argument_type, next_arguments)
return True
elif argument_type.token != '' and argument.startswith(argument_type.token) and (argument_type.name == argument.split(argument_type.token)[1] or argument in argument_type.aliases) and argument_type.value_token != '':
# --new=
if argument_type.value_token == ' ':
if next_arguments is None or len(next_arguments) == 0:
raise Exception(f'Invalid argument: {argument}')
value = next_arguments[0]
else:
value = argument.split(argument_type.value_token)[1]
self._set_variable(argument_type.name, value)
self._additional_arguments.append(argument_type.name)
if next_arguments is not None and len(next_arguments) > 0:
return self._validate_argument_child(next_arguments[0], argument_type, next_arguments)
return True
elif argument_type.token == '' and (argument.startswith(argument_type.name) or argument in argument_type.aliases) and argument_type.value_token == '':
# new
self._additional_arguments.append(argument_type.name)
if next_arguments is not None and len(next_arguments) > 0:
return self._validate_argument_child(next_arguments[0], argument_type, next_arguments)
return True
elif argument_type.token == '' and (argument.startswith(argument_type.name) or argument in argument_type.aliases) and argument_type.value_token != '':
# new=
value = ''
if argument_type.value_token == ' ':
if next_arguments is None or len(next_arguments) == 0:
raise Exception(f'Invalid argument: {argument}')
value = next_arguments[0]
else:
value = argument.split(argument_type.value_token)[1]
self._set_variable(argument_type.name, value)
self._additional_arguments.append(argument_type.name)
if next_arguments is not None and len(next_arguments) > 0:
return self._validate_argument_child(next_arguments[0], argument_type, next_arguments[1:])
return True
else:
return False
def add_environment_variables(self, prefix: str): def add_environment_variables(self, prefix: str):
for variable in ConfigurationVariableName.to_list(): for variable in ConfigurationVariableName.to_list():
var_name = f'{prefix}{variable}' var_name = f'{prefix}{variable}'
@ -93,112 +165,34 @@ class Configuration(ConfigurationABC):
for arg_name in ConfigurationVariableName.to_list(): for arg_name in ConfigurationVariableName.to_list():
self.add_console_argument(ConsoleArgument('--', arg_name, [], '')) self.add_console_argument(ConsoleArgument('--', arg_name, [], ''))
old_is_arg = None
arg_list = sys.argv[1:] arg_list = sys.argv[1:]
for arg in arg_list: print(arg_list)
try:
is_done = False
for argument_type in self._argument_types:
# check prefix
if argument_type.token != '' and arg.startswith(argument_type.token):
name = arg.split(argument_type.token)[1]
if argument_type.value_token == '':
if name == argument_type.name or name in argument_type.aliases:
if not self._is_multiple_args_allowed:
if old_is_arg is None:
self._additional_arguments.append(argument_type.name)
is_done = True
old_is_arg = argument_type
break
break
self._additional_arguments.append(argument_type.name)
is_done = True
old_is_arg = argument_type
break
if argument_type.value_token != '' and arg.__contains__(argument_type.value_token):
name = name.split(argument_type.value_token)[0]
if name == argument_type.name or name in argument_type.aliases:
if not self._is_multiple_args_allowed:
if old_is_arg is None:
value = arg.split(argument_type.value_token)[1]
self._set_variable(argument_type.name, value)
is_done = True
old_is_arg = argument_type
break
break
value = arg.split(argument_type.value_token)[1]
self._set_variable(argument_type.name, value)
is_done = True
old_is_arg = argument_type
break
elif arg == argument_type.name or arg in argument_type.aliases:
if not self._is_multiple_args_allowed:
if old_is_arg is None:
self._additional_arguments.append(argument_type.name)
is_done = True
old_is_arg = argument_type
break
break
self._additional_arguments.append(argument_type.name)
is_done = True
old_is_arg = argument_type
break
elif old_is_arg is not None and old_is_arg.arg_types is not None:
for arg_type in old_is_arg.arg_types:
if is_done:
break
if arg_type.name == arg:
if arg_type.value_token != '' and arg_type.value_token in arg:
value = arg.split(arg_type.value_token)[1]
self._set_variable(arg_type.name, value)
self._additional_arguments.append(arg_type.name)
is_done = True
old_is_arg = arg_type
break
elif arg_type.value_token == ' ':
for i in range(0, len(arg_list)): for i in range(0, len(arg_list)):
if arg_list[i] == arg and i+1 < len(arg_list): argument = arg_list[i]
value = arg_list[i+1] next_arguments = []
self._set_variable(arg_type.name, value)
self._additional_arguments.append(arg_type.name)
arg_list.remove(value)
is_done = True
old_is_arg = arg_type
break
if is_done: error_message = ''
break
self._additional_arguments.append(arg_type.name) if i+1 < len(arg_list):
is_done = True next_arguments = arg_list[i+1:]
old_is_arg = arg_type
break
if not is_done: found = False
message = f'Invalid argument: {arg}' for argument_type in self._argument_types:
try:
if self._argument_error_function is not None: found = self._validate_argument_by_argument_type(argument, argument_type, next_arguments)
self._argument_error_function(message) if found:
else: return
self._print_error(__name__, message)
exit()
except Exception as e: except Exception as e:
message = f'Invalid argument: {arg} -> {e}' error_message = e
print('\nerror')
if not found and error_message == '':
error_message = f'Invalid argument: {argument}'
if self._argument_error_function is not None: if self._argument_error_function is not None:
self._argument_error_function(message) self._argument_error_function(error_message)
else: else:
self._print_error(__name__, message) self._print_error(__name__, error_message)
exit() exit()