@@ -199,7 +199,7 @@ pub struct Params {
199199 /// format GTYPE input groups with GFMT
200200 pub gtype_group_format : Option < String > ,
201201 /// keep NUM lines of the common prefix and suffix
202- pub horizon_lines : Option < String > ,
202+ pub horizon_lines : Option < usize > ,
203203 /// output merged file with '#ifdef NAME' diffs
204204 pub ifdef : Option < String > ,
205205 /// ignore all white space
@@ -504,8 +504,11 @@ impl Params {
504504
505505/// Converts clap args to Params.
506506impl TryFrom < clap:: ArgMatches > for Params {
507+ // For centralized parser errors. Requires Parser with UResult and all errors with .into().
508+ // type Error = Box<dyn UError>;
507509 type Error = ParseDiffError ;
508510
511+ // fn try_from(matches: clap::ArgMatches) -> UResult<Self> {
509512 fn try_from ( matches : clap:: ArgMatches ) -> Result < Self , Self :: Error > {
510513 // dbg!(&matches);
511514
@@ -587,8 +590,8 @@ impl TryFrom<clap::ArgMatches> for Params {
587590 }
588591
589592 // has horizon_lines?
590- if let Some ( horizon_lines) = matches. get_one :: < String > ( options:: HORIZON_LINES ) {
591- params. horizon_lines = Some ( horizon_lines. clone ( ) ) ;
593+ if let Some ( horizon_lines) = matches. get_one :: < usize > ( options:: HORIZON_LINES ) {
594+ params. horizon_lines = Some ( * horizon_lines) ;
592595 }
593596
594597 // has ifdef?
@@ -629,12 +632,11 @@ impl TryFrom<clap::ArgMatches> for Params {
629632 }
630633
631634 // has tabsize?
632- if let Some ( tabsize) = matches. get_one :: < String > ( options:: TABSIZE ) {
633- // params.tabsize = Some(tabsize.clone());
634- params. tabsize = tabsize
635- . parse :: < usize > ( )
636- // TODO error message
637- . map_err ( |_op| ParseDiffError :: MissingOperands ) ?;
635+ if let Some ( tabsize) = matches. get_one :: < u16 > ( options:: TABSIZE ) {
636+ params. tabsize = * tabsize as usize ;
637+ // params.tabsize = tabsize
638+ // .parse::<usize>()
639+ // .map_err(|_op| ParseDiffError::InvalidSomething)?;
638640 }
639641
640642 // has to_file?
@@ -651,20 +653,11 @@ impl TryFrom<clap::ArgMatches> for Params {
651653 }
652654
653655 // has width?
654- if let Some ( width) = matches. get_one :: < String > ( options:: WIDTH ) {
655- // params.width = Some(width.clone());
656- // match width.parse::<usize>() {
657- // Ok(width) => {
658- // params.width = width;
659- // // next_param_consumed = true;
660- // }
661- // // Err(_) => return Err(format!("invalid context length '{context}'")),
662- // // TODO error
663- // Err(_) => return Err(ParseCmpError::NoOperands("exe".to_string())),
664- // }
665- params. width = width
666- . parse :: < usize > ( )
667- . map_err ( |_op| ParseDiffError :: MissingOperands ) ?;
656+ if let Some ( width) = matches. get_one :: < u16 > ( options:: WIDTH ) {
657+ params. width = * width as usize
658+ // params.width = width
659+ // .parse::<usize>()
660+ // .map_err(|_op| ParseDiffError::InvalidSomething)?;
668661 }
669662
670663 if let Some ( format) = format_out {
@@ -681,11 +674,7 @@ impl TryFrom<clap::ArgMatches> for Params {
681674 // dbg!(&files);
682675
683676 match files. len ( ) {
684- 0 => return Err ( ParseDiffError :: MissingOperands ) ,
685- // If only file_1 is set, then file_2 defaults to '-', so it reads from StandardInput.
686- 1 => {
687- return Err ( ParseDiffError :: MissingOperands ) ;
688- }
677+ 0 | 1 => return Err ( ParseDiffError :: MissingOperands ) ,
689678 2 => {
690679 // diff DIRECTORY FILE => diff DIRECTORY/FILE FILE
691680 // diff FILE DIRECTORY => diff FILE DIRECTORY/FILE
@@ -900,6 +889,7 @@ impl TryFrom<clap::ArgMatches> for Params {
900889// }
901890
902891/// Contains all parser errors and their text messages.
892+ /// TODO should be centralized for all utils, messages repeat mostly.
903893///
904894/// All errors can be output easily using the normal Display functionality.
905895/// To format the error message for the typical diffutils output, use [format_error_text].
@@ -981,7 +971,7 @@ impl std::fmt::Display for ParseDiffError {
981971 translate ! ( "diff-error-conflicting-output-options" , "opt1" => opt_1, "opt2" => opt_2)
982972 }
983973 Self :: ExtraOperand ( extra_operand) => {
984- translate ! ( "base-common -extra-operand" , "operand" => extra_operand. quote( ) )
974+ translate ! ( "diff-error -extra-operand" , "operand" => extra_operand. quote( ) )
985975 }
986976 Self :: InvalidContextLength ( value) => {
987977 translate ! ( "diff-error-invalid-context-length" , "value" => value)
@@ -1005,82 +995,6 @@ impl std::fmt::Display for ParseDiffError {
1005995 }
1006996}
1007997
1008- // pub fn uu_app() -> Command {
1009- // Command::new(uucore::util_name())
1010- // .version(uucore::crate_version!())
1011- // .help_template(uucore::localized_help_template(uucore::util_name()))
1012- // .override_usage(uucore::format_usage(&translate!("diff-usage")))
1013- // .about(translate!("diff-about"))
1014- // .infer_long_args(true)
1015- // .arg(
1016- // Arg::new(options::FILE)
1017- // .action(ArgAction::Set)
1018- // .hide(true)
1019- // .value_hint(clap::ValueHint::FilePath)
1020- // .value_parser(clap::value_parser!(OsString)),
1021- // )
1022- // .arg(
1023- // Arg::new(options::BYTES_LIMIT)
1024- // .long("bytes")
1025- // .short('n')
1026- // .value_name("LIMIT")
1027- // .action(ArgAction::Set)
1028- // .help(translate!("diff-help-bytes-limit")),
1029- // )
1030- // .arg(
1031- // Arg::new(options::IGNORE_INITIAL)
1032- // .long("ignore-initial")
1033- // .short('i')
1034- // .value_name("SKIP[:SKIP2]")
1035- // .action(ArgAction::Set)
1036- // .help(translate!("diff-help-ignore-initial")),
1037- // )
1038- // .arg(
1039- // Arg::new(options::PRINT_BYTES)
1040- // .long("print-bytes")
1041- // .short('b')
1042- // .action(ArgAction::SetTrue)
1043- // .help(translate!("diff-help-print-bytes")),
1044- // )
1045- // .arg(
1046- // Arg::new(options::QUIET)
1047- // .long("quiet")
1048- // .action(ArgAction::SetTrue)
1049- // .help(translate!("diff-help-quiet")),
1050- // )
1051- // .arg(
1052- // Arg::new(options::SILENT)
1053- // .long("silent")
1054- // .short('s')
1055- // .action(ArgAction::SetTrue)
1056- // .help(translate!("diff-help-silent")),
1057- // )
1058- // .arg(
1059- // Arg::new(options::VERBOSE)
1060- // .long("verbose")
1061- // .short('l')
1062- // .action(ArgAction::SetTrue)
1063- // .help(translate!("diff-help-verbose")),
1064- // )
1065- // }
1066-
1067- // Required for build.rs
1068- // pub fn uu_app() -> Command {
1069- // Command::new(uucore::util_name())
1070- // .version(uucore::crate_version!())
1071- // .help_template(uucore::localized_help_template(uucore::util_name()))
1072- // .override_usage(uucore::format_usage(&translate!("diff-usage")))
1073- // .about(translate!("diff-about"))
1074- // .infer_long_args(true)
1075- // .arg(
1076- // Arg::new(options::FILE)
1077- // .action(ArgAction::Set)
1078- // .hide(true)
1079- // .value_hint(clap::ValueHint::FilePath)
1080- // .value_parser(clap::value_parser!(OsString)),
1081- // )
1082- // }
1083-
1084998// uu_app .args for the options
1085999pub fn uu_app ( ) -> Command {
10861000 Command :: new ( uucore:: util_name ( ) )
@@ -1177,6 +1091,7 @@ pub fn uu_app() -> Command {
11771091 Arg :: new ( options:: HORIZON_LINES )
11781092 . long ( "horizon-lines" )
11791093 . value_name ( "NUM" )
1094+ . value_parser ( clap:: value_parser!( usize ) )
11801095 . action ( ArgAction :: Set )
11811096 . help ( translate ! ( "diff-help-horizon-lines" ) ) ,
11821097 )
@@ -1219,6 +1134,7 @@ pub fn uu_app() -> Command {
12191134 Arg :: new ( options:: IGNORE_MATCHING_LINES )
12201135 . long ( "ignore-matching-lines" )
12211136 . short ( 'I' )
1137+ // TODO REGEX?
12221138 . value_name ( "RE" )
12231139 . action ( ArgAction :: Set )
12241140 . help ( translate ! ( "diff-help-ignore-matching-lines" ) ) ,
@@ -1402,6 +1318,7 @@ pub fn uu_app() -> Command {
14021318 Arg :: new ( options:: TABSIZE )
14031319 . long ( "tabsize" )
14041320 . value_name ( "NUM" )
1321+ . value_parser ( clap:: value_parser!( u16 ) )
14051322 . action ( ArgAction :: Set )
14061323 . help ( translate ! ( "diff-help-tabsize" ) ) ,
14071324 )
@@ -1449,7 +1366,7 @@ pub fn uu_app() -> Command {
14491366 . long ( "width" )
14501367 . short ( 'W' )
14511368 . value_name ( "NUM" )
1452- // .allow_negative_numbers(yes )
1369+ . value_parser ( clap :: value_parser! ( u16 ) )
14531370 . action ( ArgAction :: Set )
14541371 . help ( translate ! ( "diff-help-width" ) ) ,
14551372 )
0 commit comments