Eric Bower
·
2026-09-19
1# Completion script for the "task" (Taskfile / go-task) command.
2# https://taskfile.dev
3
4function completion/task {
5 typeset OPTIONS ARGOPT PREFIX
6 OPTIONS=( #>#
7 "a --list-all; list tasks with or without a description"
8 "c --color; colored output (default true)"
9 "--completion:; generate shell completion script"
10 "C: --concurrency:; limit number of tasks to run concurrently"
11 "d: --dir:; set directory of execution"
12 "--disable-fuzzy; disable fuzzy matching for task names"
13 "n --dry; compile and print tasks without executing them"
14 "x --exit-code; pass-through exit code of task command"
15 "--experiments; list available experiments"
16 "F --failfast; stop all tasks if one fails"
17 "f --force; force execution even when task is up-to-date"
18 "g --global; run global Taskfile from home directory"
19 "h --help; show help"
20 "i --init; create a new Taskfile.yml in current folder"
21 "--insecure; allow downloading Taskfiles over insecure connections"
22 "--interactive; prompt for missing required variables"
23 "I: --interval:; interval to watch for changes"
24 "j --json; format task list as JSON"
25 "l --list; list tasks with description"
26 "--nested; nest namespaces when listing as JSON"
27 "--no-status; ignore status when listing as JSON"
28 "o: --output:; set output style (interleaved, group, prefixed)"
29 "--output-group-begin:; message template before grouped output"
30 "--output-group-end:; message template after grouped output"
31 "--output-group-error-only; swallow output from successful tasks"
32 "p --parallel; execute tasks in parallel"
33 "s --silent; disable echoing"
34 "--sort:; sort order (default, alphanumeric, none)"
35 "--status; exit non-zero if tasks are not up-to-date"
36 "--summary; show summary about a task"
37 "t: --taskfile:; choose Taskfile to run"
38 "v --verbose; enable verbose mode"
39 "--version; show version"
40 "w --watch; enable watch mode"
41 "y --yes; assume yes to all prompts"
42 ) #<#
43
44 # If -- is already present in WORDS, subsequent words are CLI_ARGS; do not complete
45 typeset w
46 for w in "$WORDS"; do
47 if [ "$w" = "--" ]; then
48 return
49 fi
50 done
51
52 command -f completion//parseoptions -en
53 case $ARGOPT in
54 (-)
55 command -f completion//completeoptions
56 ;;
57 (d|--dir)
58 complete -P "$PREFIX" -d
59 ;;
60 (t|--taskfile)
61 complete -P "$PREFIX" -f
62 ;;
63 (o|--output)
64 complete -P "$PREFIX" -D "Interleaved output" interleaved
65 complete -P "$PREFIX" -D "Grouped output" group
66 complete -P "$PREFIX" -D "Prefixed output" prefixed
67 ;;
68 (--sort)
69 complete -P "$PREFIX" -D "Default sort order" default
70 complete -P "$PREFIX" -D "Alphanumeric sort" alphanumeric
71 complete -P "$PREFIX" -D "No sorting" none
72 ;;
73 (--completion)
74 complete -P "$PREFIX" -- bash zsh fish powershell
75 ;;
76 (*)
77 command -f completion/task::tasks
78 ;;
79 esac
80}
81
82function completion/task::tasks {
83 # Extract directory, taskfile, or global flags from WORDS so task can locate the Taskfile
84 typeset task_args
85 task_args=()
86 typeset i=2
87 while [ $i -le ${WORDS[#]} ]; do
88 case "${WORDS[i]}" in
89 (-g|--global)
90 task_args=("$task_args" "${WORDS[i]}")
91 ;;
92 (-d|--dir|-t|--taskfile)
93 if [ $((i+1)) -le ${WORDS[#]} ]; then
94 task_args=("$task_args" "${WORDS[i]}" "${WORDS[i+1]}")
95 i=$((i+1))
96 fi
97 ;;
98 (--dir=*|--taskfile=*)
99 task_args=("$task_args" "${WORDS[i]}")
100 ;;
101 (-d*|-t*)
102 task_args=("$task_args" "${WORDS[i]}")
103 ;;
104 esac
105 i=$((i+1))
106 done
107
108 # Extract tasks with descriptions
109 typeset line name desc
110 typeset IFS='
111'
112 for line in $(task "$task_args" --list-all 2>/dev/null | sed -n 's/^\* \([^:]*\):[[:space:]]*\(.*\)/\1\t\2/p'); do
113 name="${line%% *}"
114 desc="${line#* }"
115 if [ "$desc" ]; then
116 complete -P "$PREFIX" -D "$desc" -- "$name"
117 else
118 complete -P "$PREFIX" -- "$name"
119 fi
120 done
121
122 # Also complete any task aliases or tasks without descriptions
123 typeset tasks
124 tasks=($(task "$task_args" --silent --list-all 2>/dev/null))
125 if [ ${tasks[#]} -gt 0 ]; then
126 complete -P "$PREFIX" -- "$tasks"
127 fi
128}
129
130# kak: filetype=sh