-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: nargo command to generate shell completions (#6413)
- Loading branch information
Showing
8 changed files
with
205 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
--- | ||
title: Setting up shell completions | ||
tags: [] | ||
sidebar_position: 3 | ||
--- | ||
|
||
The `nargo` binary provides a command to generate shell completions: | ||
|
||
```bash | ||
nargo generate-completion-script [shell] | ||
``` | ||
|
||
where `shell` must be one of `bash`, `elvish`, `fish`, `powershell`, and `zsh`. | ||
|
||
Below we explain how to install them in some popular shells. | ||
|
||
## Installing Zsh Completions | ||
|
||
If you have `oh-my-zsh` installed, you might already have a directory of automatically loading completion scripts — `.oh-my-zsh/completions`. | ||
If not, first create it: | ||
|
||
```bash | ||
mkdir -p ~/.oh-my-zsh/completions` | ||
``` | ||
|
||
Then copy the completion script to that directory: | ||
|
||
```bash | ||
nargo generate-completion-script zsh > ~/.oh-my-zsh/completions/_nargo | ||
``` | ||
|
||
Without `oh-my-zsh`, you’ll need to add a path for completion scripts to your function path, and turn on completion script auto-loading. | ||
First, add these lines to `~/.zshrc`: | ||
|
||
```bash | ||
fpath=(~/.zsh/completions $fpath) | ||
autoload -U compinit | ||
compinit | ||
``` | ||
|
||
Next, create a directory at `~/.zsh/completions`: | ||
|
||
```bash | ||
mkdir -p ~/.zsh/completions | ||
``` | ||
|
||
Then copy the completion script to that directory: | ||
|
||
```bash | ||
nargo generate-completion-script zsh > ~/.zsh/completions/_nargo | ||
``` | ||
|
||
## Installing Bash Completions | ||
|
||
If you have [bash-completion](https://github.com/scop/bash-completion) installed, you can just copy the completion script to the `/usr/local/etc/bash_completion.d` directory: | ||
|
||
```bash | ||
nargo generate-completion-script bash > /usr/local/etc/bash_completion.d/nargo | ||
``` | ||
|
||
Without `bash-completion`, you’ll need to source the completion script directly. | ||
First create a directory such as `~/.bash_completions/`: | ||
|
||
```bash | ||
mkdir ~/.bash_completions/ | ||
``` | ||
|
||
Copy the completion script to that directory: | ||
|
||
```bash | ||
nargo generate-completion-script bash > ~/.bash_completions/nargo.bash | ||
``` | ||
|
||
Then add the following line to `~/.bash_profile` or `~/.bashrc`: | ||
|
||
|
||
```bash | ||
source ~/.bash_completions/nargo.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/nargo.fish`: | ||
|
||
```bash | ||
nargo generate-completion-script fish > ~/.config/fish/completions/nargo.fish | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tooling/nargo_cli/src/cli/generate_completion_script_cmd.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use clap::{Args, CommandFactory}; | ||
use clap_complete::Shell; | ||
|
||
use crate::errors::CliError; | ||
|
||
use super::NargoCli; | ||
|
||
/// Generates a shell completion script for your favorite shell | ||
#[derive(Debug, Clone, Args)] | ||
pub(crate) struct GenerateCompletionScriptCommand { | ||
/// The shell to generate completions for. One of: bash, elvish, fish, powershell, zsh | ||
pub(crate) shell: String, | ||
} | ||
|
||
pub(crate) fn run(command: GenerateCompletionScriptCommand) -> Result<(), CliError> { | ||
let shell = match command.shell.to_lowercase().as_str() { | ||
"bash" => Shell::Bash, | ||
"elvish" => Shell::Elvish, | ||
"fish" => Shell::Fish, | ||
"powershell" => Shell::PowerShell, | ||
"zsh" => Shell::Zsh, | ||
_ => { | ||
return Err(CliError::Generic( | ||
"Invalid shell. Supported shells are: bash, elvish, fish, powershell, zsh" | ||
.to_string(), | ||
)) | ||
} | ||
}; | ||
|
||
clap_complete::generate(shell, &mut NargoCli::command(), "nargo", &mut std::io::stdout()); | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.