Skip to content

Commit

Permalink
Add shell completion and man page
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgalopes committed Oct 17, 2023
1 parent 4ba677e commit d96151b
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 16 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ members = ["tests"]

[workspace.package]
version = "0.8.0"
authors = ["Martin Haug <[email protected]>", "Laurenz Stampfl <[email protected]>"]
authors = [
"Martin Haug <[email protected]>",
"Laurenz Stampfl <[email protected]>",
]
edition = "2021"
repository = "https://github.com/typst/svg2pdf"
readme = "README.md"
Expand All @@ -19,6 +22,7 @@ authors.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true
build = "build.rs"

[features]
default = ["image"]
Expand All @@ -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" }
34 changes: 34 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
15 changes: 15 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>,
/// The number of SVG pixels per PDF points.
#[clap(long, default_value = "72.0")]
pub dpi: f32,
}
16 changes: 3 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::path::Path;
use std::process;

use clap::Parser;
Expand All @@ -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<PathBuf>,
/// 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() {
Expand All @@ -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")?);
Expand Down

0 comments on commit d96151b

Please sign in to comment.