Skip to content

Conversation

@timrulebosch
Copy link

This PR is adding template support for CLI commands --list / --list-all. The implementation adds an optional flag (--template) which is used to specify a template file(s). When specified, the template files(s) are loaded and rendered. The templating mechanism makes use of the existing slim-sprig functions available in Task.

Primary motivation for the PR is to generate documentation for a library of Taskfiles. Some of the documentation can be extracted from the existing Taskfile schema, and other elements of the documentation are embedded in the Taskfiles. This later embedded documentation (example here) would be made available to the templating mechanism as well (PR in development).

As it currently stands, the PR replaces the --list code (a for loop) with the templating mechanism.

Example

mddoc.template

# Taskfile
Available tasks for this project:
{{ range . }}
## {{.Task}}
{{.Desc}}
{{if len .Aliases}}
### Aliases
{{range .Aliases}}- {{.}}
{{end}}{{end}}{{end}}

Console Output

$ task --list-all --template mddoc.template
# Taskfile
Available tasks for this project:

## clean
Cleans temp files and folders

### Aliases
- clear

## default


## format
Runs golangci-lint and formats any Go files

### Aliases
- fmt
- f

## generate
Runs all generate tasks

### Aliases
- gen
- g

Notes:

fixes #1695
fixes #2261
related #1916
related #931 (perhaps)
related #2091

Outstanding items needed to complete PR:

  • Refinement of operation - e.g. relationship between list list-all and template CLI options.
  • Documentation with examples.
  • Application to the --summary command?
  • Support template definition in the taskrc file.?

@trulede
Copy link
Contributor

trulede commented Nov 4, 2025

It occurs to me that the listDefaultTemplate could also be optionally loaded from the taskrc file which would allow for easier customisation of --list/--list-all/--summary behaviour, such as mentioned in #2486.

Probably something like:

templates:
  cli:
    list: >
      task: Available tasks for this project:{{ range . }}
      {{color "Yellow"}}* {{color "Green"}}{{ .Task }}{{color "Reset"}}:{{"\t"}} 
      {{ .Desc | replace "\n" " " }}{{if len .Aliases}}{{color "Cyan"}}{{"\t"}}(aliases: {{.Aliases | join ", "}})
      {{color "Reset"}}{{end}}{{end}}
    list-all: >
      task ....
    summary: >
      task ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Option to disable output of commands in summary Generate documentation based on your taskfile

2 participants