1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
# Generating and Installing Completion Scripts
Install shell completion scripts generated by your command-line tool.
## Overview
Command-line tools that you build with `ArgumentParser` include a built-in option for generating completion scripts, with support for Bash, Z shell, and Fish. To generate completions, run your command with the `--generate-completion-script` option to generate completions for your specific shell.
```
$ example --generate-completion-script bash
#compdef example
local context state state_descr line
_example_commandname="example"
typeset -A opt_args
_example() {
integer ret=1
local -a args
...
}
_example
```
The correct method of installing a completion script can depend on both your shell and your configuration.
### Installing Zsh Completions
If you have [`oh-my-zsh`](https://ohmyz.sh) installed, you already have a directory of automatically loading completion scripts — `.oh-my-zsh/completions`. Copy your new completion script to that directory.
```
$ example --generate-completion-script zsh > ~/.oh-my-zsh/completions/_example
```
> Your completion script must have the following filename format: `_example`.
Without `oh-my-zsh`, you'll need to add a path for completion scripts to your function path, and turn on completion script autoloading. First, add these lines to `~/.zshrc`:
```
fpath=(~/.zsh/completion $fpath)
autoload -U compinit
compinit
```
Next, create a directory at `~/.zsh/completion` and copy the completion script to the new directory.
### Installing Bash Completions
If you have [`bash-completion`](https://github.com/scop/bash-completion) installed, you can just copy your new completion script to the `/usr/local/etc/bash_completion.d` directory.
Without `bash-completion`, you'll need to source the completion script directly. Copy it to a directory such as `~/.bash_completions/`, and then add the following line to `~/.bash_profile` or `~/.bashrc`:
```
source ~/.bash_completions/example.bash
```
### Installing Fish Completions
Copy the completion script to any path listed in the environment variable `$fish_completion_path`. For example, a typical location is `~/.config/fish/completions/your_script.fish`.
|