From d96151b98b49850d40ba270fd9fa0656dd05bca7 Mon Sep 17 00:00:00 2001 From: Gabriel Lopes Rodrigues Date: Fri, 13 Oct 2023 12:00:16 -0300 Subject: [PATCH] Add shell completion and man page --- Cargo.lock | 27 +++++++++++++++++++++++++++ Cargo.toml | 19 ++++++++++++++++--- build.rs | 34 ++++++++++++++++++++++++++++++++++ src/cli.rs | 15 +++++++++++++++ src/main.rs | 16 +++------------- 5 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 build.rs create mode 100644 src/cli.rs diff --git a/Cargo.lock b/Cargo.lock index 8cdc2990..dadfcbde 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -253,6 +253,15 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ae8ba90b9d8b007efe66e55e48fb936272f5ca00349b5b0e89877520d35ea7" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.4.2" @@ -271,6 +280,16 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" +[[package]] +name = "clap_mangen" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b44f35c514163027542f7147797ff930523eea288e03642727348ef1a9666f6b" +dependencies = [ + "clap", + "roff", +] + [[package]] name = "color_quant" version = "1.1.0" @@ -1123,6 +1142,12 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "roff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316" + [[package]] name = "roxmltree" version = "0.18.0" @@ -1261,6 +1286,8 @@ name = "svg2pdf" version = "0.8.0" dependencies = [ "clap", + "clap_complete", + "clap_mangen", "fontdb", "image", "miniz_oxide", diff --git a/Cargo.toml b/Cargo.toml index 8d53a63a..d8ebe6f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,10 @@ members = ["tests"] [workspace.package] version = "0.8.0" -authors = ["Martin Haug ", "Laurenz Stampfl "] +authors = [ + "Martin Haug ", + "Laurenz Stampfl ", +] edition = "2021" repository = "https://github.com/typst/svg2pdf" readme = "README.md" @@ -19,6 +22,7 @@ authors.workspace = true edition.workspace = true repository.workspace = true license.workspace = true +build = "build.rs" [features] default = ["image"] @@ -34,14 +38,23 @@ name = "svg2pdf" path = "src/main.rs" required-features = ["cli"] +[build-dependencies] +clap = { version = "4.4.2", features = ["derive", "string"] } +clap_complete = "4.4.3" +clap_mangen = "0.2.14" + [dependencies] miniz_oxide = "0.7" pdf-writer = "0.9" usvg = { version = "0.36", default-features = false } -image = { version = "0.24", default-features = false, features = ["jpeg", "png", "gif"], optional = true } +image = { version = "0.24", default-features = false, features = [ + "jpeg", + "png", + "gif", +], optional = true } termcolor = { version = "1", optional = true } clap = { version = "4.4.2", features = ["derive"], optional = true } -fontdb = { version = "0.15", optional= true } +fontdb = { version = "0.15", optional = true } [dev-dependencies] usvg = { version = "0.36.0" } diff --git a/build.rs b/build.rs new file mode 100644 index 00000000..186ffcd2 --- /dev/null +++ b/build.rs @@ -0,0 +1,34 @@ +use std::{env, path::Path}; + +use clap::{CommandFactory, ValueEnum}; +use clap_complete::{generate_to, Shell}; + +mod args { + include!("src/cli.rs"); +} + +fn main() -> Result<(), std::io::Error> { + if !cfg!(feature = "cli") { + return Ok(()); + } + + let outdir_str = match env::var_os("OUT_DIR") { + None => return Ok(()), + Some(outdir) => outdir, + }; + + // Put the files in the same level as the binary (e.g. /target/debug folder) + let outdir_path = &Path::new(&outdir_str).ancestors().nth(3).unwrap(); + + let mut cmd = args::Args::command(); + + let man = clap_mangen::Man::new(cmd.clone()); + let mut manpage_file = std::fs::File::create(outdir_path.join("svg2pdf.1"))?; + man.render(&mut manpage_file)?; + + for shell in Shell::value_variants() { + generate_to(*shell, &mut cmd, "svg2pdf", &outdir_path).unwrap(); + } + + Ok(()) +} diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 00000000..905997c8 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,15 @@ +use std::path::PathBuf; + +use clap::Parser; + +#[derive(Debug, Parser)] +#[clap(about, version)] +pub struct Args { + /// Path to read SVG file from. + pub input: PathBuf, + /// Path to write PDF file to. + pub output: Option, + /// The number of SVG pixels per PDF points. + #[clap(long, default_value = "72.0")] + pub dpi: f32, +} diff --git a/src/main.rs b/src/main.rs index c87c7925..7c3841a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use std::io::{self, Write}; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process; use clap::Parser; @@ -8,17 +8,7 @@ use usvg::{TreeParsing, TreeTextToPath}; use svg2pdf::Options; -#[derive(Debug, Parser)] -#[clap(about, version)] -struct Args { - /// Path to read SVG file from. - input: PathBuf, - /// Path to write PDF file to. - output: Option, - /// The number of SVG pixels per PDF points. - #[clap(long, default_value = "72.0")] - dpi: f32, -} +mod cli; fn main() { if let Err(msg) = run() { @@ -28,7 +18,7 @@ fn main() { } fn run() -> Result<(), String> { - let args = Args::parse(); + let args = cli::Args::parse(); let name = Path::new(args.input.file_name().ok_or("Input path does not point to a file")?);