diff --git a/src/uu/od/locales/en-US.ftl b/src/uu/od/locales/en-US.ftl index e9edda4395b..86ccc869991 100644 --- a/src/uu/od/locales/en-US.ftl +++ b/src/uu/od/locales/en-US.ftl @@ -45,6 +45,7 @@ od-error-invalid-endian = Invalid argument --endian={$endian} od-error-invalid-inputs = Invalid inputs: {$msg} od-error-too-large = value is too large od-error-radix-invalid = invalid output address radix '{$radix}'; it must be one character from [doxn] +od-error-no-type-with-strings = no type may be specified when dumping strings od-error-radix-empty = Radix cannot be empty, and must be one of [o, d, x, n] od-error-invalid-width = invalid width {$width}; using {$min} instead od-error-missing-format-spec = missing format specification after '--format' / '-t' diff --git a/src/uu/od/locales/fr-FR.ftl b/src/uu/od/locales/fr-FR.ftl index d4a775c95ea..e84813578a7 100644 --- a/src/uu/od/locales/fr-FR.ftl +++ b/src/uu/od/locales/fr-FR.ftl @@ -46,6 +46,7 @@ od-error-invalid-endian = Argument invalide --endian={$endian} od-error-invalid-inputs = Entrées invalides : {$msg} od-error-too-large = la valeur est trop grande od-error-radix-invalid = base d'adresse de sortie invalide '{$radix}' ; elle doit être un caractère parmi [doxn] +od-error-no-type-with-strings = aucun type ne peut être spécifié lors de l'affichage des chaînes od-error-radix-empty = Radix ne peut pas être vide, et doit être l'un de [o, d, x, n] od-error-invalid-width = largeur invalide {$width} ; utilisation de {$min} à la place od-error-missing-format-spec = spécification de format manquante après '--format' / '-t' diff --git a/src/uu/od/src/od.rs b/src/uu/od/src/od.rs index bdd69fbe18e..769ec0c345b 100644 --- a/src/uu/od/src/od.rs +++ b/src/uu/od/src/od.rs @@ -37,6 +37,7 @@ use crate::parse_nrofbytes::{parse_number_of_bytes, parse_width}; use crate::partial_reader::PartialReader; use crate::peek_reader::{PeekRead, PeekReader}; use crate::prn_char::format_ascii_dump; +use crate::prn_int::FORMAT_ITEM_OCT16; use clap::ArgAction; use clap::{Arg, ArgMatches, Command, parser::ValueSource}; use std::ffi::OsString; @@ -138,7 +139,11 @@ impl OdOptions { } }; - let formats = parse_format_flags(args).map_err(|e| USimpleError::new(1, e))?; + let mut formats = parse_format_flags(args).map_err(|e| USimpleError::new(1, e))?; + let has_type_option = !formats.is_empty(); + if formats.is_empty() { + formats.push(ParsedFormatterItemInfo::new(FORMAT_ITEM_OCT16, false)); // 2 byte octal is the default + } let mut line_bytes = if let (Some(s), Some(ValueSource::CommandLine)) = ( matches.get_one::(options::WIDTH), @@ -195,6 +200,13 @@ impl OdOptions { let read_bytes = parse_bytes_option(matches, args, diag_args, options::READ_BYTES, 'N')?; let strings = parse_bytes_option(matches, args, diag_args, options::STRINGS, 'S')?; + if strings.is_some() && has_type_option { + return Err(USimpleError::new( + 1, + translate!("od-error-no-type-with-strings"), + )); + } + let string_min_length = match strings { None => None, Some(n) => Some(usize::try_from(n).map_err(|_| { diff --git a/src/uu/od/src/parse_formats.rs b/src/uu/od/src/parse_formats.rs index fb88e4df73c..ef85e96ab33 100644 --- a/src/uu/od/src/parse_formats.rs +++ b/src/uu/od/src/parse_formats.rs @@ -157,10 +157,6 @@ pub fn parse_format_flags(args: &[String]) -> Result Result, Strin #[cfg(test)] pub fn parse_format_flags_str(args_str: &[&'static str]) -> Result, String> { let args: Vec = args_str.iter().map(|s| (*s).to_string()).collect(); - parse_format_flags(&args).map(|v| { + let mut formats = parse_format_flags(&args)?; + if formats.is_empty() { + formats.push(ParsedFormatterItemInfo::new(FORMAT_ITEM_OCT16, false)); // 2 byte octal is the default + } + Ok(formats + .into_iter() // tests using this function assume add_ascii_dump is not set - v.into_iter() - .inspect(|f| assert!(!f.add_ascii_dump)) - .map(|f| f.formatter_item_info) - .collect() - }) + .inspect(|f| assert!(!f.add_ascii_dump)) + .map(|f| f.formatter_item_info) + .collect()) } #[test] diff --git a/tests/by-util/test_od.rs b/tests/by-util/test_od.rs index a27c855378c..7d52940f10d 100644 --- a/tests/by-util/test_od.rs +++ b/tests/by-util/test_od.rs @@ -1792,3 +1792,19 @@ fn test_od_zero_byte_type_size() { .succeeds() .stdout_is("0000000 64636261\n0000004\n"); } + +#[test] +fn test_strings_with_type_option_is_error() { + for args in [ + &["--strings=1", "-t", "x1", "a.txt"][..], + &["--strings=1", "--format=x1", "a.txt"][..], + &["-S1", "-x", "a.txt"][..], + ] { + let (at, mut ucmd) = at_and_ucmd!(); + at.write("a.txt", "a\n"); + ucmd.args(args) + .fails() + .code_is(1) + .stderr_contains("no type may be specified when dumping strings"); + } +}