Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre and post task lists into help output #929

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions invoke/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,8 @@ def print_task_help(self, name: str) -> None:
ctx = self.parser.contexts[name]
tuples = ctx.help_tuples()
docstring = inspect.getdoc(self.collection[name])
pre_tasks = self.collection[name].pre
post_tasks = self.collection[name].post
header = "Usage: {} [--core-opts] {} {}[other tasks here ...]"
opts = "[--options] " if tuples else ""
print(header.format(self.binary, name, opts))
Expand All @@ -804,6 +806,27 @@ def print_task_help(self, name: str) -> None:
print(self.leading_indent + "none")
print("")

print("Pre-tasks:")
if pre_tasks:
for pre in pre_tasks:
print(
self.leading_indent + self.collection.transform(pre.name)
)
print("")
else:
print(self.leading_indent + "none")
print("")
print("Post-tasks:")
if post_tasks:
for post in post_tasks:
print(
self.leading_indent + self.collection.transform(post.name)
)
print("")
else:
print(self.leading_indent + "none")
print("")

def list_tasks(self) -> None:
# Short circuit if no tasks to show (Collection now implements bool)
focus = self.scoped_collection
Expand Down
4 changes: 2 additions & 2 deletions tests/_support/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def foo2(c):
pass


@task
@task(post=[foo2])
def foo3(c):
"""Foo the other bar:

Expand All @@ -32,7 +32,7 @@ def foo3(c):
pass


@task(default=True)
@task(default=True, pre=[foo3])
def biz(c):
pass

Expand Down
38 changes: 38 additions & 0 deletions tests/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def copying_from_task_context_does_not_set_empty_list_values(self):
# .value = <default value> actually ends up creating a
# list-of-lists.
p = Program()

# Set up core-args parser context with an iterable arg that hasn't
# seen any value yet
def filename_args():
Expand Down Expand Up @@ -643,6 +644,12 @@ def prints_help_for_task_only(self):
-h STRING, --why=STRING Motive
-w STRING, --who=STRING Who to punch

Pre-tasks:
none

Post-tasks:
none

""".lstrip()
for flag in ["-h", "--help"]:
expect("-c decorators {} punch".format(flag), out=expected)
Expand All @@ -657,6 +664,12 @@ def works_for_unparameterized_tasks(self):
Options:
none

Pre-tasks:
foo3

Post-tasks:
none

""".lstrip()
expect("-c decorators -h biz", out=expected)

Expand All @@ -676,6 +689,12 @@ def displays_docstrings_if_given(self):
Options:
none

Pre-tasks:
none

Post-tasks:
none

""".lstrip()
expect("-c decorators -h foo", out=expected)

Expand All @@ -693,6 +712,12 @@ def dedents_correctly(self):
Options:
none

Pre-tasks:
none

Post-tasks:
none

""".lstrip()
expect("-c decorators -h foo2", out=expected)

Expand All @@ -710,6 +735,12 @@ def dedents_correctly_for_alt_docstring_style(self):
Options:
none

Pre-tasks:
none

Post-tasks:
foo2

""".lstrip()
expect("-c decorators -h foo3", out=expected)

Expand All @@ -726,6 +757,12 @@ def exits_after_printing(self):
-h STRING, --why=STRING Motive
-w STRING, --who=STRING Who to punch

Pre-tasks:
none

Post-tasks:
none

""".lstrip()
expect("-c decorators -h punch --list", out=expected)

Expand Down Expand Up @@ -1374,6 +1411,7 @@ def env_vars_load_with_prefix(self, monkeypatch):

def env_var_prefix_can_be_overridden(self, monkeypatch):
monkeypatch.setenv("MYAPP_RUN_HIDE", "both")

# This forces the execution stuff, including Executor, to run
# NOTE: it's not really possible to rework the impl so this test is
# cleaner - tasks require per-task/per-collection config, which can
Expand Down