diff --git a/.gitignore b/.gitignore index d070336..b7eed8e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea/* /target +/completion *.iml diff --git a/Cargo.lock b/Cargo.lock index d417e32..189f20a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,6 +99,15 @@ dependencies = [ "clap_lex", ] +[[package]] +name = "clap_complete" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "586a385f7ef2f8b4d86bddaa0c094794e7ccbfe5ffef1f434fe928143fc783a5" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.4.2" @@ -262,6 +271,7 @@ name = "osc-variant" version = "0.4.0" dependencies = [ "clap", + "clap_complete", "console", "quick-xml", "serde", diff --git a/Cargo.toml b/Cargo.toml index 875ab05..abecbdb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ description = "Anwendung zum Anpassen einer OSC-Datei an einen Standort" license = "MIT" readme = "README.md" +build = "build.rs" + [dependencies] clap = { version = "4.4", features = ["std", "help", "usage", "derive", "error-context"], default-features = false } serde = { version = "1.0", features = ["derive"] } @@ -15,6 +17,10 @@ quick-xml = { version = "0.30", features = ["escape-html", "serialize"], default console = "0.15" sha256 = "1.4" +[build-dependencies] +clap = { version = "4.4", features = ["std", "help", "usage", "derive", "error-context"], default-features = false } +clap_complete = "4.4" + [profile.release] opt-level = "s" codegen-units = 1 @@ -24,3 +30,6 @@ panic = "abort" [package.metadata.deb] copyright = "Copyright (c) 2023 Comprehensive Cancer Center Mainfranken" +assets = [ + ["completion/osc-variant.bash", "etc/bash_completion.d/", "644"] +] diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..8722d6a --- /dev/null +++ b/build.rs @@ -0,0 +1,44 @@ +/* + * MIT License + * + * Copyright (c) 2023 Comprehensive Cancer Center Mainfranken + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +use std::fs; +use std::io::Error; + +use clap_complete::generate_to; +use clap_complete::Shell::Bash; + +include!("src/cli.rs"); + +fn main() -> Result<(), Error> { + let mut cmd = build_cli(); + + let package_name = std::env::var("CARGO_CRATE_NAME").unwrap_or("osc-variant".to_string()); + + fs::remove_dir_all("completion")?; + fs::create_dir("completion")?; + + generate_to(Bash, &mut cmd, package_name.as_str(), "completion")?; + + Ok(()) +} diff --git a/src/cli.rs b/src/cli.rs index 0194f9d..52c1138 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -22,18 +22,23 @@ * SOFTWARE. */ -use clap::{Parser, Subcommand}; +use clap::{Command, CommandFactory, Parser, Subcommand}; + +#[allow(dead_code)] +fn build_cli() -> Command { + Cli::command() +} #[derive(Parser)] #[command(author, version, about, long_about = None)] #[command(propagate_version = true, arg_required_else_help(true))] pub struct Cli { #[command(subcommand)] - pub command: Command, + pub cmd: SubCommand, } #[derive(Subcommand)] -pub enum Command { +pub enum SubCommand { #[command( name = "sha256sum", about = "Berechne SHA256 Prüfsumme für die angegebene Datei" diff --git a/src/main.rs b/src/main.rs index 9d89fc5..017efe4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ use quick_xml::se::Serializer; use serde::Serialize; use sha256::digest; -use crate::cli::{Cli, Command}; +use crate::cli::{Cli, SubCommand}; use crate::model::onkostar_editor::OnkostarEditor; use crate::profile::Profile; @@ -111,8 +111,8 @@ fn read_profile(filename: String) -> Result { fn main() -> Result<(), Box> { let cli = Cli::parse(); - match cli.command { - Command::List { + match cli.cmd { + SubCommand::List { inputfile, sorted, filter, @@ -127,7 +127,7 @@ fn main() -> Result<(), Box> { } data.print_list(); } - Command::Tree { + SubCommand::Tree { inputfile, sorted, filter, @@ -142,7 +142,7 @@ fn main() -> Result<(), Box> { } OnkostarEditor::print_tree(&data); } - Command::Modify { + SubCommand::Modify { inputfile, profile, outputfile, @@ -193,7 +193,7 @@ fn main() -> Result<(), Box> { } } } - Command::Diff { + SubCommand::Diff { inputfile_a, inputfile_b, strict, @@ -209,7 +209,7 @@ fn main() -> Result<(), Box> { data_a.print_diff(data_b, strict); } - Command::Sha256Sum { inputfile } => { + SubCommand::Sha256Sum { inputfile } => { match fs::read_to_string(inputfile.clone()) { Ok(content) => { println!(