Skip to content

Commit

Permalink
Display the default value of args in help #126
Browse files Browse the repository at this point in the history
Also improve validation of args
  • Loading branch information
nat-n committed Mar 4, 2023
1 parent f76b8a6 commit ba5ee7c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
5 changes: 3 additions & 2 deletions poethepoet/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from .exceptions import ExecutionError, PoeException

if TYPE_CHECKING:

from .config import PoeConfig
from .context import RunContext
from .task import PoeTask
Expand Down Expand Up @@ -175,7 +174,9 @@ def print_help(
if isinstance(error, str):
error = PoeException(error)

tasks_help: Dict[str, Tuple[str, Sequence[Tuple[Tuple[str, ...], str]]]] = {
tasks_help: Dict[
str, Tuple[str, Sequence[Tuple[Tuple[str, ...], str, str]]]
] = {
task_name: (
(
content.get("help", ""),
Expand Down
11 changes: 9 additions & 2 deletions poethepoet/task/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,18 @@ def _get_arg_options_list(arg: ArgParams, name: Optional[str] = None):
@classmethod
def get_help_content(
cls, args_def: Optional[ArgsDef]
) -> List[Tuple[Tuple[str, ...], str]]:
) -> List[Tuple[Tuple[str, ...], str, str]]:
if args_def is None:
return []

def format_default(arg) -> str:
default = arg.get("default")
if default:
return f"[default: {default}]"
return ""

return [
(arg["options"], arg.get("help", ""))
(arg["options"], arg.get("help", ""), format_default(arg))
for arg in cls._normalize_args_def(args_def)
]

Expand Down
24 changes: 16 additions & 8 deletions poethepoet/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def set_default_verbosity(self, default_verbosity: int):
def print_help(
self,
tasks: Optional[
Mapping[str, Tuple[str, Sequence[Tuple[Tuple[str, ...], str]]]]
Mapping[str, Tuple[str, Sequence[Tuple[Tuple[str, ...], str, str]]]]
] = None,
info: Optional[str] = None,
error: Optional[PoeException] = None,
Expand Down Expand Up @@ -195,25 +195,33 @@ def print_help(
max_task_len = max(
max(
len(task),
max([len(", ".join(opts)) for (opts, _) in args] or (0,)) + 2,
max([len(", ".join(opts)) for (opts, _, _) in args] or (0,))
+ 2,
)
for task, (_, args) in tasks.items()
)
col_width = max(13, min(30, max_task_len))

tasks_section = ["<h2>CONFIGURED TASKS</h2>"]
for task, (help_text, args_help) in tasks.items():
if task.startswith("_"):
continue
tasks_section.append(
f" <em>{self._padr(task, col_width)}</em> {help_text}"
)
for (options, arg_help_text) in args_help:
tasks_section.append(
" "
f"<em3>{self._padr(', '.join(options), col_width - 2)}</em3>"
f" {arg_help_text}"
)
for (options, arg_help_text, default) in args_help:
task_arg_help = [
" ",
f"<em3>{self._padr(', '.join(options), col_width-1)}</em3>",
]
if arg_help_text:
task_arg_help.append(arg_help_text)
if default:
task_arg_help.append(default)
tasks_section.append(" ".join(task_arg_help))

result.append(tasks_section)

else:
result.append("<h2-dim>NO TASKS CONFIGURED</h2-dim>")

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ poethepoet = "poethepoet.plugin:PoetryPlugin"
help = "Execute poe from this repo (useful for testing)"
script = "poethepoet:main"


[tool.coverage.report]
omit = ["**/site-packages/**", "poethepoet/completion/*", "poethepoet/plugin.py"]

Expand Down
43 changes: 22 additions & 21 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,29 @@ def test_documentation_of_task_named_args(run_poe):
assert (
"\nResult: No task specified.\n" in result.capture
), "Output should include status message"

assert re.search(
r"CONFIGURED TASKS\n"
r" composite_task \s+\n"
r" echo-args \s+\n"
r" static-args-test \s+\n"
r" call_attrs \s+\n"
r" greet \s+\n"
r" print-script-result \s+\n"
r" dont-print-script-result \s+\n"
r" greet-passed-args \s+\n"
r" --greeting \s+\n"
r" --user \s+\n"
r" --optional \s+\n"
r" --upper \s+\n"
r" greet-full-args \s+\n"
r" --greeting, -g \s+\n"
r" --user \s+\n"
r" --upper \s+\n"
r" --age, -a \s+\n"
r" --height, -h \s+The user's height in meters\n"
r" greet-strict \s+All arguments are required\n"
r" --greeting \s+this one is required\n"
r" --name \s+and this one is required\n",
r" composite_task \s+\n"
r" echo-args \s+\n"
r" static-args-test \s+\n"
r" call_attrs \s+\n"
r" greet \s+\n"
r" print-script-result \s+\n"
r" dont-print-script-result\s+\n"
r" greet-passed-args \s+\n"
r" --greeting \s+\n"
r" --user \s+\n"
r" --optional \s+\n"
r" --upper \s+\n"
r" greet-full-args \s+\n"
r" --greeting, -g \s+\[default: hi\]\n"
r" --user \s+\n"
r" --upper \s+\n"
r" --age, -a \s+\n"
r" --height, -h \s+The user's height in meters\n"
r" greet-strict \s+All arguments are required\n"
r" --greeting \s+this one is required \[default: \$\{DOES\}n't \$\{STUFF\}\]\n"
r" --name \s+and this one is required\n",
result.capture,
)

0 comments on commit ba5ee7c

Please sign in to comment.