Skip to content

Commit

Permalink
Clean up help messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
inno committed May 11, 2024
1 parent ebbe16e commit a7e25fb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
35 changes: 18 additions & 17 deletions simplecli/simplecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,29 +237,28 @@ def help_text(
params: list[Param],
docstring: str = "",
) -> str:
help_msg = ["Usage: ", f"\t{filename} ..."]
help_msg = []
if docstring:
help_msg += ["Description: ", f"\t{docstring.lstrip()}"]
help_msg.append("Options:")
positional = []
max_attr_len = len(max(params, key=lambda x: len(x.help_name)).help_name)
for param in params:
if get_origin(param.annotation) in (Union, UnionType):
types = [a.__name__ for a in param.datatypes]
if "NoneType" in types:
types.remove("NoneType")
arg_types = " ".join(types)
arg_types += " OPTIONAL"
else:
arg_types = param.help_type
else:
arg_types = param.help_type
if param.required:
positional.append(param.help_name)
help_line = f" --{param.help_name}"
if param.annotation is not Empty:
help_line += f"\t\t({arg_types})\t"
offset = max_attr_len - len(param.help_name) + 2
help_line += " " * offset
if param.description:
help_line += f" {param.description}"
if param.default is not Empty:
help_line += f" [Default: {param.default}]"
help_line += f" {param.description}"
if type(param.default) in (int, float, str):
help_line += f" (Default: {param.default})"
help_msg.append(help_line)
return "\n".join(help_msg)
usage = f" {filename} "
if positional:
usage += "[" + "] [".join(positional) + "]"
return "\n".join(["Usage:", usage, ""] + help_msg)


def clean_args(argv: list[str]) -> tuple[ArgList, ArgDict]:
Expand Down Expand Up @@ -358,7 +357,9 @@ def wrap(func: Callable[..., Any]) -> None:
version = func.__globals__.get("__version__", "")
if version:
params.append(Param("version", internal_only=True))
params.append(Param("help", internal_only=True))
params.append(
Param("help", description="This message", internal_only=True)
)
if "help" in kw_args:
exit(help_text(filename, params, format_docstring(func.__doc__ or "")))

Expand Down
20 changes: 10 additions & 10 deletions tests/test_help_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def test_help_text_union():
filename="filename",
params=[Param(name="somevar", annotation=Union[int, float])],
)
assert "[int, float]" in text
assert "OPTIONAL" not in text
assert "filename [somevar]" in text
assert "--somevar" in text


@skip_if_uniontype_unsupported
Expand All @@ -18,26 +18,26 @@ def test_help_text_uniontype():
filename="filename",
params=[Param(name="somevar", annotation=float | int)],
)
assert "[float, int]" in text
assert "OPTIONAL" not in text
assert "filename [somevar]" in text
assert "--somevar" in text


def test_help_text_optional():
text = help_text(
filename="filename",
params=[Param(name="somevar", annotation=Optional[float])],
)
assert "float" in text
assert "OPTIONAL" in text
assert "filename [somevar]" not in text
assert "--somevar" in text


def test_help_text_union_none():
text = help_text(
filename="filename",
params=[Param(name="somevar", annotation=Union[float, None])],
)
assert "float" in text
assert "OPTIONAL" in text
assert "filename [somevar]" not in text
assert "--somevar" in text


@skip_if_uniontype_unsupported
Expand All @@ -46,5 +46,5 @@ def test_help_text_uniontype_none():
filename="filename",
params=[Param(name="somevar", annotation=float | None)],
)
assert "float" in text
assert "OPTIONAL" in text
assert "filename [somevar]" not in text
assert "--somevar" in text
7 changes: 0 additions & 7 deletions tests/test_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def code1(this_var: int): # stuff and things

help_msg = e.value.args[0]
assert "--this-var" in help_msg
assert "(int)" in help_msg
assert "stuff and things" in help_msg


Expand All @@ -66,11 +65,9 @@ def code(

help_msg = e.value.args[0]
assert "--that-var" in help_msg
assert "[str, int]" in help_msg
assert "that is the var" in help_msg
assert "--count" in help_msg
assert "Default: 54" in help_msg
assert "OPTIONAL" in help_msg


def test_wrap_simple_type_error(monkeypatch):
Expand Down Expand Up @@ -109,7 +106,6 @@ def code2(this_var: int): # stuff and things
assert "Description:" not in help_msg
assert "Version: 1.2.3" not in help_msg
assert "--this-var" in help_msg
assert "(int)" in help_msg
assert "stuff and things" in help_msg


Expand All @@ -128,7 +124,6 @@ def code1(this_var: int): # stuff and things
assert "Version: 1.2.3" in help_msg
assert "Description:" not in help_msg
assert "--this-var" not in help_msg
assert "(int)" not in help_msg
assert "stuff and things" not in help_msg


Expand All @@ -148,7 +143,6 @@ def code2(this_var: int): # stuff and things
assert "Description:" in help_msg
assert "this is a description" in help_msg
assert "--this-var" in help_msg
assert "(int)" in help_msg
assert "stuff and things" in help_msg


Expand All @@ -168,7 +162,6 @@ def code(

help_msg = e.value.args[0]
assert "--that-var" in help_msg
assert "[str, int]" in help_msg
assert "that is the var" in help_msg
assert "--count" in help_msg
assert "Default: 54" in help_msg
Expand Down

0 comments on commit a7e25fb

Please sign in to comment.