Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/uu/ptx/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ ptx-error-write-failed = write failed
ptx-error-extra-operand = extra operand { $operand }
ptx-error-empty-regexp = A regular expression cannot match a length zero string
ptx-error-invalid-regexp = Invalid regexp: { $error }
ptx-error-invalid-number = invalid { $kind }: '{ $value }'
1 change: 1 addition & 0 deletions src/uu/ptx/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ ptx-error-write-failed = échec de l'écriture
ptx-error-extra-operand = opérande supplémentaire { $operand }
ptx-error-empty-regexp = Une expression régulière ne peut pas correspondre à une chaîne de longueur zéro
ptx-error-invalid-regexp = Expression régulière invalide : { $error }
ptx-error-invalid-number = { $kind } invalide : '{ $value }'
27 changes: 20 additions & 7 deletions src/uu/ptx/src/ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use std::io::{BufRead, BufReader, BufWriter, Read, Write, stdin, stdout};
use std::path::Path;

use clap::{Arg, ArgAction, Command, value_parser};
use clap::{Arg, ArgAction, Command};
use regex::Regex;
use rustc_hash::FxHashSet;
use uucore::display::Quotable;
Expand Down Expand Up @@ -200,6 +200,19 @@
char_start: usize,
}

/// Parses `--gap-size`/`--width`'s value the way GNU does: a plain positive
/// integer, with any other string -- unparseable, zero, or overflowing --

Check warning on line 204 in src/uu/ptx/src/ptx.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

WARNING: `cspell`: Unknown word 'unparseable' (file:'src/uu/ptx/src/ptx.rs', line:204)
/// reported the same way as any other.
fn parse_ptx_number(value: &str, kind: &'static str) -> UResult<u64> {
match value.parse::<u64>() {
Ok(n) if n > 0 => Ok(n),
_ => Err(USimpleError::new(
1,
translate!("ptx-error-invalid-number", "kind" => kind, "value" => value.to_owned()),
)),
}
}

fn get_config(matches: &mut clap::ArgMatches) -> UResult<Config> {
let mut config = Config::default();
let err_msg = "parsing options failed";
Expand Down Expand Up @@ -245,13 +258,13 @@
.expect(err_msg)
.clone_into(&mut config.trunc_str);
}
if matches.contains_id(options::WIDTH) {
config.line_width = *matches.get_one::<u64>(options::WIDTH).unwrap() as usize;
if let Some(value) = matches.get_one::<String>(options::WIDTH) {
config.line_width = parse_ptx_number(value, "line width")? 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::<u64>(options::GAP_SIZE).unwrap() as usize;
if let Some(value) = matches.get_one::<String>(options::GAP_SIZE) {
config.gap_size = parse_ptx_number(value, "gap width")? as usize;
}
if let Some(format) = matches.get_one::<String>(options::FORMAT) {
config.format = match format.as_str() {
Expand Down Expand Up @@ -376,7 +389,7 @@
word = word.to_uppercase();
}

// Count from the previous match to avoid rescanning the line prefix.

Check warning on line 392 in src/uu/ptx/src/ptx.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

WARNING: `cspell`: Unknown word 'rescanning' (file:'src/uu/ptx/src/ptx.rs', line:392)
char_start += line[last_counted_byte..beg].chars().count();
last_counted_byte = beg;
word_set.insert(WordRef {
Expand Down Expand Up @@ -1041,7 +1054,7 @@
Arg::new(options::GAP_SIZE)
.short('g')
.long(options::GAP_SIZE)
.value_parser(value_parser!(u64).range(1..))
.allow_hyphen_values(true)
.help(translate!("ptx-help-gap-size"))
.value_name("NUMBER"),
)
Expand Down Expand Up @@ -1082,7 +1095,7 @@
Arg::new(options::WIDTH)
.short('w')
.long(options::WIDTH)
.value_parser(value_parser!(u64).range(1..))
.allow_hyphen_values(true)
.help(translate!("ptx-help-width"))
.value_name("NUMBER"),
)
Expand Down
27 changes: 25 additions & 2 deletions tests/by-util/test_ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,31 @@ 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
new_ucmd!()
.arg("-g")
.arg("0")
.fails_with_code(1)
.stderr_is("ptx: invalid gap width: '0'\n");
new_ucmd!()
.arg("-w")
.arg("0")
.fails_with_code(1)
.stderr_is("ptx: invalid line width: '0'\n");
}

#[test]
fn test_gap_size_negative_as_separate_arg() {
// A negative value passed as its own argument (not attached with
// `-g-5`/`=`) must not be mistaken for a new, unrecognized flag; GNU
// still rejects the negative value itself, just with its own wording.
new_ucmd!()
.args(&["-g", "-5"])
.fails_with_code(1)
.stderr_is("ptx: invalid gap width: '-5'\n");
new_ucmd!()
.args(&["--width", "-5"])
.fails_with_code(1)
.stderr_is("ptx: invalid line width: '-5'\n");
}
#[test]
fn test_reference_format_for_stdin() {
Expand Down
Loading