blob: 4f119220feaa6224114228e40c9f5c349b695d53 [file] [log] [blame]
Matthew Maurerbd398542019-09-05 16:25:08 -07001#compdef cargo
2
3autoload -U regexp-replace
4
5zstyle -T ':completion:*:*:cargo:*' tag-order && \
6 zstyle ':completion:*:*:cargo:*' tag-order 'common-commands'
7
8_cargo() {
9local context state state_descr line
10typeset -A opt_args
11
12# leading items in parentheses are an exclusion list for the arguments following that arg
13# See: http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-Functions
14# - => exclude all other options
15# 1 => exclude positional arg 1
16# * => exclude all other args
17# +blah => exclude +blah
18_arguments \
19 '(- 1 *)'{-h,--help}'[show help message]' \
20 '(- 1 *)--list[list installed commands]' \
21 '(- 1 *)'{-V,--version}'[show version information]' \
22 {-v,--verbose}'[use verbose output]' \
23 --color'[colorization option]' \
24 '(+beta +nightly)+stable[use the stable toolchain]' \
25 '(+stable +nightly)+beta[use the beta toolchain]' \
26 '(+stable +beta)+nightly[use the nightly toolchain]' \
27 '1: :->command' \
28 '*:: :->args'
29
30case $state in
31 command)
32 _alternative 'common-commands:common:_cargo_cmds' 'all-commands:all:_cargo_all_cmds'
33 ;;
34
35 args)
36 case $words[1] in
37 bench)
38 _arguments \
39 '--features=[space separated feature list]' \
40 '--all-features[enable all available features]' \
41 '(-h, --help)'{-h,--help}'[show help message]' \
42 '(-j, --jobs)'{-j,--jobs}'[number of parallel jobs, defaults to # of CPUs]' \
43 "${command_scope_spec[@]}" \
44 '--manifest-path=[path to manifest]: :_files -/' \
45 '--no-default-features[do not build the default features]' \
46 '--no-run[compile but do not run]' \
47 '(-p,--package)'{-p=,--package=}'[package to run benchmarks for]:packages:_get_package_names' \
48 '--target=[target triple]' \
49 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
50 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
51 '--color=:colorization option:(auto always never)' \
52 ;;
53
54 build)
55 _arguments \
56 '--features=[space separated feature list]' \
57 '--all-features[enable all available features]' \
58 '(-h, --help)'{-h,--help}'[show help message]' \
59 '(-j, --jobs)'{-j,--jobs}'[number of parallel jobs, defaults to # of CPUs]' \
60 "${command_scope_spec[@]}" \
61 '--manifest-path=[path to manifest]: :_files -/' \
62 '--no-default-features[do not build the default features]' \
63 '(-p,--package)'{-p=,--package=}'[package to build]:packages:_get_package_names' \
64 '--release=[build in release mode]' \
65 '--target=[target triple]' \
66 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
67 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
68 '--color=:colorization option:(auto always never)' \
69 ;;
70
71 check)
72 _arguments \
73 '--features=[space separated feature list]' \
74 '--all-features[enable all available features]' \
75 '(-h, --help)'{-h,--help}'[show help message]' \
76 '(-j, --jobs)'{-j,--jobs}'[number of parallel jobs, defaults to # of CPUs]' \
77 "${command_scope_spec[@]}" \
78 '--manifest-path=[path to manifest]: :_files -/' \
79 '--no-default-features[do not check the default features]' \
80 '(-p,--package)'{-p=,--package=}'[package to check]:packages:_get_package_names' \
81 '--release=[check in release mode]' \
82 '--target=[target triple]' \
83 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
84 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
85 '--color=:colorization option:(auto always never)' \
86 ;;
87
88 clean)
89 _arguments \
90 '(-h, --help)'{-h,--help}'[show help message]' \
91 '--manifest-path=[path to manifest]: :_files -/' \
92 '(-p,--package)'{-p=,--package=}'[package to clean]:packages:_get_package_names' \
93 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
94 '--release[whether or not to clean release artifacts]' \
95 '--target=[target triple(default:all)]' \
96 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
97 '--color=:colorization option:(auto always never)' \
98 ;;
99
100 doc)
101 _arguments \
102 '--features=[space separated feature list]' \
103 '--all-features[enable all available features]' \
104 '(-h, --help)'{-h,--help}'[show help message]' \
105 '(-j, --jobs)'{-j,--jobs}'[number of parallel jobs, defaults to # of CPUs]' \
106 '--manifest-path=[path to manifest]: :_files -/' \
107 '--no-deps[do not build docs for dependencies]' \
108 '--no-default-features[do not build the default features]' \
109 '--document-private-items[include non-public items in the documentation]' \
110 '--open[open docs in browser after the build]' \
111 '(-p, --package)'{-p,--package}'=[package to document]' \
112 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
113 '--release[build artifacts in release mode, with optimizations]' \
114 '--target=[build for the target triple]' \
115 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
116 '--color=:colorization option:(auto always never)' \
117 ;;
118
119 fetch)
120 _arguments \
121 '(-h, --help)'{-h,--help}'[show help message]' \
122 '--manifest-path=[path to manifest]: :_files -/' \
123 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
124 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
125 '--color=:colorization option:(auto always never)' \
126 ;;
127
128 generate-lockfile)
129 _arguments \
130 '(-h, --help)'{-h,--help}'[show help message]' \
131 '--manifest-path=[path to manifest]: :_files -/' \
132 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
133 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
134 '--color=:colorization option:(auto always never)' \
135 ;;
136
137 git-checkout)
138 _arguments \
139 '(-h, --help)'{-h,--help}'[show help message]' \
140 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
141 '--reference=[REF]' \
142 '--url=[URL]' \
143 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
144 '--color=:colorization option:(auto always never)' \
145 ;;
146
147 help)
148 _arguments \
149 '(-h, --help)'{-h,--help}'[show help message]' \
150 '*: :_cargo_cmds' \
151 ;;
152
153 init)
154 _arguments \
155 '--lib[use library template]' \
156 '--vcs:initialize a new repo with a given VCS:(git hg none)' \
157 '(-h, --help)'{-h,--help}'[show help message]' \
158 '--name=[set the resulting package name]' \
159 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
160 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
161 '--color=:colorization option:(auto always never)' \
162 ;;
163
164 install)
165 _arguments \
166 '--bin=[only install the specified binary]' \
167 '--branch=[branch to use when installing from git]' \
168 '--color=:colorization option:(auto always never)' \
169 '--debug[build in debug mode instead of release mode]' \
170 '--example[install the specified example instead of binaries]' \
171 '--features=[space separated feature list]' \
172 '--all-features[enable all available features]' \
173 '--git=[URL from which to install the crate]' \
174 '(-h, --help)'{-h,--help}'[show help message]' \
175 '(-j, --jobs)'{-j,--jobs}'[number of parallel jobs, defaults to # of CPUs]' \
176 '--no-default-features[do not build the default features]' \
177 '--path=[local filesystem path to crate to install]: :_files -/' \
178 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
179 '--rev=[specific commit to use when installing from git]' \
180 '--root=[directory to install packages into]: :_files -/' \
181 '--tag=[tag to use when installing from git]' \
182 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
183 '--vers=[version to install from crates.io]' \
184 ;;
185
186 locate-project)
187 _arguments \
188 '(-h, --help)'{-h,--help}'[show help message]' \
189 '--manifest-path=[path to manifest]: :_files -/' \
190 ;;
191
192 login)
193 _arguments \
194 '(-h, --help)'{-h,--help}'[show help message]' \
195 '--host=[Host to set the token for]' \
196 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
197 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
198 '--color=:colorization option:(auto always never)' \
199 ;;
200
201 metadata)
202 _arguments \
203 '(-h, --help)'{-h,--help}'[show help message]' \
204 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
205 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
206 "--no-deps[output information only about the root package and don't fetch dependencies]" \
207 '--no-default-features[do not include the default feature]' \
208 '--manifest-path=[path to manifest]: :_files -/' \
209 '--features=[space separated feature list]' \
210 '--all-features[enable all available features]' \
211 '--format-version=[format version(default: 1)]' \
212 '--color=:colorization option:(auto always never)' \
213 ;;
214
215 new)
216 _arguments \
217 '--lib[use library template]' \
218 '--vcs:initialize a new repo with a given VCS:(git hg none)' \
219 '(-h, --help)'{-h,--help}'[show help message]' \
220 '--name=[set the resulting package name]' \
221 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
222 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
223 '--color=:colorization option:(auto always never)' \
224 ;;
225
226 owner)
227 _arguments \
228 '(-a, --add)'{-a,--add}'[add owner LOGIN]' \
229 '(-h, --help)'{-h,--help}'[show help message]' \
230 '--index[registry index]' \
231 '(-l, --list)'{-l,--list}'[list owners of a crate]' \
232 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
233 '(-r, --remove)'{-r,--remove}'[remove owner LOGIN]' \
234 '--token[API token to use when authenticating]' \
235 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
236 '--color=:colorization option:(auto always never)' \
237 ;;
238
239 package)
240 _arguments \
241 '(-h, --help)'{-h,--help}'[show help message]' \
242 '(-l, --list)'{-l,--list}'[print files included in a package without making one]' \
243 '--manifest-path=[path to manifest]: :_files -/' \
244 '--no-metadata[ignore warnings about a lack of human-usable metadata]' \
245 '--no-verify[do not build to verify contents]' \
246 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
247 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
248 '--color=:colorization option:(auto always never)' \
249 ;;
250
251 pkgid)
252 _arguments \
253 '(-h, --help)'{-h,--help}'[show help message]' \
254 '--manifest-path=[path to manifest]: :_files -/' \
255 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
256 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
257 '--color=:colorization option:(auto always never)' \
258 ;;
259
260 publish)
261 _arguments \
262 '(-h, --help)'{-h,--help}'[show help message]' \
263 '--host=[Host to set the token for]' \
264 '--manifest-path=[path to manifest]: :_files -/' \
265 '--no-verify[Do not verify tarball until before publish]' \
266 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
267 '--token[token to use when uploading]' \
268 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
269 '--color=:colorization option:(auto always never)' \
270 ;;
271
272 read-manifest)
273 _arguments \
274 '(-h, --help)'{-h,--help}'[show help message]' \
275 '--manifest-path=[path to manifest]: :_files -/' \
276 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
277 '--color=:colorization option:(auto always never)' \
278 ;;
279
280 run)
281 _arguments \
282 '--example=[name of the bin target]' \
283 '--features=[space separated feature list]' \
284 '--all-features[enable all available features]' \
285 '(-h, --help)'{-h,--help}'[show help message]' \
286 '(-j, --jobs)'{-j,--jobs}'[number of parallel jobs, defaults to # of CPUs]' \
287 '--manifest-path=[path to manifest]: :_files -/' \
288 '--bin=[name of the bin target]' \
289 '--no-default-features[do not build the default features]' \
290 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
291 '--release=[build in release mode]' \
292 '--target=[target triple]' \
293 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
294 '--color=:colorization option:(auto always never)' \
295 '*: :_normal' \
296 ;;
297
298 rustc)
299 _arguments \
300 '--color=:colorization option:(auto always never)' \
301 '--features=[features to compile for the package]' \
302 '--all-features[enable all available features]' \
303 '(-h, --help)'{-h,--help}'[show help message]' \
304 '(-j, --jobs)'{-j,--jobs}'=[number of parallel jobs, defaults to # of CPUs]' \
305 '--manifest-path=[path to the manifest to fetch dependencies for]: :_files -/' \
306 '--no-default-features[do not compile default features for the package]' \
307 '(-p, --package)'{-p,--package}'=[profile to compile for]' \
308 '--profile=[profile to build the selected target for]' \
309 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
310 '--release[build artifacts in release mode, with optimizations]' \
311 '--target=[target triple which compiles will be for]' \
312 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
313 "${command_scope_spec[@]}" \
314 ;;
315
316 rustdoc)
317 _arguments \
318 '--color=:colorization option:(auto always never)' \
319 '--features=[space-separated list of features to also build]' \
320 '--all-features[enable all available features]' \
321 '(-h, --help)'{-h,--help}'[show help message]' \
322 '(-j, --jobs)'{-j,--jobs}'=[number of parallel jobs, defaults to # of CPUs]' \
323 '--manifest-path=[path to the manifest to document]: :_files -/' \
324 '--no-default-features[do not build the `default` feature]' \
325 '--document-private-items[include non-public items in the documentation]' \
326 '--open[open the docs in a browser after the operation]' \
327 '(-p, --package)'{-p,--package}'=[package to document]' \
328 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
329 '--release[build artifacts in release mode, with optimizations]' \
330 '--target=[build for the target triple]' \
331 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
332 "${command_scope_spec[@]}" \
333 ;;
334
335 search)
336 _arguments \
337 '--color=:colorization option:(auto always never)' \
338 '(-h, --help)'{-h,--help}'[show help message]' \
339 '--host=[host of a registry to search in]' \
340 '--limit=[limit the number of results]' \
341 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
342 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
343 ;;
344
345 test)
346 _arguments \
347 '--features=[space separated feature list]' \
348 '--all-features[enable all available features]' \
349 '(-h, --help)'{-h,--help}'[show help message]' \
350 '(-j, --jobs)'{-j,--jobs}'[number of parallel jobs, defaults to # of CPUs]' \
351 '--manifest-path=[path to manifest]: :_files -/' \
352 '--test=[test name]: :_test_names' \
353 '--no-default-features[do not build the default features]' \
354 '--no-fail-fast[run all tests regardless of failure]' \
355 '--no-run[compile but do not run]' \
356 '(-p,--package)'{-p=,--package=}'[package to run tests for]:packages:_get_package_names' \
357 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
358 '--release[build artifacts in release mode, with optimizations]' \
359 '--target=[target triple]' \
360 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
361 '--color=:colorization option:(auto always never)' \
362 '1: :_test_names' \
363 '(--doc --bin --example --test --bench)--lib[only test library]' \
364 '(--lib --bin --example --test --bench)--doc[only test documentation]' \
365 '(--lib --doc --example --test --bench)--bin=[binary name]' \
366 '(--lib --doc --bin --test --bench)--example=[example name]' \
367 '(--lib --doc --bin --example --bench)--test=[test name]' \
368 '(--lib --doc --bin --example --test)--bench=[benchmark name]' \
369 '--message-format:error format:(human json short)' \
370 '--frozen[require lock and cache up to date]' \
371 '--locked[require lock up to date]'
372 ;;
373
374 uninstall)
375 _arguments \
376 '--bin=[only uninstall the binary NAME]' \
377 '--color=:colorization option:(auto always never)' \
378 '(-h, --help)'{-h,--help}'[show help message]' \
379 '(-q, --quiet)'{-q,--quiet}'[less output printed to stdout]' \
380 '--root=[directory to uninstall packages from]: :_files -/' \
381 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
382 ;;
383
384 update)
385 _arguments \
386 '--aggressive=[force dependency update]' \
387 '(-h, --help)'{-h,--help}'[show help message]' \
388 '--manifest-path=[path to manifest]: :_files -/' \
389 '(-p,--package)'{-p=,--package=}'[package to update]:packages:__get_package_names' \
390 '--precise=[update single dependency to PRECISE]: :' \
391 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
392 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
393 '--color=:colorization option:(auto always never)' \
394 ;;
395
396 verify-project)
397 _arguments \
398 '(-h, --help)'{-h,--help}'[show help message]' \
399 '--manifest-path=[path to manifest]: :_files -/' \
400 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
401 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
402 '--color=:colorization option:(auto always never)' \
403 ;;
404
405 version)
406 _arguments \
407 '(-h, --help)'{-h,--help}'[show help message]' \
408 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
409 '--color=:colorization option:(auto always never)' \
410 ;;
411
412 yank)
413 _arguments \
414 '(-h, --help)'{-h,--help}'[show help message]' \
415 '--index[registry index]' \
416 '(-q, --quiet)'{-q,--quiet}'[no output printed to stdout]' \
417 '--token[API token to use when authenticating]' \
418 '--undo[undo a yank, putting a version back into the index]' \
419 '(-v, --verbose)'{-v,--verbose}'[use verbose output]' \
420 '--color=:colorization option:(auto always never)' \
421 '--vers[yank version]' \
422 ;;
423 esac
424 ;;
425esac
426}
427
428_cargo_cmds(){
429local -a commands;commands=(
430'bench:execute all benchmarks of a local package'
431'build:compile the current package'
432'check:check the current package without compiling'
433'clean:remove generated artifacts'
434'doc:build package documentation'
435'fetch:fetch package dependencies'
436'generate-lockfile:create lockfile'
437'git-checkout:git checkout'
438'help:get help for commands'
439'init:create new package in current directory'
440'install:install a Rust binary'
441'locate-project:print "Cargo.toml" location'
442'login:login to remote server'
443'metadata:the metadata for a package in json'
444'new:create a new package'
445'owner:manage the owners of a crate on the registry'
446'package:assemble local package into a distributable tarball'
447'pkgid:print a fully qualified package specification'
448'publish:upload package to the registry'
449'read-manifest:print manifest in JSON format'
450'run:run the main binary of the local package'
451'rustc:compile a package and all of its dependencies'
452'rustdoc:build documentation for a package'
453'search:search packages on crates.io'
454'test:execute all unit and tests of a local package'
455'uninstall:remove a Rust binary'
456'update:update dependencies'
457'verify-project:check Cargo.toml'
458'version:show version information'
459'yank:remove pushed file from index'
460)
461_describe -t common-commands 'common commands' commands
462}
463
464_cargo_all_cmds(){
465local -a commands;commands=($(cargo --list))
466_describe -t all-commands 'all commands' commands
467}
468
469
470#FIXME: Disabled until fixed
471#gets package names from the manifest file
472_get_package_names()
473{
474}
475
476#TODO:see if it makes sense to have 'locate-project' to have non-json output.
477#strips package name from json stuff
478_locate_manifest(){
479local manifest=`cargo locate-project 2>/dev/null`
480regexp-replace manifest '\{"root":"|"\}' ''
481echo $manifest
482}
483
484# Extracts the values of "name" from the array given in $1 and shows them as
485# command line options for completion
486_get_names_from_array()
487{
488 local -a filelist;
489 local manifest=$(_locate_manifest)
490 if [[ -z $manifest ]]; then
491 return 0
492 fi
493
494 local last_line
495 local -a names;
496 local in_block=false
497 local block_name=$1
498 names=()
499 while read line
500 do
501 if [[ $last_line == "[[$block_name]]" ]]; then
502 in_block=true
503 else
504 if [[ $last_line =~ '.*\[\[.*' ]]; then
505 in_block=false
506 fi
507 fi
508
509 if [[ $in_block == true ]]; then
510 if [[ $line =~ '.*name.*=' ]]; then
511 regexp-replace line '^.*name *= *|"' ""
512 names+=$line
513 fi
514 fi
515
516 last_line=$line
517 done < $manifest
518 _describe $block_name names
519
520}
521
522#Gets the test names from the manifest file
523_test_names()
524{
525 _get_names_from_array "test"
526}
527
528#Gets the bench names from the manifest file
529_benchmark_names()
530{
531 _get_names_from_array "bench"
532}
533
534# These flags are mutually exclusive specifiers for the scope of a command; as
535# they are used in multiple places without change, they are expanded into the
536# appropriate command's `_arguments` where appropriate.
537set command_scope_spec
538command_scope_spec=(
539 '(--bin --example --test --lib)--bench=[benchmark name]: :_benchmark_names'
540 '(--bench --bin --test --lib)--example=[example name]'
541 '(--bench --example --test --lib)--bin=[binary name]'
542 '(--bench --bin --example --test)--lib=[library name]'
543 '(--bench --bin --example --lib)--test=[test name]'
544)
545
546_cargo