diff --git a/src/params.rs b/src/params.rs index b723918..f61dfc2 100644 --- a/src/params.rs +++ b/src/params.rs @@ -147,8 +147,6 @@ pub fn parse_params>( let mut format = None; let mut context = None; let mut show_c_function = false; - let tabsize_re = Regex::new(r"^--tabsize=(?\d+)$").unwrap(); - let width_re = Regex::new(r"^--width=(?P\d+)$").unwrap(); while let Some((position, param)) = opts.next() { // -L LABEL, -LLABEL, --label LABEL or --label=LABEL: the first names // the from-file in headers and messages, the second the to-file. @@ -242,7 +240,7 @@ pub fn parse_params>( push_function_line_pattern(&mut params, C_FUNCTION_PATTERN)?; continue; } - if let Some(argument) = option_argument(¶m, "-F", "--show-function-line") { + if let Some(argument) = option_argument(¶m, Some("-F"), "--show-function-line") { let pattern = argument_value(argument, ¶m, &mut opts)?; push_function_line_pattern(&mut params, &pattern.to_string_lossy())?; continue; @@ -259,7 +257,7 @@ pub fn parse_params>( params.ignore_file_name_case = false; continue; } - if let Some(argument) = option_argument(¶m, "-S", "--starting-file") { + if let Some(argument) = option_argument(¶m, Some("-S"), "--starting-file") { params.starting_file = Some(argument_value(argument, ¶m, &mut opts)?); continue; } @@ -296,37 +294,21 @@ pub fn parse_params>( format = Some(Format::SideBySide); continue; } - if width_re.is_match(param.to_string_lossy().as_ref()) { - let param = param.into_string().unwrap(); - let width_str: &str = width_re - .captures(param.as_str()) - .unwrap() - .name("long") - .unwrap() - .as_str(); + if let Some(argument) = option_argument(¶m, Some("-W"), "--width") { + let width = argument_value(argument, ¶m, &mut opts)?; + let width_str = width.to_string_lossy(); params.width = match width_str.parse::() { - Ok(num) => { - if num == 0 { - return Err("invalid width «0»".to_string()); - } - - num - } - Err(_) => return Err(format!("invalid width «{width_str}»")), + Ok(num) if num != 0 => num, + _ => return Err(format!("invalid width «{width_str}»")), }; + continue; } - if tabsize_re.is_match(param.to_string_lossy().as_ref()) { - // Because param matches the regular expression, - // it is safe to assume it is valid UTF-8. - let param = param.into_string().unwrap(); - let tabsize_str = tabsize_re - .captures(param.as_str()) - .unwrap() - .name("num") - .unwrap() - .as_str(); + if let Some(argument) = option_argument(¶m, None, "--tabsize") { + let tabsize = argument_value(argument, ¶m, &mut opts)?; + let tabsize_str = tabsize.to_string_lossy(); + params.tabsize = match tabsize_str.parse::() { Ok(num) if num != 0 && num <= MAX_TABSIZE => num, _ => return Err(format!("invalid tabsize «{tabsize_str}»")), @@ -480,15 +462,23 @@ enum OptionArgument<'a> { /// Returns how the argument of the option named `short` (like `-F`) or /// `long` is given, if `param` is that option. -fn option_argument<'a>(param: &'a OsStr, short: &str, long: &str) -> Option> { - if param == short || param == long { +fn option_argument<'a>( + param: &'a OsStr, + short: Option<&str>, + long: &str, +) -> Option> { + if param == long || short.is_some_and(|short| param == short) { return Some(OptionArgument::Separate); } + let param = param.to_str()?; + param .strip_prefix(long) .and_then(|rest| rest.strip_prefix('=')) - .or_else(|| param.strip_prefix(short).filter(|value| !value.is_empty())) + .or_else(|| { + short.and_then(|short| param.strip_prefix(short).filter(|value| !value.is_empty())) + }) .map(OptionArgument::Attached) } @@ -1010,6 +1000,37 @@ mod tests { } #[test] fn tabsize() { + // Separate argument. + assert_eq!( + Ok(Params { + executable: os("diff"), + from: os("foo"), + to: os("bar"), + tabsize: 42, + ..Default::default() + }), + parse_params( + [os("diff"), os("--tabsize"), os("42"), os("foo"), os("bar")] + .iter() + .cloned() + .peekable() + ) + ); + // Missing argument. + assert!(parse_params([os("diff"), os("--tabsize")].iter().cloned().peekable()).is_err()); + assert!(parse_params( + [ + os("diff"), + os("--tabsize"), + os("invalid"), + os("foo"), + os("bar") + ] + .iter() + .cloned() + .peekable() + ) + .is_err()); assert_eq!( Ok(Params { executable: os("diff"), @@ -1140,21 +1161,60 @@ mod tests { } #[test] fn width() { - assert_eq!( - Ok(Params { - executable: os("diff"), - from: os("foo"), - to: os("bar"), - width: 100, - ..Default::default() - }), - parse_params( - [os("diff"), os("--width=100"), os("foo"), os("bar")] - .iter() - .cloned() - .peekable() - ) - ); + let parse = |args: &[&str]| { + let args = std::iter::once("diff") + .chain(args.iter().copied()) + .chain(["foo", "bar"]); + + parse_params(args.map(os).peekable()).map(|params| params.width) + }; + + // All supported forms of the width option. + for args in [ + &["-W", "100"][..], + &["-W100"][..], + &["--width=100"][..], + &["--width", "100"][..], + ] { + assert_eq!(Ok(100), parse(args), "{args:?}"); + } + + // Zero is not a valid width. + for args in [ + &["-W", "0"][..], + &["-W0"][..], + &["--width=0"][..], + &["--width", "0"][..], + ] { + assert_eq!( + Err("invalid width «0»".to_string()), + parse(args), + "{args:?}" + ); + } + + // Non-numeric values are rejected. + for args in [ + &["-W", "abc"][..], + &["-Wabc"][..], + &["--width=abc"][..], + &["--width", "abc"][..], + ] { + assert_eq!( + Err("invalid width «abc»".to_string()), + parse(args), + "{args:?}" + ); + } + + // Missing option arguments are rejected. + for option in ["-W", "--width"] { + assert_eq!( + Err(format!("option '{option}' requires an argument")), + parse_params([os("diff"), os(option)].into_iter().peekable()) + .map(|params| params.width) + ); + } } #[test] fn labels() { diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index c251c39..a20d648 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -802,6 +802,33 @@ fn sdiff_gutter_marker_column(#[case] options: &[&str], #[case] expected: &str) .stdout_is(expected); } +// Regression for #396: -W must accept a separate argument. +#[test] +fn sdiff_width_separate_argument() { + let (at, ts) = at_and_ts!(); + at.write("file1", "aa\n"); + at.write("file2", "bb\n"); + + ts.ucmd() + .args(&["-y", "-W", "20", "file1", "file2"]) + .run() + .stdout_is("aa <\n >\tbb\n"); +} + +// Regression for #396: --tabsize must accept a separate argument. +#[test] +fn tabsize_separate_argument() { + let (at, ts) = at_and_ts!(); + at.write("file1", "same\n"); + at.write("file2", "same\n"); + + ts.ucmd() + .args(&["--tabsize", "4", "file1", "file2"]) + .succeeds() + .no_stdout() + .no_stderr(); +} + #[rstest] #[case::delete_and_append( "north\neast\nsouth\nwest\n",