Skip to content

Commit

Permalink
avm, cli: Add completions command to generate shell completions (#3251)
Browse files Browse the repository at this point in the history
  • Loading branch information
zilayo authored Sep 16, 2024
1 parent e6a7d69 commit 4866680
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Add `LazyAccount` ([#3194](https://github.com/coral-xyz/anchor/pull/3194)).
- avm: Ask whether to install if the version is not installed with the `use` command ([#3230](https://github.com/coral-xyz/anchor/pull/3230)).
- cli: Warn if a manifest has `solana-program` dependency ([#3250](https://github.com/coral-xyz/anchor/pull/3250)).
- cli: Add completions command to generate shell completions via the clap_complete crate ([#3251](https://github.com/coral-xyz/anchor/pull/3251)).

### Fixes

Expand Down
64 changes: 47 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion avm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ path = "src/anchor/main.rs"
anyhow = "1.0.32"
cfg-if = "1.0.0"
cargo_toml = "0.19.2"
clap = { version = "4.2.4", features = ["derive"] }
clap = { version = "4.5.17", features = ["derive"] }
clap_complete = "4.5.26"
dirs = "4.0.0"
once_cell = "1.8.0"
reqwest = { version = "0.11.9", default-features = false, features = ["blocking", "json", "rustls-tls"] }
Expand Down
11 changes: 10 additions & 1 deletion avm/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{anyhow, Error, Result};
use avm::InstallTarget;
use clap::{Parser, Subcommand};
use clap::{CommandFactory, Parser, Subcommand};
use semver::Version;

pub const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -38,6 +38,11 @@ pub enum Commands {
List {},
#[clap(about = "Update to the latest Anchor version")]
Update {},
#[clap(about = "Generate shell completions for AVM")]
Completions {
#[clap(value_enum)]
shell: clap_complete::Shell,
},
}

// If `latest` is passed use the latest available version.
Expand Down Expand Up @@ -78,6 +83,10 @@ pub fn entry(opts: Cli) -> Result<()> {
Commands::Uninstall { version } => avm::uninstall_version(&version),
Commands::List {} => avm::list_versions(),
Commands::Update {} => avm::update(),
Commands::Completions { shell } => {
clap_complete::generate(shell, &mut Cli::command(), "avm", &mut std::io::stdout());
Ok(())
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ base64 = "0.21"
bincode = "1.3.3"
cargo_toml = "0.19.2"
chrono = "0.4.19"
clap = { version = "4.2.4", features = ["derive"] }
clap = { version = "4.5.17", features = ["derive"] }
clap_complete = "4.5.26"
dirs = "4.0"
flate2 = "1.0.19"
heck = "0.4.0"
Expand Down
16 changes: 15 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use anchor_lang_idl::convert::convert_idl;
use anchor_lang_idl::types::{Idl, IdlArrayLen, IdlDefinedFields, IdlType, IdlTypeDefTy};
use anyhow::{anyhow, Context, Result};
use checks::{check_anchor_version, check_deps, check_idl_build_feature, check_overflow};
use clap::Parser;
use clap::{CommandFactory, Parser};
use dirs::home_dir;
use flate2::read::GzDecoder;
use flate2::read::ZlibDecoder;
Expand Down Expand Up @@ -365,6 +365,11 @@ pub enum Command {
#[clap(long)]
idl: Option<String>,
},
/// Generates shell completions.
Completions {
#[clap(value_enum)]
shell: clap_complete::Shell,
},
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -924,6 +929,15 @@ fn process_command(opts: Opts) -> Result<()> {
address,
idl,
} => account(&opts.cfg_override, account_type, address, idl),
Command::Completions { shell } => {
clap_complete::generate(
shell,
&mut Opts::command(),
"anchor",
&mut std::io::stdout(),
);
Ok(())
}
}
}

Expand Down
39 changes: 39 additions & 0 deletions docs/src/pages/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,42 @@ Now verify the CLI is installed properly.
```shell
anchor --version
```

## Generating Shell Completions

Shell completions can be generated for `bash`, `elvish`, `fish`, `powershell`, and `zsh`.

### Bash

```bash
mkdir -p $HOME/.local/share/bash-completion/completions
anchor completions bash > $HOME/.local/share/bash-completion/completions/anchor
avm completions bash > $HOME/.local/share/bash-completion/completions/avm
exec bash
```

### Fish

```bash
mkdir -p $HOME/.config/fish/completions
anchor completions fish > $HOME/.config/fish/completions/anchor.fish
avm completions fish > $HOME/.config/fish/completions/avm.fish
source $HOME/.config/fish/config.fish
```

### Zsh

First ensure the following is in your `.zshrc` file. If using `oh-my-zsh` this step can be skipped.

```bash
autoload -U compinit
compinit -i
```

Next run:

```bash
anchor completions zsh | sudo tee /usr/local/share/zsh/site-functions/_anchor
avm completions zsh | sudo tee /usr/local/share/zsh/site-functions/_avm
exec zsh
```

0 comments on commit 4866680

Please sign in to comment.