From b8a31521b0ba45d449876ec205df533afcced701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BClker?= Date: Sat, 10 Aug 2024 22:13:52 +0200 Subject: [PATCH] text-rendering: use clap for cli interface This is part of the ongoing effort to remove our own cli parser --- tests/runners/text-rendering/Cargo.toml | 3 +- tests/runners/text-rendering/src/main.rs | 71 +++++++----------------- 2 files changed, 23 insertions(+), 51 deletions(-) diff --git a/tests/runners/text-rendering/Cargo.toml b/tests/runners/text-rendering/Cargo.toml index 57907f74..25c2ec41 100644 --- a/tests/runners/text-rendering/Cargo.toml +++ b/tests/runners/text-rendering/Cargo.toml @@ -2,9 +2,10 @@ name = "text-rendering" version = "0.1.0" edition = "2021" +description = "A testrunner for the text rendering tests" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] font = { workspace = true } -cli = { workspace = true } +clap = { workspace = true } diff --git a/tests/runners/text-rendering/src/main.rs b/tests/runners/text-rendering/src/main.rs index 2255ad4f..4d390218 100644 --- a/tests/runners/text-rendering/src/main.rs +++ b/tests/runners/text-rendering/src/main.rs @@ -1,43 +1,24 @@ //! A Tool used for [Unicode Rendering Tests](https://github.com/unicode-org/text-rendering-tests) -//! -use cli::CommandLineArgumentParser; -use font::ttf::{Font, TTFParseError}; use std::{fs, io}; -#[derive(Debug, Default, CommandLineArgumentParser)] -struct ArgumentParser { - #[argument( - may_be_omitted, - short_name = 'f', - long_name = "font", - description = "Specify the font to be used" - )] +use clap::Parser; +use font::ttf::{Font, TTFParseError}; + +#[derive(Debug, Default, Parser)] +#[command(version, about, long_about = None)] +struct Arguments { + /// Specify the font to be used + #[arg(short = 'f', long = "font")] font_path: Option, - #[argument( - may_be_omitted, - short_name = 'r', - long_name = "render", - description = "The characters to be rendered" - )] + /// The characters to be rendered + #[arg(short = 'r', long = "render")] text: Option, - #[argument( - may_be_omitted, - short_name = 't', - long_name = "testcase", - description = "The name of the current test case" - )] + /// The name of the current test case + #[arg(short = 't', long = "testcase")] testcase: Option, - - #[argument( - flag, - short_name = 'v', - long_name = "version", - description = "Show package version" - )] - version: bool, } #[derive(Debug)] @@ -49,25 +30,15 @@ enum Error { } fn main() -> Result<(), Error> { - let arguments = match ArgumentParser::parse() { - Ok(arguments) => arguments, - Err(_) => { - println!("{}", ArgumentParser::help()); - return Ok(()); - }, - }; + let args = Arguments::parse(); + + let font_bytes = fs::read(args.font_path.expect("No font path provided")).map_err(Error::IO)?; + let font = Font::new(&font_bytes).map_err(Error::Font)?; + let svg = font.render_as_svg( + &args.text.expect("No text provided"), + &args.testcase.expect("No testcase name provided"), + ); + println!("{svg}"); - if arguments.version { - println!("{}", env!("CARGO_PKG_VERSION")); - } else { - let font_bytes = - fs::read(arguments.font_path.expect("No font path provided")).map_err(Error::IO)?; - let font = Font::new(&font_bytes).map_err(Error::Font)?; - let svg = font.render_as_svg( - &arguments.text.expect("No text provided"), - &arguments.testcase.expect("No testcase name provided"), - ); - println!("{svg}"); - } Ok(()) }