diff --git a/src/cobbler_tftp/cli.py b/src/cobbler_tftp/cli.py index 31871cd..b1d2869 100644 --- a/src/cobbler_tftp/cli.py +++ b/src/cobbler_tftp/cli.py @@ -65,7 +65,7 @@ def cli(ctx): multiple=True, help="""Set custom settings in format:\n ..<...>.=.\n - Your settings must use single quotes. If a single quote appears within a value it must be escaped.""", + The value is parsed as YAML. Quotes around the value are recommended for strings.""", ) def start( daemon: Optional[bool], diff --git a/src/cobbler_tftp/settings/__init__.py b/src/cobbler_tftp/settings/__init__.py index 9811d8b..d5cd598 100644 --- a/src/cobbler_tftp/settings/__init__.py +++ b/src/cobbler_tftp/settings/__init__.py @@ -256,7 +256,8 @@ def load_cli_options( :param daemon: If the application should be run in the background as a daemon or not. :param enable_automigration: Whether to enable the automigration or not. :param settings: List of custom settings which can be entered manually. - Each entry has the format: ``..<...>.=`` + Each entry has the format: ``..<...>.=``, + where VALUE is parsed as a YAML value. :return _settings_dict: Settings dictionary. """ @@ -271,12 +272,13 @@ def load_cli_options( if "." not in option_list[0]: self._settings_dict.update({option_list[0]: option_list[1]}) else: - parent = option_list[0].split(".") + key_path = option_list[0].split(".") + key_path.reverse() - setting_to_update = {parent[-1]: option_list[1]} + setting_to_update = yaml.safe_load(option_list[1]) - for key in range(len(parent), 0, -1): - setting_to_update = {parent[key]: setting_to_update} + for key in key_path: + setting_to_update = {key: setting_to_update} self._settings_dict.update(setting_to_update) # type: ignore diff --git a/tests/unittests/application_settings/test_settings.py b/tests/unittests/application_settings/test_settings.py index 9a0660a..09db383 100644 --- a/tests/unittests/application_settings/test_settings.py +++ b/tests/unittests/application_settings/test_settings.py @@ -118,3 +118,21 @@ def test_build_settings_with_missing_config_file( assert captured_message.out == expected_message assert isinstance(settings, Settings) assert_default_settings(settings) + + +def test_build_settings_with_cli_args(settings_factory: SettingsFactory): + cli_settings = ["tftp.address=1.2.3.4"] + + settings = settings_factory.build_settings(None, cli_arguments=cli_settings) + + assert isinstance(settings, Settings) + assert settings.tftp_addr == "1.2.3.4" + + +def test_build_settings_with_integer_cli_args(settings_factory: SettingsFactory): + cli_settings = ["tftp.port=1969"] + + settings = settings_factory.build_settings(None, cli_arguments=cli_settings) + + assert isinstance(settings, Settings) + assert settings.tftp_port == 1969