Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cli completions + clap version bump to v4.5.17 #3251

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: Added completions command to generate shell completions via the clap_complete crate ([#3251](https://github.com/coral-xyz/anchor/pull/3251)).
zilayo marked this conversation as resolved.
Show resolved Hide resolved

### 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
12 changes: 11 additions & 1 deletion avm/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::{anyhow, Error, Result};
use avm::InstallTarget;
use clap::{Parser, Subcommand};
use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::generate;
use semver::Version;

pub const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -38,6 +39,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 +84,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 } => {
generate(shell, &mut Cli::command(), "avm", &mut std::io::stdout());
zilayo marked this conversation as resolved.
Show resolved Hide resolved
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
17 changes: 16 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ 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 clap_complete::generate;
use dirs::home_dir;
use flate2::read::GzDecoder;
use flate2::read::ZlibDecoder;
Expand Down Expand Up @@ -365,6 +366,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 +930,15 @@ fn process_command(opts: Opts) -> Result<()> {
address,
idl,
} => account(&opts.cfg_override, account_type, address, idl),
Command::Completions { shell } => {
generate(
zilayo marked this conversation as resolved.
Show resolved Hide resolved
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
```
Loading