Skip to content
This repository has been archived by the owner on Sep 26, 2022. It is now read-only.

Commit

Permalink
Generating the show_usage message from parsing the Cli commands __docs__
Browse files Browse the repository at this point in the history
  • Loading branch information
bigspider committed Oct 9, 2020
1 parent 1b27e0b commit fbe7209
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 18 deletions.
17 changes: 0 additions & 17 deletions teos/cli/help.py

This file was deleted.

43 changes: 42 additions & 1 deletion teos/cli/teos_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/teos/unit/cli/test_teos_cli.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit fbe7209

Please sign in to comment.