Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shell completions #3

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -16,6 +16,7 @@ autobins = false

[dependencies]
clap = { version = "4.4.18", features = ["derive"] }
clap_complete = "4.4.18"
walkdir = "1"
time = "0.1"
error-chain = "0.12"
Expand Down
7 changes: 6 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ pub struct Args {
/// Print some info about TARGET before
/// burying
#[arg(short, long)]
pub inspect: bool
pub inspect: bool,

/// Generate shell completions file
/// for the specified shell
#[arg(long, value_name = "SHELL")]
pub completions: Option<String>,
}
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::io::{BufRead, BufReader, Error, ErrorKind, Write};
use std::io::{BufRead, BufReader, Error, ErrorKind, Write, stdout};
use std::os::unix::fs::{FileTypeExt, PermissionsExt};
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::{env, fs, io};
use clap::{CommandFactory, Parser};
use clap::{CommandFactory, Parser, ValueEnum};
use clap_complete::{generate, Shell};
use walkdir::WalkDir;

mod util;
Expand All @@ -23,6 +24,16 @@ struct RecordItem<'a> {

fn main() -> ExitCode {
let cli = args::Args::parse();

if let Some(shell) = cli.completions.as_deref() {
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 stdout());
return ExitCode::SUCCESS;
}

if let Err(ref e) = run(cli) {
println!("Exception: {}", e);
return ExitCode::FAILURE;
Expand Down Expand Up @@ -616,8 +627,9 @@ mod tests {
graveyard: Some(test_env.graveyard.clone()),
decompose: false,
seance: false,
unbury: Option::None,
unbury: None,
inspect: false,
completions: None,
});

// Verify that the file no longer exists
Expand All @@ -644,6 +656,7 @@ mod tests {
seance: false,
unbury: Some(Vec::new()),
inspect: false,
completions: None,
});

// Verify that the file exists in the original location with the correct data
Expand Down