From 91bb342c06cdb0ff6ff0098d98890eb36a61ef86 Mon Sep 17 00:00:00 2001 From: Luka Hartwig Date: Tue, 11 Apr 2023 18:47:06 +0200 Subject: [PATCH] Add completion command --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/main.rs | 14 +++++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 89fda58..fb8c2eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,6 +166,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01c22dcfb410883764b29953103d9ef7bb8fe21b3fa1158bc99986c2067294bd" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.2.0" @@ -786,6 +795,7 @@ dependencies = [ "chrono", "chrono-humanize", "clap", + "clap_complete", "home", "refinery", "rusqlite", diff --git a/Cargo.toml b/Cargo.toml index f9026fb..72ad48c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ anyhow = "1.0.70" chrono = "0.4.24" chrono-humanize = "0.2.2" clap = { version = "4.2.1", features = ["derive"] } +clap_complete = "4.2.0" home = "0.5.4" refinery = { version = "0.8.7", features = ["rusqlite-bundled"] } rusqlite = { version = "0.28.0", features = ["bundled", "chrono"] } diff --git a/src/main.rs b/src/main.rs index 4324737..89b3394 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ use anyhow::Result; use chrono_humanize::HumanTime; -use clap::{Parser, Subcommand}; -use std::fs; +use clap::{CommandFactory, Parser, Subcommand}; +use clap_complete::{generate, Shell}; +use std::{fs, io}; mod store; mod todo; @@ -12,6 +13,8 @@ use crate::todo::{Todo, TodoStatus}; #[derive(Debug, Parser)] #[clap(name = "todo", version)] struct App { + #[arg(short)] + fail: String, #[clap(subcommand)] command: Commands, } @@ -27,6 +30,8 @@ enum Commands { Set { id: u32, status: TodoStatus }, /// Remove done todos Prune, + /// Generates completions + Completions { shell: Shell }, } fn main() -> Result<()> { @@ -38,7 +43,7 @@ fn main() -> Result<()> { if !path.exists() { fs::create_dir_all(path.as_path())?; } - + let store = Store::open(path.join("todo.db"))?; match app.command { @@ -65,6 +70,9 @@ fn main() -> Result<()> { Commands::Prune => { store.prune_todos().expect("failed pruning todos"); } + Commands::Completions { shell } => { + generate(shell, &mut App::command(), "todo", &mut io::stdout()); + } } Ok(())