|
3 | 3 | // For the full copyright and license information, please view the LICENSE |
4 | 4 | // file that was distributed with this source code. |
5 | 5 | // spell-checker:ignore (ToDO) extendedbigdecimal numberparse |
| 6 | +use std::ffi::OsString; |
6 | 7 | use std::io::{stdout, ErrorKind, Write}; |
7 | 8 |
|
8 | 9 | use clap::{crate_version, Arg, ArgAction, Command}; |
@@ -47,9 +48,33 @@ struct SeqOptions<'a> { |
47 | 48 | /// The elements are (first, increment, last). |
48 | 49 | type RangeFloat = (ExtendedBigDecimal, ExtendedBigDecimal, ExtendedBigDecimal); |
49 | 50 |
|
| 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 | + |
50 | 75 | #[uucore::main] |
51 | 76 | 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))?; |
53 | 78 |
|
54 | 79 | let numbers_option = matches.get_many::<String>(ARG_NUMBERS); |
55 | 80 |
|
@@ -138,7 +163,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { |
138 | 163 | pub fn uu_app() -> Command { |
139 | 164 | Command::new(uucore::util_name()) |
140 | 165 | .trailing_var_arg(true) |
141 | | - .allow_negative_numbers(true) |
142 | 166 | .infer_long_args(true) |
143 | 167 | .version(crate_version!()) |
144 | 168 | .about(ABOUT) |
@@ -169,7 +193,10 @@ pub fn uu_app() -> Command { |
169 | 193 | .help("use printf style floating-point FORMAT"), |
170 | 194 | ) |
171 | 195 | .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) |
172 | 198 | Arg::new(ARG_NUMBERS) |
| 199 | + .allow_hyphen_values(true) |
173 | 200 | .action(ArgAction::Append) |
174 | 201 | .num_args(1..=3), |
175 | 202 | ) |
|
0 commit comments