Skip to content

Commit

Permalink
feat: add completions generation for shells (prefix-dev#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
swarnimarun authored Dec 19, 2023
1 parent 5ade85f commit cfb664f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 15 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ indexmap = "2.1.0"
dunce = "1.0.4"
fs-err = "2.11.0"
which = "5.0.0"
clap_complete = "4.4.4"

[dev-dependencies]
insta = { version = "1.34.0", features = ["yaml"] }
Expand Down
29 changes: 29 additions & 0 deletions docs/cli_usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# CLI Usage

## Shell Completions

We support shell completions through clap-complete.
You can generate them for your shell using the `--generate` command.

Eg,
```sh
rattler-build --generate=zsh > ${ZSH_COMPLETIONS_PATH:~/.zsh/completions}/_rattler-build
compinit
```

Ensure that whereever you install the is pointed to by your FPATH (for zsh or equivalent in other shells).
Now you can use TAB or your configured completion key. :3

```sh
$ rattler-build <TAB>
build -- Build a package
help -- Print this message or the help of the given subcommand(s)
rebuild -- Rebuild a package
test -- Test a package
```

Example for Fish Shell just generate the `completions.fish` and add to `~/.config/fish/completions`.
```sh
rattler-build --generate=fish > ${ZSH_COMPLETIONS_PATH:~/.config/fish/completions}/rattler-build.fish
```

3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ nav:

- Package specification: package_spec.md
- Compilers and cross compilation: compilers.md
- Automatic recipe linting: automatic_linting.md
- Testing packages: testing.md
- Reproducible builds: rebuild.md

- Internals: internals.md
- Recipe file: recipe_file.md

- CLI Usage: cli_usage.md
- Automatic recipe linting: automatic_linting.md

plugins:
- search
Expand Down
9 changes: 2 additions & 7 deletions rust-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,8 @@ mod tests {
let help_test = rattler()
// no heap allocations happen here, ideally!
.with_args(Vec::<&str>::new())
.map(|out| out.stderr)
.map(|s| {
#[cfg(target_family = "unix")]
return s.starts_with(b"Usage: rattler-build [OPTIONS]");
#[cfg(target_family = "windows")]
return s.starts_with(b"Usage: rattler-build.exe [OPTIONS]");
})
.map(|out| out.stdout)
.map(|s| s.starts_with(b"Usage: rattler-build [OPTIONS]"))
.unwrap();
assert!(help_test);
}
Expand Down
32 changes: 25 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This is the main entry point for the `rattler-build` binary.
use clap::{arg, crate_version, Parser};
use clap::{arg, crate_version, CommandFactory, Parser};

use clap_verbosity_flag::{InfoLevel, Verbosity};
use dunce::canonicalize;
Expand Down Expand Up @@ -59,8 +59,12 @@ enum SubCommands {
#[derive(Parser)]
#[clap(version = crate_version!())]
struct App {
// If provided, outputs the completion file for given shell
#[arg(long = "generate", value_enum)]
generator: Option<clap_complete::Shell>,

#[clap(subcommand)]
subcommand: SubCommands,
subcommand: Option<SubCommands>,

#[command(flatten)]
verbose: Verbosity<InfoLevel>,
Expand Down Expand Up @@ -220,11 +224,25 @@ async fn main() -> miette::Result<()> {
)
.init();

match args.subcommand {
SubCommands::Build(args) => run_build_from_args(args, multi_progress).await,
SubCommands::Test(args) => run_test_from_args(args).await,
SubCommands::Rebuild(args) => rebuild_from_args(args).await,
SubCommands::Upload(args) => upload_from_args(args).await,
if let Some(generator) = args.generator {
let mut cmd = App::command();
tracing::info!("Generating completion file for {generator:?}...");
fn print_completions<G: clap_complete::Generator>(gen: G, cmd: &mut clap::Command) {
clap_complete::generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
}
print_completions(generator, &mut cmd);
Ok(())
} else {
match args.subcommand {
Some(SubCommands::Build(args)) => run_build_from_args(args, multi_progress).await,
Some(SubCommands::Test(args)) => run_test_from_args(args).await,
Some(SubCommands::Rebuild(args)) => rebuild_from_args(args).await,
Some(SubCommands::Upload(args)) => upload_from_args(args).await,
None => {
_ = App::command().print_long_help();
Ok(())
}
}
}
}

Expand Down

0 comments on commit cfb664f

Please sign in to comment.