Skip to content

Commit e28dcc0

Browse files
author
Gunter Schmidt
committed
Fix: error messages
1 parent 9103347 commit e28dcc0

5 files changed

Lines changed: 37 additions & 128 deletions

File tree

.vscode/launch.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@
7878
}
7979
},
8080
"args": [
81-
"-c2",
82-
// "--context=",
83-
// "src",
8481
"lorem_ipsum.txt",
85-
"lorem_ipsum_diff.txt"
82+
"lorem_ipsum_diff.txt",
83+
"--width=56jj"
8684
// "1ZiB"
8785
// "99999999999999999999999999999999999999999999999999999999999"
8886
// "-bl",

src/uu/cmp/locales/en-US.ftl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ cmp-help-silent = suppress all normal output
1313
cmp-help-verbose = output byte numbers and differing byte values
1414
1515
# Error messages
16+
# could be centralized
17+
cmp-error-incompatible-options = options --{ $opt1 } and --{ $opt2 } are incompatible
1618
cmp-error-missing-operands = missing operand after '{ $util_name }'
1719
cmp-error-invalid-value = invalid value '{ $value }' for option --{ $option }
1820
cmp-error-invalid-value-overflow = invalid value '{ $value }' (too large) for option --{ $option }
1921
cmp-error-invalid-value-unit = invalid unit in '{ $value }' for option --{ $option }
20-
cmp-error-incompatible-options = options --{ $opt1 } and --{ $opt2 } are incompatible
21-
cmp-error-is-directory = '{ $name }' is a directory
22+
cmp-error-extra-operand = extra operand {$operand}
2223
cmp-error-not-yet-implemented = the option '--{ $option}' is not yet implemented
23-
# unclear if the centralized one can be used
24-
base-common-extra-operand = extra operand {$operand}

src/uu/cmp/src/params_cmp.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ impl std::fmt::Display for ParseCmpError {
384384
},
385385

386386
Self::ExtraOperand(extra_operand) => {
387-
translate!("base-common-extra-operand", "operand" => extra_operand.quote())
387+
translate!("cmp-error-extra-operand", "operand" => extra_operand.quote())
388388
}
389389
Self::NoOperands(_exe_name) => {
390390
translate!("cmp-error-missing-operands", "util_name" => uucore::util_name())
@@ -449,10 +449,9 @@ pub fn uu_app() -> Command {
449449
Arg::new(options::SILENT)
450450
.long("silent")
451451
.short('s')
452-
// .visible_alias(options::QUIET)
452+
// .visible_alias(options::QUIET) works, but shows different --help
453453
.action(ArgAction::SetTrue)
454454
.help(translate!("cmp-help-silent")),
455-
// TODO .visible_short_flag_alias('q'))
456455
)
457456
.arg(
458457
Arg::new(options::VERBOSE)

src/uu/diff/locales/en-US.ftl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,11 @@ diff-info-files-are-identical = Files { $file_1 } and { $file_2 } are identical
5959
diff-info-files-are-different = Files { $file_1 } and { $file_2 } differ
6060
6161
# Error messages
62-
# diff-error-missing-newline = No newline at end of file
63-
# some of these are available centralized I believe
64-
# diff-error-conflicting-output-options = Conflicting output style options '--{ $opt1 }' and '--{ $opt2 }'.
65-
# diff-error-invalid-context-length = invalid context length '{ $value }'
66-
# diff-error-invalid-unified-length = invalid unified length '{ $value }'
67-
# diff-error-extra-operand = extra operand '{ $operand }'
68-
# diff-error-is-directory = '{ $name }' is a directory
69-
# diff-error-missing-operands = missing operand after '{ $util_name }'
70-
# diff-error-no-such-file-or-directory = { $name }: No such file or directory
62+
diff-error-missing-newline = No newline at end of file
63+
diff-error-conflicting-output-options = Conflicting output style options '--{ $opt1 }' and '--{ $opt2 }'.
64+
diff-error-invalid-context-length = invalid context length '{ $value }'
65+
diff-error-invalid-unified-length = invalid unified length '{ $value }'
66+
# These could be centralized. Requires Parser with UResult and all errors with .into().
67+
diff-error-extra-operand = extra operand '{ $operand }'
68+
diff-error-missing-operands = missing operand after '{ $util_name }'
7169
diff-error-not-yet-implemented = the option '--{ $option}' is not yet implemented
72-
# unclear if the centralized one can be used
73-
base-common-extra-operand = extra operand {$operand}

src/uu/diff/src/params_diff.rs

Lines changed: 23 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
506506
impl 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
1085999
pub 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

Comments
 (0)