diff --git a/pyproject.toml b/pyproject.toml index c1099a7879015908f61d613a8eaf2ffb0b67a2bb..22a97f671b79ae95af73958d772e5b4b2f4b31a8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,6 +20,7 @@ pytest = "^5.2" [tool.poetry.scripts] stechuhr-client = "stechuhr_client.client:main" +config = "stechuhr_client.config:main" [build-system] diff --git a/stechuhr_client/config.py b/stechuhr_client/config.py index 3657f79a6fcb8d61106d62fe0c83666d215c18a4..82f3a44a6db87d4168009b169d5d003224cd6cdf 100644 --- a/stechuhr_client/config.py +++ b/stechuhr_client/config.py @@ -290,6 +290,94 @@ def read_config(config_path: str) -> dict: process_led_list_value(config, "success_on_time") return config +def main(): + """ + Gets run only if config.py is called directly or via `poetry run config` + Entry point for the CLI application + """ + + # List of available commands and their respective functions + commands = { + "default" : print_default, + "paths": print_paths, + "directories": print_directories, + "create": create_config, + "test": test + } + + # List of available options + availaible_options = [ + ["-h", "--help"] + ] + + # If no argument has been passed display the help and exit + if len(sys.argv) == 1: + print_help() + exit() + + # Extract the command arguments + command_args = [c for c in sys.argv[1:] if not c.strip().startswith("-")] + + # Extract the short_options + short_options = [c.lstrip("-") for c in sys.argv[1:] if c.strip().startswith("-") and not c.strip().startswith("--")] + + # Flatten the short_options to e.g. so -1234 will result in ["1", "2", "3", "4"] + short_options = [item for sublist in short_options for item in sublist] + + # Extract the long options + long_options = [c.lstrip("--") for c in sys.argv[1:] if c.strip().startswith("--")] + + errored = False + # Short Options + for o in short_options: + if o not in [a[0].lstrip("-") for a in availaible_options]: + print("Error: the option \"-{}\" does not exist.".format(o), file=sys.stderr) + errored = True + + # Long Options + for o in long_options: + if o not in [a[0].lstrip("--") for a in availaible_options]: + print("Error: the option \"--{}\" does not exist.".format(o), file=sys.stderr) + errored = True + + # If any of the above errored, exit. This allows to display all errors at once + if errored: + print("\nCheck the available commands and options below:") + print() + print_help() + exit() + + # Currently we only handle a single command + if len(command_args) == 1: + command = sys.argv[1] + # Short commands are allowed if they are not ambigous. E.g "te" will trigger "test" + if not any([c.startswith(command.strip().lower()) for c in commands.keys()]): + # No fitting command has been found, print helpt and exit + print_help() + exit() + elif len([c for c in commands.keys() if c.startswith(command.strip().lower())]) > 1: + # More than one fitting command has been found, display this message + print("Ambiguous Input: There are {} commands starting with \"{}\": {}".format( + len([c for c in commands.keys() if c.startswith(command.strip().lower())]), + command, + ", ".join([c for c in commands.keys() if c.startswith(command.strip().lower())]) + )) + else: + # A command has been found: + choice = [c for c in commands.keys() if c.startswith(command.strip().lower())][0] + + # If there is a -h or --help option, display the function's docstring + # otherwise execute the function + if "h" in short_options or "help" in long_options: + print("Help: config {}".format(commands[choice].__name__)) + print(commands[choice].__doc__) + else: + commands[choice]() + else: + # If more than one command is given, display the help + print_help() + + def test(): """