From 811262cf011e490e9c6d1a743d3f9ff1a180a583 Mon Sep 17 00:00:00 2001 From: oech3 <79379754+oech3@users.noreply.github.com> Date: Sat, 8 Aug 2026 00:34:14 +0900 Subject: [PATCH] ptx: handle -g and -w by clap & reject 0 --- src/uu/ptx/src/ptx.rs | 28 ++++++---------------------- tests/by-util/test_ptx.rs | 2 ++ 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/uu/ptx/src/ptx.rs b/src/uu/ptx/src/ptx.rs index eb3ddc53710..e72c713a081 100644 --- a/src/uu/ptx/src/ptx.rs +++ b/src/uu/ptx/src/ptx.rs @@ -12,14 +12,12 @@ use std::ffi::{OsStr, OsString}; use std::fmt::Write as FmtWrite; use std::fs::File; use std::io::{BufRead, BufReader, BufWriter, Read, Write, stdin, stdout}; -use std::num::ParseIntError; use std::path::Path; -use clap::{Arg, ArgAction, Command}; +use clap::{Arg, ArgAction, Command, value_parser}; use regex::Regex; -use thiserror::Error; use uucore::display::Quotable; -use uucore::error::{FromIo, UError, UResult, USimpleError, UUsageError}; +use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; use uucore::format_usage; use uucore::translate; @@ -189,14 +187,6 @@ struct WordRef { filename: OsString, } -#[derive(Debug, Error)] -enum PtxError { - #[error("{0}")] - ParseError(ParseIntError), -} - -impl UError for PtxError {} - fn get_config(matches: &mut clap::ArgMatches) -> UResult { let mut config = Config::default(); let err_msg = "parsing options failed"; @@ -240,20 +230,12 @@ fn get_config(matches: &mut clap::ArgMatches) -> UResult { .clone_into(&mut config.trunc_str); } if matches.contains_id(options::WIDTH) { - config.line_width = matches - .get_one::(options::WIDTH) - .expect(err_msg) - .parse() - .map_err(PtxError::ParseError)?; + config.line_width = *matches.get_one::(options::WIDTH).unwrap() as usize; } else if matches.get_flag(options::TYPESET_MODE) { config.line_width = 100; } if matches.contains_id(options::GAP_SIZE) { - config.gap_size = matches - .get_one::(options::GAP_SIZE) - .expect(err_msg) - .parse() - .map_err(PtxError::ParseError)?; + config.gap_size = *matches.get_one::(options::GAP_SIZE).unwrap() as usize; } if let Some(format) = matches.get_one::(options::FORMAT) { config.format = match format.as_str() { @@ -1058,6 +1040,7 @@ pub fn uu_app() -> Command { Arg::new(options::GAP_SIZE) .short('g') .long(options::GAP_SIZE) + .value_parser(value_parser!(u64).range(1..)) .help(translate!("ptx-help-gap-size")) .value_name("NUMBER"), ) @@ -1098,6 +1081,7 @@ pub fn uu_app() -> Command { Arg::new(options::WIDTH) .short('w') .long(options::WIDTH) + .value_parser(value_parser!(u64).range(1..)) .help(translate!("ptx-help-width")) .value_name("NUMBER"), ) diff --git a/tests/by-util/test_ptx.rs b/tests/by-util/test_ptx.rs index 777f6dc1311..f7717869d96 100644 --- a/tests/by-util/test_ptx.rs +++ b/tests/by-util/test_ptx.rs @@ -9,6 +9,8 @@ use uutests::new_ucmd; #[test] fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails_with_code(1); + new_ucmd!().arg("-g").arg("0").fails_with_code(1); // clap provided message + new_ucmd!().arg("-w").arg("0").fails_with_code(1); // clap provided message } #[test] fn test_reference_format_for_stdin() {