Skip to content

Instantly share code, notes, and snippets.

@aoyama-val
Created March 24, 2024 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoyama-val/6f7fed440d984483c7829ce72964bdbd to your computer and use it in GitHub Desktop.
Save aoyama-val/6f7fed440d984483c7829ce72964bdbd to your computer and use it in GitHub Desktop.
zsh cargo completion that only meets my needs
#compdef cargo
_cargo() {
local context curcontext=$curcontext state line
declare -A opt_args
local ret=1
local -a common_options
_arguments -C \
'(-h --help)'{-h,--help}'[show this help message and exit]' \
--version"[show program's version number and exit]" \
'1: :__cargo_commands' \
'*:: :->args' \
&& ret=0
case $state in
(args)
case $words[1] in
run|r)
_arguments -s : \
'--bin[Name of the bin target to run]' \
'--example[Name of the example target to run]' \
'1: :__cargo_run_options' \
&& ret=0
;;
*)
_files
;;
esac
esac
}
__cargo_run_options() {
local -a files
files=($(print -r -- src/bin/*.rs))
files=(${(R)files##*/})
files=(${(R)files%.rs})
_values 'files' ${(@)files}
}
__cargo_commands() {
local -a _c
_c=(
'add:Add dependencies to a Cargo.toml manifest file'
'bench:Execute all benchmarks of a local package'
'build:Compile a local package and all of its dependencies'
'check:Check a local package and all of its dependencies for errors'
'clean:Remove artifacts that cargo has generated in the past'
'clippy:Checks a package to catch common mistakes and improve your Rust code.'
'config:Inspect configuration values'
'doc:Build a package'\''s documentation'
'fetch:Fetch dependencies of a package from the network'
'fix:Automatically fix lint warnings reported by rustc'
'fmt:Formats all bin and lib files of the current crate using rustfmt.'
'generate-lockfile:Generate the lockfile for a package'
'git-checkout:This command has been removed'
'help:Displays help for a cargo subcommand'
'init:Create a new cargo package in an existing directory'
'install:Install a Rust binary. Default location is $HOME/.cargo/bin'
'locate-project:Print a JSON representation of a Cargo.toml file'\''s location'
'login:Log in to a registry.'
'logout:Remove an API token from the registry locally'
'metadata:Output the resolved dependencies of a package'
'miri'
'new:Create a new cargo package at <path>'
'owner:Manage the owners of a crate on the registry'
'package:Assemble the local package into a distributable tarball'
'pkgid:Print a fully qualified package specification'
'publish:Upload a package to the registry'
'read-manifest:Print a JSON representation of a Cargo.toml manifest.'
'remove:Remove dependencies from a Cargo.toml manifest file'
'report:Generate and display various kinds of reports'
'run:Run a binary or example of the local package'
'rustc:Compile a package, and pass extra options to the compiler'
'rustdoc:Build a package'\''s documentation, using specified custom flags.'
'search:Search packages in the registry. Default registry is crates.io'
'test:Execute all unit and integration tests and build examples of a local package'
'tree:Display a tree visualization of a dependency graph'
'uninstall:Remove a Rust binary'
'update:Update dependencies as recorded in the local lock file'
'vendor:Vendor all dependencies for a project locally'
'verify-project:Check correctness of crate manifest'
'version:Show version information'
'yank:Remove a pushed crate from the index'
)
_describe -t commands Commands _c
}
_cargo "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment