_cargo 23 KB

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