diff --git a/teos/cli/help.py b/teos/cli/help.py deleted file mode 100644 index 0bc99e64..00000000 --- a/teos/cli/help.py +++ /dev/null @@ -1,17 +0,0 @@ -def show_usage(): - return ( - "USAGE: " - "\n\tteos-cli [global options] command [command options] [arguments]" - "\n\nCOMMANDS:" - "\n\tget_all_appointments \tGets information about all appointments stored in the tower." - "\n\tget_tower_info \t\tGets generic information about the tower." - "\n\tget_users \t\tGets the list of registered user ids." - "\n\tget_user \t\tGets information about a specific user." - "\n\thelp \t\t\tShows a list of commands or help for a specific command." - "\n\nGLOBAL OPTIONS:" - "\n\t--rpcconnect \tRPC server where to send the requests. Defaults to 'localhost' (modifiable in conf file)." - "\n\t--rpcport \tRPC port where to send the requests. Defaults to '8814' (modifiable in conf file)." - "\n\t--datadir \tSpecify data directory used for the config file. Defaults to '~\\.teos'." - "\n\t-d, --debug \tShows debug information and stores it in teos_cli.log." - "\n\t-h, --help \tShows this message." - ) diff --git a/teos/cli/teos_cli.py b/teos/cli/teos_cli.py index 199dfe62..9b8c086c 100755 --- a/teos/cli/teos_cli.py +++ b/teos/cli/teos_cli.py @@ -14,11 +14,52 @@ from teos import DEFAULT_CONF, DATA_DIR, CONF_FILE_NAME -from teos.cli.help import show_usage from teos.protobuf.tower_services_pb2_grpc import TowerServicesStub from teos.protobuf.user_pb2 import GetUserRequest +def show_usage(): + command_help_lines = [] + longest_command_length = max(len(x) for x in Cli.COMMANDS.keys()) + + for cmd, cmd_cls in Cli.COMMANDS.items(): + doc = cmd_cls.__doc__ + + # find and parse the first line containing NAME: + name_line = next(line for line in doc.split("\n") if "NAME:" in line) + if "teos-cli" not in name_line: + raise InvalidParameter(f"The NAME line of the {cmd} command is malformed") + + _, rest = name_line.split("teos-cli") + + if " - " not in rest: + raise InvalidParameter(f"The NAME line of the {cmd} command is malformed") + + cmd_name_in_line, cmd_description = map(lambda x: x.strip(), rest.split(" - ")) + + if cmd_name_in_line != cmd: + raise InvalidParameter(f"The NAME line of the {cmd} command is malformed") + + padded_cmd_name = cmd.ljust(longest_command_length, " ") + + command_help_lines.append(f"\t{padded_cmd_name} {cmd_description}") + + commands_help = "\n".join(command_help_lines) + return ( + "USAGE: " + "\n\tteos-cli [global options] command [command options] [arguments]" + "\n\nCOMMANDS:\n" + f"{commands_help}" + "\n\nGLOBAL OPTIONS:" + "\n\t--rpcconnect RPC server where to send the requests. Defaults to 'localhost' (modifiable in conf file)." + "\n\t--rpcport RPC port where to send the requests. Defaults to '8814' (modifiable in conf file)." + "\n\t--datadir Specify data directory used for the config file. Defaults to '~\\.teos'." + "\n\t-d, --debug Shows debug information and stores it in teos_cli.log." + "\n\t-h, --help Shows this message." + "\n" + ) + + def to_json(obj): """ All conversions to json in this module should be consistent, therefore we restrict the options using diff --git a/test/teos/unit/cli/test_teos_cli.py b/test/teos/unit/cli/test_teos_cli.py new file mode 100644 index 00000000..0f6270b5 --- /dev/null +++ b/test/teos/unit/cli/test_teos_cli.py @@ -0,0 +1,8 @@ +import pytest + +from teos.cli.teos_cli import show_usage + + +def test_show_usage_does_not_throw(): + # If any of the Cli commands' docstring has a wrong format and cannot be parsed, this will raise an error + show_usage() \ No newline at end of file