mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-01 05:27:45 +00:00
Merge pull request #2811 from jfinkels/od-uresult
od: return UResult from uumain() function
This commit is contained in:
commit
80fab36639
1 changed files with 68 additions and 34 deletions
|
@ -44,6 +44,7 @@ use crate::peekreader::*;
|
||||||
use crate::prn_char::format_ascii_dump;
|
use crate::prn_char::format_ascii_dump;
|
||||||
use clap::{self, crate_version, AppSettings, Arg, ArgMatches};
|
use clap::{self, crate_version, AppSettings, Arg, ArgMatches};
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
|
use uucore::error::{UResult, USimpleError};
|
||||||
use uucore::parse_size::ParseSizeError;
|
use uucore::parse_size::ParseSizeError;
|
||||||
use uucore::InvalidEncodingHandling;
|
use uucore::InvalidEncodingHandling;
|
||||||
|
|
||||||
|
@ -120,25 +121,36 @@ struct OdOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OdOptions {
|
impl OdOptions {
|
||||||
fn new(matches: ArgMatches, args: Vec<String>) -> Result<OdOptions, String> {
|
fn new(matches: ArgMatches, args: Vec<String>) -> UResult<OdOptions> {
|
||||||
let byte_order = match matches.value_of(options::ENDIAN) {
|
let byte_order = match matches.value_of(options::ENDIAN) {
|
||||||
None => ByteOrder::Native,
|
None => ByteOrder::Native,
|
||||||
Some("little") => ByteOrder::Little,
|
Some("little") => ByteOrder::Little,
|
||||||
Some("big") => ByteOrder::Big,
|
Some("big") => ByteOrder::Big,
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
return Err(format!("Invalid argument --endian={}", s));
|
return Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
format!("Invalid argument --endian={}", s),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut skip_bytes = matches.value_of(options::SKIP_BYTES).map_or(0, |s| {
|
let mut skip_bytes = match matches.value_of(options::SKIP_BYTES) {
|
||||||
parse_number_of_bytes(s).unwrap_or_else(|e| {
|
None => 0,
|
||||||
crash!(1, "{}", format_error_message(e, s, options::SKIP_BYTES))
|
Some(s) => match parse_number_of_bytes(s) {
|
||||||
})
|
Ok(n) => n,
|
||||||
});
|
Err(e) => {
|
||||||
|
return Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
format_error_message(e, s, options::SKIP_BYTES),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
let mut label: Option<usize> = None;
|
let mut label: Option<usize> = None;
|
||||||
|
|
||||||
let parsed_input = parse_inputs(&matches).map_err(|e| format!("Invalid inputs: {}", e))?;
|
let parsed_input = parse_inputs(&matches)
|
||||||
|
.map_err(|e| USimpleError::new(1, format!("Invalid inputs: {}", e)))?;
|
||||||
let input_strings = match parsed_input {
|
let input_strings = match parsed_input {
|
||||||
CommandLineInputs::FileNames(v) => v,
|
CommandLineInputs::FileNames(v) => v,
|
||||||
CommandLineInputs::FileAndOffset((f, s, l)) => {
|
CommandLineInputs::FileAndOffset((f, s, l)) => {
|
||||||
|
@ -148,15 +160,26 @@ impl OdOptions {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let formats = parse_format_flags(&args)?;
|
let formats = parse_format_flags(&args).map_err(|e| USimpleError::new(1, e))?;
|
||||||
|
|
||||||
let mut line_bytes = matches.value_of(options::WIDTH).map_or(16, |s| {
|
let mut line_bytes = match matches.value_of(options::WIDTH) {
|
||||||
|
None => 16,
|
||||||
|
Some(s) => {
|
||||||
if matches.occurrences_of(options::WIDTH) == 0 {
|
if matches.occurrences_of(options::WIDTH) == 0 {
|
||||||
return 16;
|
16
|
||||||
|
} else {
|
||||||
|
match parse_number_of_bytes(s) {
|
||||||
|
Ok(n) => n,
|
||||||
|
Err(e) => {
|
||||||
|
return Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
format_error_message(e, s, options::WIDTH),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
parse_number_of_bytes(s)
|
|
||||||
.unwrap_or_else(|e| crash!(1, "{}", format_error_message(e, s, options::WIDTH)))
|
|
||||||
});
|
|
||||||
|
|
||||||
let min_bytes = formats.iter().fold(1, |max, next| {
|
let min_bytes = formats.iter().fold(1, |max, next| {
|
||||||
cmp::max(max, next.formatter_item_info.byte_size)
|
cmp::max(max, next.formatter_item_info.byte_size)
|
||||||
|
@ -168,18 +191,28 @@ impl OdOptions {
|
||||||
|
|
||||||
let output_duplicates = matches.is_present(options::OUTPUT_DUPLICATES);
|
let output_duplicates = matches.is_present(options::OUTPUT_DUPLICATES);
|
||||||
|
|
||||||
let read_bytes = matches.value_of(options::READ_BYTES).map(|s| {
|
let read_bytes = match matches.value_of(options::READ_BYTES) {
|
||||||
parse_number_of_bytes(s).unwrap_or_else(|e| {
|
None => None,
|
||||||
crash!(1, "{}", format_error_message(e, s, options::READ_BYTES))
|
Some(s) => match parse_number_of_bytes(s) {
|
||||||
})
|
Ok(n) => Some(n),
|
||||||
});
|
Err(e) => {
|
||||||
|
return Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
format_error_message(e, s, options::READ_BYTES),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
let radix = match matches.value_of(options::ADDRESS_RADIX) {
|
let radix = match matches.value_of(options::ADDRESS_RADIX) {
|
||||||
None => Radix::Octal,
|
None => Radix::Octal,
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
let st = s.as_bytes();
|
let st = s.as_bytes();
|
||||||
if st.len() != 1 {
|
if st.len() != 1 {
|
||||||
return Err("Radix must be one of [d, o, n, x]".to_string());
|
return Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
"Radix must be one of [d, o, n, x]".to_string(),
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
let radix: char =
|
let radix: char =
|
||||||
*(st.get(0).expect("byte string of length 1 lacks a 0th elem")) as char;
|
*(st.get(0).expect("byte string of length 1 lacks a 0th elem")) as char;
|
||||||
|
@ -188,7 +221,12 @@ impl OdOptions {
|
||||||
'x' => Radix::Hexadecimal,
|
'x' => Radix::Hexadecimal,
|
||||||
'o' => Radix::Octal,
|
'o' => Radix::Octal,
|
||||||
'n' => Radix::NoPrefix,
|
'n' => Radix::NoPrefix,
|
||||||
_ => return Err("Radix must be one of [d, o, n, x]".to_string()),
|
_ => {
|
||||||
|
return Err(USimpleError::new(
|
||||||
|
1,
|
||||||
|
"Radix must be one of [d, o, n, x]".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -210,7 +248,8 @@ impl OdOptions {
|
||||||
|
|
||||||
/// parses and validates command line parameters, prepares data structures,
|
/// parses and validates command line parameters, prepares data structures,
|
||||||
/// opens the input and calls `odfunc` to process the input.
|
/// opens the input and calls `odfunc` to process the input.
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
#[uucore_procs::gen_uumain]
|
||||||
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let args = args
|
let args = args
|
||||||
.collect_str(InvalidEncodingHandling::Ignore)
|
.collect_str(InvalidEncodingHandling::Ignore)
|
||||||
.accept_any();
|
.accept_any();
|
||||||
|
@ -221,12 +260,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
.clone() // Clone to reuse clap_opts to print help
|
.clone() // Clone to reuse clap_opts to print help
|
||||||
.get_matches_from(args.clone());
|
.get_matches_from(args.clone());
|
||||||
|
|
||||||
let od_options = match OdOptions::new(clap_matches, args) {
|
let od_options = OdOptions::new(clap_matches, args)?;
|
||||||
Err(s) => {
|
|
||||||
crash!(1, "{}", s);
|
|
||||||
}
|
|
||||||
Ok(o) => o,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut input_offset =
|
let mut input_offset =
|
||||||
InputOffset::new(od_options.radix, od_options.skip_bytes, od_options.label);
|
InputOffset::new(od_options.radix, od_options.skip_bytes, od_options.label);
|
||||||
|
@ -482,7 +516,7 @@ fn odfunc<I>(
|
||||||
input_offset: &mut InputOffset,
|
input_offset: &mut InputOffset,
|
||||||
input_decoder: &mut InputDecoder<I>,
|
input_decoder: &mut InputDecoder<I>,
|
||||||
output_info: &OutputInfo,
|
output_info: &OutputInfo,
|
||||||
) -> i32
|
) -> UResult<()>
|
||||||
where
|
where
|
||||||
I: PeekRead + HasError,
|
I: PeekRead + HasError,
|
||||||
{
|
{
|
||||||
|
@ -540,15 +574,15 @@ where
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
show_error!("{}", e);
|
show_error!("{}", e);
|
||||||
input_offset.print_final_offset();
|
input_offset.print_final_offset();
|
||||||
return 1;
|
return Err(1.into());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if input_decoder.has_error() {
|
if input_decoder.has_error() {
|
||||||
1
|
Err(1.into())
|
||||||
} else {
|
} else {
|
||||||
0
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue