Skip to content
Snippets Groups Projects
Commit 41bba04c authored by David Huss's avatar David Huss :speech_balloon:
Browse files

Add missing main to config

parent f93e14b8
Branches
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@ pytest = "^5.2"
[tool.poetry.scripts]
stechuhr-client = "stechuhr_client.client:main"
config = "stechuhr_client.config:main"
[build-system]
......
......@@ -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():
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment