Skip to content

Commit ee0426e

Browse files
committed
seq: use allow_hyphen_values instead of
allow_negative_numbers because clap removed support for "exotic" negative numbers like -.1
1 parent 934cbb3 commit ee0426e

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/uu/seq/src/seq.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55
// spell-checker:ignore (ToDO) extendedbigdecimal numberparse
6+
use std::ffi::OsString;
67
use std::io::{stdout, ErrorKind, Write};
78

89
use clap::{crate_version, Arg, ArgAction, Command};
@@ -47,9 +48,33 @@ struct SeqOptions<'a> {
4748
/// The elements are (first, increment, last).
4849
type RangeFloat = (ExtendedBigDecimal, ExtendedBigDecimal, ExtendedBigDecimal);
4950

51+
// Turn short args with attached value, for example "-s,", into two args "-s" and "," to make
52+
// them work with clap.
53+
fn split_short_args_with_value(args: impl uucore::Args) -> impl uucore::Args {
54+
let mut v: Vec<OsString> = Vec::new();
55+
56+
for arg in args {
57+
let bytes = arg.as_encoded_bytes();
58+
59+
if bytes.len() > 2
60+
&& (bytes.starts_with(b"-f") || bytes.starts_with(b"-s") || bytes.starts_with(b"-t"))
61+
{
62+
let (short_arg, value) = bytes.split_at(2);
63+
// SAFETY:
64+
// Both `short_arg` and `value` only contain content that originated from `OsStr::as_encoded_bytes`
65+
v.push(unsafe { OsString::from_encoded_bytes_unchecked(short_arg.to_vec()) });
66+
v.push(unsafe { OsString::from_encoded_bytes_unchecked(value.to_vec()) });
67+
} else {
68+
v.push(arg);
69+
}
70+
}
71+
72+
v.into_iter()
73+
}
74+
5075
#[uucore::main]
5176
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
52-
let matches = uu_app().try_get_matches_from(args)?;
77+
let matches = uu_app().try_get_matches_from(split_short_args_with_value(args))?;
5378

5479
let numbers_option = matches.get_many::<String>(ARG_NUMBERS);
5580

@@ -138,7 +163,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
138163
pub fn uu_app() -> Command {
139164
Command::new(uucore::util_name())
140165
.trailing_var_arg(true)
141-
.allow_negative_numbers(true)
142166
.infer_long_args(true)
143167
.version(crate_version!())
144168
.about(ABOUT)
@@ -169,7 +193,10 @@ pub fn uu_app() -> Command {
169193
.help("use printf style floating-point FORMAT"),
170194
)
171195
.arg(
196+
// we use allow_hyphen_values instead of allow_negative_numbers because clap removed
197+
// the support for "exotic" negative numbers like -.1 (see https://github.com/clap-rs/clap/discussions/5837)
172198
Arg::new(ARG_NUMBERS)
199+
.allow_hyphen_values(true)
173200
.action(ArgAction::Append)
174201
.num_args(1..=3),
175202
)

tests/by-util/test_seq.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ fn test_hex_rejects_sign_after_identifier() {
4848
.args(&["-0x-123ABC"])
4949
.fails()
5050
.no_stdout()
51-
.stderr_contains("unexpected argument '-0' found");
51+
.usage_error("invalid floating point argument: '-0x-123ABC'");
5252
new_ucmd!()
5353
.args(&["-0x+123ABC"])
5454
.fails()
5555
.no_stdout()
56-
.stderr_contains("unexpected argument '-0' found");
56+
.usage_error("invalid floating point argument: '-0x+123ABC'");
5757
}
5858

5959
#[test]

0 commit comments

Comments
 (0)