Skip to content

Commit

Permalink
feat!: use subcommands for shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Apr 14, 2024
1 parent 91e797c commit adbb270
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
21 changes: 17 additions & 4 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clap::Parser;
use clap::{Parser, Subcommand};
use std::io::{Error, ErrorKind};
use std::path::PathBuf;

Expand Down Expand Up @@ -32,10 +32,23 @@ pub struct Args {
#[arg(short, long)]
pub inspect: bool,

// /// Generate shell completions file
// /// for the specified shell
// #[arg(long, value_name = "SHELL")]
// pub completions: Option<String>,
#[command(subcommand)]
pub command: Option<Commands>,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
/// Generate shell completions file
/// for the specified shell
#[arg(long, value_name = "SHELL")]
pub completions: Option<String>,
Completions {
/// The shell to generate completions for
#[arg(value_name = "SHELL")]
shell: String,
},
}

struct IsDefault {
Expand All @@ -56,7 +69,7 @@ impl IsDefault {
seance: cli.seance == defaults.seance,
unbury: cli.unbury == defaults.unbury,
inspect: cli.inspect == defaults.inspect,
completions: cli.completions == defaults.completions,
completions: cli.command.is_none(),
}
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ use std::process::ExitCode;
fn main() -> ExitCode {
let cli = args::Args::parse();

if let Some(shell) = cli.completions.as_deref() {
if ["nu", "nushell"].contains(&shell) {
let shell = Nushell;
generate(shell, &mut args::Args::command(), "rip", &mut io::stdout());
} else {
let shell = Shell::from_str(shell, true).unwrap_or_else(|_| {
eprintln!("Invalid shell specification: {}", shell);
std::process::exit(1);
});
generate(shell, &mut args::Args::command(), "rip", &mut io::stdout());
match &cli.command {
Some(args::Commands::Completions { shell }) => {
if "nu" == shell || "nushell" == shell {
let shell = Nushell;
generate(shell, &mut args::Args::command(), "rip", &mut io::stdout());
} else {
let shell = Shell::from_str(shell, true).unwrap_or_else(|_| {
eprintln!("Invalid shell specification: {}", shell);
std::process::exit(1);
});
generate(shell, &mut args::Args::command(), "rip", &mut io::stdout());
}
return ExitCode::SUCCESS;
}
return ExitCode::SUCCESS;
None => {}
}

let mode = util::ProductionMode;
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rip2::args::{validate_args, Args};
use rip2::args::{validate_args, Args, Commands};
use rip2::util::TestMode;
use rip2::{copy_file, move_target};
use rstest::rstest;
Expand All @@ -16,7 +16,7 @@ use std::os::unix::fs::FileTypeExt;
#[rstest]
fn test_validation() {
let bad_completions = Args {
completions: Some("bash".to_string()),
command: Some(Commands::Completions { shell: "bash".to_string() }),
decompose: true,
..Args::default()
};
Expand Down

0 comments on commit adbb270

Please sign in to comment.