diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f5e8fad19..11b76fa69d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ most recent changes however. ## May +- Rye's `self` command now includes a `completion` subcommand to generate + a completion script for your shell. + - The downloaded Python distributions are now validated against the SHA-256 hashes. diff --git a/Cargo.lock b/Cargo.lock index ad53bc660a..63e14e22db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -144,6 +144,15 @@ dependencies = [ "terminal_size", ] +[[package]] +name = "clap_complete" +version = "4.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a19591b2ab0e3c04b588a0e04ddde7b9eaa423646d1b4a8092879216bf47473" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.2.0" @@ -855,6 +864,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "clap_complete", "console", "curl", "decompress", diff --git a/rye/Cargo.toml b/rye/Cargo.toml index 823de16c50..8bdf609ea6 100644 --- a/rye/Cargo.toml +++ b/rye/Cargo.toml @@ -9,6 +9,7 @@ license = "MIT" [dependencies] anyhow = { version = "1.0.70", features = ["backtrace"] } clap = { version = "4.2.2", default-features = false, features = ["derive", "usage", "wrap_help", "std"] } +clap_complete = "4.2.1" console = "0.15.5" curl = { version = "0.4.44", features = ["ssl", "static-curl", "static-ssl"] } decompress = { version = "0.6.0", default-features = false, features = ["tarzst", "targz"] } diff --git a/rye/src/cli/rye.rs b/rye/src/cli/rye.rs index f4c2a517a7..e0981355a2 100644 --- a/rye/src/cli/rye.rs +++ b/rye/src/cli/rye.rs @@ -1,7 +1,8 @@ use std::process::Command; use anyhow::{bail, Context, Error}; -use clap::Parser; +use clap::{CommandFactory, Parser}; +use clap_complete::Shell; /// Rye self management #[derive(Parser, Debug)] @@ -10,6 +11,14 @@ pub struct Args { command: SubCommand, } +/// Generates a completion script for a shell. +#[derive(Parser, Debug)] +pub struct CompletionCommand { + /// The shell to generate a completion script for (defaults to 'bash'). + #[arg(short, long)] + shell: Option, +} + /// Performs an update of rye. /// /// This currently just is an alias to running cargo install again with the @@ -29,15 +38,28 @@ pub struct UpdateCommand { #[derive(Parser, Debug)] enum SubCommand { + Completion(CompletionCommand), Update(UpdateCommand), } pub fn execute(cmd: Args) -> Result<(), Error> { match cmd.command { + SubCommand::Completion(args) => completion(args), SubCommand::Update(args) => update(args), } } +fn completion(args: CompletionCommand) -> Result<(), Error> { + clap_complete::generate( + args.shell.unwrap_or(Shell::Bash), + &mut super::Args::command(), + "rye", + &mut std::io::stdout(), + ); + + Ok(()) +} + fn update(args: UpdateCommand) -> Result<(), Error> { let mut helper = rename_helper::RenameHelper::new()?; let mut cmd = Command::new("cargo");