mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
all: remove accept_{any, complete, lossy}
and ConversionResult
Outside of tests, only `accept_any` was used, meaning that this unnecessarily complicated the code. The behaviour of `accept_any` is now the default (and only) option.
This commit is contained in:
parent
0ed8b97a3f
commit
5621502a95
48 changed files with 74 additions and 216 deletions
|
@ -87,9 +87,7 @@ impl Config {
|
|||
|
||||
pub fn parse_base_cmd_args(args: impl uucore::Args, about: &str, usage: &str) -> UResult<Config> {
|
||||
let command = base_app(about, usage);
|
||||
let arg_list = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let arg_list = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
Config::from(&command.try_get_matches_from(arg_list)?)
|
||||
}
|
||||
|
||||
|
|
|
@ -28,9 +28,7 @@ pub mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
// Since options have to go before names,
|
||||
// if the first argument is not an option, then there is no option,
|
||||
|
|
|
@ -52,10 +52,7 @@ pub fn uu_app<'a>() -> Command<'a> {
|
|||
|
||||
fn parse_cmd_args(args: impl uucore::Args) -> UResult<(Config, Format)> {
|
||||
let matches = uu_app()
|
||||
.try_get_matches_from(
|
||||
args.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any(),
|
||||
)
|
||||
.try_get_matches_from(args.collect_str(InvalidEncodingHandling::ConvertLossy))
|
||||
.with_exit_code(1)?;
|
||||
let format = ENCODINGS
|
||||
.iter()
|
||||
|
|
|
@ -184,9 +184,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let matches = uu_app().try_get_matches_from(args)?;
|
||||
|
||||
|
|
|
@ -46,9 +46,7 @@ fn get_long_usage() -> String {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let mut args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let mut args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
// Before we can parse 'args' with clap (and previously getopts),
|
||||
// a possible MODE prefix '-' needs to be removed (e.g. "chmod -x FILE").
|
||||
|
|
|
@ -33,9 +33,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -114,9 +114,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -132,9 +132,7 @@ fn open_file(name: &str) -> io::Result<LineReader> {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
let filename1 = matches.value_of(options::FILE_1).unwrap();
|
||||
|
|
|
@ -713,9 +713,7 @@ mod tests {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -398,9 +398,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let delimiter_is_equal = args.contains(&"-d=".to_string()); // special case
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
|
|
@ -707,7 +707,6 @@ fn append_dashes_if_not_present(mut acc: Vec<String>, mut s: String) -> Vec<Stri
|
|||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let dashed_args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any()
|
||||
.into_iter()
|
||||
.fold(Vec::new(), append_dashes_if_not_present);
|
||||
|
||||
|
|
|
@ -65,9 +65,7 @@ pub fn guess_syntax() -> OutputFmt {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let matches = uu_app().get_matches_from(&args);
|
||||
|
||||
|
|
|
@ -28,9 +28,7 @@ fn get_long_usage() -> String {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let after_help = get_long_usage();
|
||||
|
||||
|
|
|
@ -516,9 +516,7 @@ fn build_exclude_patterns(matches: &ArgMatches) -> UResult<Vec<Pattern>> {
|
|||
#[uucore::main]
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -110,9 +110,7 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result<bool> {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
let no_newline = matches.contains_id(options::NO_NEWLINE);
|
||||
|
|
|
@ -269,9 +269,7 @@ fn expand_shortcuts(args: &[String]) -> Vec<String> {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let matches = uu_app().get_matches_from(expand_shortcuts(&args));
|
||||
|
||||
|
|
|
@ -33,9 +33,7 @@ pub fn uu_app<'a>() -> Command<'a> {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
// For expr utility we do not want getopts.
|
||||
// The following usage should work without escaping hyphens: `expr -15 = 1 + 2 \* \( 3 - -4 \)`
|
||||
|
|
|
@ -31,9 +31,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let (args, obs_width) = handle_obsolete(&args[..]);
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
|
|
@ -38,9 +38,7 @@ pub enum Mode {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let mut args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let mut args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
let obs_signal = handle_obsolete(&mut args);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
|
|
@ -37,9 +37,7 @@ static ABOUT: &str = "Print user's login name";
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let _ = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -83,9 +83,7 @@ fn strip_minus_from_mode(args: &mut Vec<String>) -> bool {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let mut args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let mut args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
// Before we can parse 'args' with clap (and previously getopts),
|
||||
// a possible MODE prefix '-' needs to be removed (e.g. "chmod -x FILE").
|
||||
|
|
|
@ -28,9 +28,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -81,9 +81,7 @@ fn _mknod(file_name: &str, mode: mode_t, dev: dev_t) -> i32 {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
// Linux-specific options, not implemented
|
||||
// opts.optflag("Z", "", "set the SELinux security context to default type");
|
||||
// opts.optopt("", "context", "like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX");
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
use clap::{crate_version, Arg, ArgMatches, Command};
|
||||
use uucore::display::{println_verbatim, Quotable};
|
||||
use uucore::error::{FromIo, UError, UResult};
|
||||
use uucore::format_usage;
|
||||
use uucore::{format_usage, InvalidEncodingHandling};
|
||||
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
|
@ -316,7 +316,7 @@ impl Params {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args.collect_str_lossy().accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().try_get_matches_from(&args)?;
|
||||
|
||||
|
|
|
@ -84,9 +84,7 @@ pub mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -86,9 +86,7 @@ impl Display for NohupError {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -261,9 +261,7 @@ fn concat_format_arg_and_value(args: &[String]) -> Vec<String> {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let matches = uu_app().get_matches_from(concat_format_arg_and_value(&args));
|
||||
|
||||
|
|
|
@ -256,9 +256,7 @@ impl OdOptions {
|
|||
/// opens the input and calls `odfunc` to process the input.
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let clap_opts = uu_app();
|
||||
|
||||
|
|
|
@ -39,9 +39,7 @@ const POSIX_NAME_MAX: usize = 14;
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -49,9 +49,7 @@ fn get_long_usage() -> String {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let after_help = get_long_usage();
|
||||
|
||||
|
|
|
@ -378,9 +378,7 @@ pub fn uu_app<'a>() -> Command<'a> {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(uucore::InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(uucore::InvalidEncodingHandling::Ignore);
|
||||
|
||||
let opt_args = recreate_arguments(&args);
|
||||
|
||||
|
|
|
@ -271,9 +271,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
let format_string = matches
|
||||
|
|
|
@ -722,9 +722,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
// let mut opts = Options::new();
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
|
|
@ -27,9 +27,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -266,9 +266,7 @@ pub mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -56,9 +56,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -1055,9 +1055,7 @@ fn make_sort_mode_arg<'a>(mode: &'a str, short: char, help: &'a str) -> Arg<'a>
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
let mut settings: GlobalSettings = Default::default();
|
||||
|
||||
let matches = match uu_app().try_get_matches_from(args) {
|
||||
|
|
|
@ -156,9 +156,7 @@ fn get_preload_env(tmp_dir: &mut TempDir) -> io::Result<(String, PathBuf)> {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -109,9 +109,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -37,9 +37,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -106,9 +106,7 @@ impl Config {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let command = uu_app();
|
||||
|
||||
|
|
|
@ -40,9 +40,7 @@ fn get_long_usage() -> String {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let after_help = get_long_usage();
|
||||
|
||||
|
|
|
@ -25,9 +25,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -24,9 +24,7 @@ mod options {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::ConvertLossy);
|
||||
|
||||
let matches = uu_app().get_matches_from(args);
|
||||
|
||||
|
|
|
@ -165,9 +165,7 @@ fn expand_shortcuts(args: &[String]) -> Vec<String> {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let matches = uu_app().get_matches_from(expand_shortcuts(&args));
|
||||
|
||||
|
|
|
@ -56,9 +56,7 @@ fn get_long_usage() -> String {
|
|||
|
||||
#[uucore::main]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let args = args
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
let args = args.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
let after_help = get_long_usage();
|
||||
|
||||
|
|
|
@ -155,38 +155,6 @@ pub enum InvalidEncodingHandling {
|
|||
ConvertLossy,
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub enum ConversionResult {
|
||||
Complete(Vec<String>),
|
||||
Lossy(Vec<String>),
|
||||
}
|
||||
|
||||
impl ConversionResult {
|
||||
pub fn accept_any(self) -> Vec<String> {
|
||||
match self {
|
||||
Self::Complete(result) | Self::Lossy(result) => result,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_lossy(self, msg: &str) -> Vec<String> {
|
||||
match self {
|
||||
Self::Lossy(result) => result,
|
||||
Self::Complete(_) => {
|
||||
panic!("{}", msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expect_complete(self, msg: &str) -> Vec<String> {
|
||||
match self {
|
||||
Self::Complete(result) => result,
|
||||
Self::Lossy(_) => {
|
||||
panic!("{}", msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Args: Iterator<Item = OsString> + Sized {
|
||||
/// Converts each iterator item to a String and collects these into a vector
|
||||
/// On invalid encoding, the result will depend on the argument. This method allows to either drop entries with illegal encoding
|
||||
|
@ -194,45 +162,31 @@ pub trait Args: Iterator<Item = OsString> + Sized {
|
|||
/// which will result in strange strings or can chosen to panic.
|
||||
/// # Arguments
|
||||
/// * `handling` - This switch allows to switch the behavior, when invalid encoding is encountered
|
||||
fn collect_str(self, handling: InvalidEncodingHandling) -> ConversionResult {
|
||||
let mut full_conversion = true;
|
||||
let result_vector: Vec<String> = self
|
||||
.map(|s| match s.into_string() {
|
||||
Ok(string) => Ok(string),
|
||||
Err(s_ret) => {
|
||||
full_conversion = false;
|
||||
eprintln!(
|
||||
"Input with broken encoding occurred! (s = {}) ",
|
||||
s_ret.quote()
|
||||
);
|
||||
match handling {
|
||||
InvalidEncodingHandling::Ignore => Err(String::new()),
|
||||
InvalidEncodingHandling::ConvertLossy => {
|
||||
Err(s_ret.to_string_lossy().into_owned())
|
||||
}
|
||||
fn collect_str(self, handling: InvalidEncodingHandling) -> Vec<String> {
|
||||
self.map(|s| match s.into_string() {
|
||||
Ok(string) => Ok(string),
|
||||
Err(s_ret) => {
|
||||
eprintln!(
|
||||
"Input with broken encoding occurred! (s = {}) ",
|
||||
s_ret.quote()
|
||||
);
|
||||
match handling {
|
||||
InvalidEncodingHandling::Ignore => Err(String::new()),
|
||||
InvalidEncodingHandling::ConvertLossy => {
|
||||
Err(s_ret.to_string_lossy().into_owned())
|
||||
}
|
||||
}
|
||||
})
|
||||
.filter(|s| match handling {
|
||||
InvalidEncodingHandling::Ignore => s.is_ok(),
|
||||
_ => true,
|
||||
})
|
||||
.map(|s| match s {
|
||||
Ok(v) => v,
|
||||
Err(e) => e,
|
||||
})
|
||||
.collect();
|
||||
|
||||
if full_conversion {
|
||||
ConversionResult::Complete(result_vector)
|
||||
} else {
|
||||
ConversionResult::Lossy(result_vector)
|
||||
}
|
||||
}
|
||||
|
||||
/// convenience function for a more slim interface
|
||||
fn collect_str_lossy(self) -> ConversionResult {
|
||||
self.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
}
|
||||
})
|
||||
.filter(|s| match handling {
|
||||
InvalidEncodingHandling::Ignore => s.is_ok(),
|
||||
_ => true,
|
||||
})
|
||||
.map(|s| match s {
|
||||
Ok(v) => v,
|
||||
Err(e) => e,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -255,7 +209,7 @@ mod tests {
|
|||
]
|
||||
}
|
||||
|
||||
fn collect_os_str(vec: Vec<OsString>, handling: InvalidEncodingHandling) -> ConversionResult {
|
||||
fn collect_os_str(vec: Vec<OsString>, handling: InvalidEncodingHandling) -> Vec<String> {
|
||||
vec.into_iter().collect_str(handling)
|
||||
}
|
||||
|
||||
|
@ -265,8 +219,7 @@ mod tests {
|
|||
assert!(os_str.to_os_string().into_string().is_err());
|
||||
let test_vec = make_os_vec(os_str);
|
||||
let collected_to_str =
|
||||
collect_os_str(test_vec.clone(), InvalidEncodingHandling::ConvertLossy)
|
||||
.expect_lossy("Lossy conversion expected in this test: bad encoding entries should be converted as good as possible");
|
||||
collect_os_str(test_vec.clone(), InvalidEncodingHandling::ConvertLossy);
|
||||
//conservation of length - when accepting lossy conversion no arguments may be dropped
|
||||
assert_eq!(collected_to_str.len(), test_vec.len());
|
||||
//first indices identical
|
||||
|
@ -288,10 +241,7 @@ mod tests {
|
|||
//assert our string is invalid utf8
|
||||
assert!(os_str.to_os_string().into_string().is_err());
|
||||
let test_vec = make_os_vec(os_str);
|
||||
let collected_to_str = collect_os_str(test_vec.clone(), InvalidEncodingHandling::Ignore)
|
||||
.expect_lossy(
|
||||
"Lossy conversion expected in this test: bad encoding entries should be filtered",
|
||||
);
|
||||
let collected_to_str = collect_os_str(test_vec.clone(), InvalidEncodingHandling::Ignore);
|
||||
//assert that the broken entry is filtered out
|
||||
assert_eq!(collected_to_str.len(), test_vec.len() - 1);
|
||||
//assert that the unbroken indices are converted as expected
|
||||
|
@ -308,8 +258,7 @@ mod tests {
|
|||
//create a vector containing only correct encoding
|
||||
let test_vec = make_os_vec(&OsString::from("test2"));
|
||||
//expect complete conversion without losses, even when lossy conversion is accepted
|
||||
let _ = collect_os_str(test_vec, InvalidEncodingHandling::ConvertLossy)
|
||||
.expect_complete("Lossy conversion not expected in this test");
|
||||
let _ = collect_os_str(test_vec, InvalidEncodingHandling::ConvertLossy);
|
||||
}
|
||||
|
||||
#[cfg(any(unix, target_os = "redox"))]
|
||||
|
|
|
@ -1021,8 +1021,7 @@ impl UCommand {
|
|||
let strings = args
|
||||
.iter()
|
||||
.map(|s| s.as_ref().to_os_string())
|
||||
.collect_str(InvalidEncodingHandling::Ignore)
|
||||
.accept_any();
|
||||
.collect_str(InvalidEncodingHandling::Ignore);
|
||||
|
||||
for s in strings {
|
||||
self.comm_string.push(' ');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue