1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 04:57:45 +00:00
This commit is contained in:
Felipe Lema 2021-03-08 12:02:46 -03:00
parent 3e0a10d732
commit 75b3bc02eb

View file

@ -16,15 +16,17 @@ use std::str::FromStr;
static ABOUT: &str = "Report or omit repeated lines."; static ABOUT: &str = "Report or omit repeated lines.";
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static OPT_ALL_REPEATED: &str = "all-repeated"; pub mod options {
static OPT_CHECK_CHARS: &str = "check-chars"; pub static ALL_REPEATED: &str = "all-repeated";
static OPT_COUNT: &str = "count"; pub static CHECK_CHARS: &str = "check-chars";
static OPT_IGNORE_CASE: &str = "ignore-case"; pub static COUNT: &str = "count";
static OPT_REPEATED: &str = "repeated"; pub static IGNORE_CASE: &str = "ignore-case";
static OPT_SKIP_FIELDS: &str = "skip-fields"; pub static REPEATED: &str = "repeated";
static OPT_SKIP_CHARS: &str = "skip-chars"; pub static SKIP_FIELDS: &str = "skip-fields";
static OPT_UNIQUE: &str = "unique"; pub static SKIP_CHARS: &str = "skip-chars";
static OPT_ZERO_TERMINATED: &str = "zero-terminated"; pub static UNIQUE: &str = "unique";
pub static ZERO_TERMINATED: &str = "zero-terminated";
}
static ARG_FILES: &str = "files"; static ARG_FILES: &str = "files";
@ -233,63 +235,63 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.usage(&usage[..]) .usage(&usage[..])
.after_help(&long_usage[..]) .after_help(&long_usage[..])
.arg( .arg(
Arg::with_name(OPT_ALL_REPEATED) Arg::with_name(options::ALL_REPEATED)
.short("D") .short("D")
.long(OPT_ALL_REPEATED) .long(options::ALL_REPEATED)
.possible_values(&["none", "prepend", "separate"]) .possible_values(&["none", "prepend", "separate"])
.help("print all duplicate lines. Delimiting is done with blank lines") .help("print all duplicate lines. Delimiting is done with blank lines")
.value_name("delimit-method") .value_name("delimit-method")
.default_value("none"), .default_value("none"),
) )
.arg( .arg(
Arg::with_name(OPT_CHECK_CHARS) Arg::with_name(options::CHECK_CHARS)
.short("w") .short("w")
.long(OPT_CHECK_CHARS) .long(options::CHECK_CHARS)
.help("compare no more than N characters in lines") .help("compare no more than N characters in lines")
.value_name("N"), .value_name("N"),
) )
.arg( .arg(
Arg::with_name(OPT_COUNT) Arg::with_name(options::COUNT)
.short("c") .short("c")
.long(OPT_COUNT) .long(options::COUNT)
.help("prefix lines by the number of occurrences"), .help("prefix lines by the number of occurrences"),
) )
.arg( .arg(
Arg::with_name(OPT_IGNORE_CASE) Arg::with_name(options::IGNORE_CASE)
.short("i") .short("i")
.long(OPT_IGNORE_CASE) .long(options::IGNORE_CASE)
.help("ignore differences in case when comparing"), .help("ignore differences in case when comparing"),
) )
.arg( .arg(
Arg::with_name(OPT_REPEATED) Arg::with_name(options::REPEATED)
.short("d") .short("d")
.long(OPT_REPEATED) .long(options::REPEATED)
.help("only print duplicate lines"), .help("only print duplicate lines"),
) )
.arg( .arg(
Arg::with_name(OPT_SKIP_CHARS) Arg::with_name(options::SKIP_CHARS)
.short("s") .short("s")
.long(OPT_SKIP_CHARS) .long(options::SKIP_CHARS)
.help("avoid comparing the first N characters") .help("avoid comparing the first N characters")
.value_name("N"), .value_name("N"),
) )
.arg( .arg(
Arg::with_name(OPT_SKIP_FIELDS) Arg::with_name(options::SKIP_FIELDS)
.short("f") .short("f")
.long(OPT_SKIP_FIELDS) .long(options::SKIP_FIELDS)
.help("avoid comparing the first N fields") .help("avoid comparing the first N fields")
.value_name("N"), .value_name("N"),
) )
.arg( .arg(
Arg::with_name(OPT_UNIQUE) Arg::with_name(options::UNIQUE)
.short("u") .short("u")
.long(OPT_UNIQUE) .long(options::UNIQUE)
.help("only print unique lines"), .help("only print unique lines"),
) )
.arg( .arg(
Arg::with_name(OPT_ZERO_TERMINATED) Arg::with_name(options::ZERO_TERMINATED)
.short("z") .short("z")
.long(OPT_ZERO_TERMINATED) .long(options::ZERO_TERMINATED)
.help("end lines with 0 byte, not newline"), .help("end lines with 0 byte, not newline"),
) )
.arg( .arg(
@ -316,11 +318,11 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
}; };
let uniq = Uniq { let uniq = Uniq {
repeats_only: matches.is_present(OPT_REPEATED) repeats_only: matches.is_present(options::REPEATED)
|| matches.occurrences_of(OPT_ALL_REPEATED) > 0, || matches.occurrences_of(options::ALL_REPEATED) > 0,
uniques_only: matches.is_present(OPT_UNIQUE), uniques_only: matches.is_present(options::UNIQUE),
all_repeated: matches.occurrences_of(OPT_ALL_REPEATED) > 0, all_repeated: matches.occurrences_of(options::ALL_REPEATED) > 0,
delimiters: match matches.value_of(OPT_ALL_REPEATED).map(String::from) { delimiters: match matches.value_of(options::ALL_REPEATED).map(String::from) {
Some(ref opt_arg) if opt_arg != "none" => match &(*opt_arg.as_str()) { Some(ref opt_arg) if opt_arg != "none" => match &(*opt_arg.as_str()) {
"prepend" => Delimiters::Prepend, "prepend" => Delimiters::Prepend,
"separate" => Delimiters::Separate, "separate" => Delimiters::Separate,
@ -328,12 +330,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
}, },
_ => Delimiters::None, _ => Delimiters::None,
}, },
show_counts: matches.is_present(OPT_COUNT), show_counts: matches.is_present(options::COUNT),
skip_fields: opt_parsed(OPT_SKIP_FIELDS, &matches), skip_fields: opt_parsed(options::SKIP_FIELDS, &matches),
slice_start: opt_parsed(OPT_SKIP_CHARS, &matches), slice_start: opt_parsed(options::SKIP_CHARS, &matches),
slice_stop: opt_parsed(OPT_CHECK_CHARS, &matches), slice_stop: opt_parsed(options::CHECK_CHARS, &matches),
ignore_case: matches.is_present(OPT_IGNORE_CASE), ignore_case: matches.is_present(options::IGNORE_CASE),
zero_terminated: matches.is_present(OPT_ZERO_TERMINATED), zero_terminated: matches.is_present(options::ZERO_TERMINATED),
}; };
uniq.print_uniq( uniq.print_uniq(
&mut open_input_file(in_file_name), &mut open_input_file(in_file_name),