Skip to content

Commit

Permalink
fix: improve completions dx (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
swarnimarun authored Jan 3, 2024
1 parent af5c317 commit 06bc026
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
6 changes: 3 additions & 3 deletions docs/cli_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
## Shell Completions

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

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

Expand All @@ -24,5 +24,5 @@ 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
rattler-build completion --shell=fish > ${ZSH_COMPLETIONS_PATH:~/.config/fish/completions}/rattler-build.fish
```
52 changes: 30 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,20 @@ enum SubCommands {

/// Upload a package
Upload(UploadOpts),

/// Generate shell completion script
Completion(ShellCompletion),
}

#[derive(Parser)]
struct ShellCompletion {
#[arg(short, long)]
shell: clap_complete::Shell,
}

#[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: Option<SubCommands>,

Expand Down Expand Up @@ -223,7 +228,7 @@ struct ArtifactoryOpts {
channel: String,

/// Your Artifactory username
#[arg(short, long, env = "ARTIFACTORY_USERNAME")]
#[arg(short = 'r', long, env = "ARTIFACTORY_USERNAME")]
username: Option<String>,

/// Your Artifactory password
Expand Down Expand Up @@ -269,24 +274,27 @@ async fn main() -> miette::Result<()> {
)
.init();

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(())
match args.subcommand {
Some(SubCommands::Completion(ShellCompletion { shell })) => {
let mut cmd = App::command();
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(shell, &mut cmd);
Ok(())
}
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 06bc026

Please sign in to comment.