mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
Merge pull request #1948 from k0ur0x/comm-clap
comm: move from getopts to clap
This commit is contained in:
commit
94dd70e1d0
2 changed files with 61 additions and 27 deletions
|
@ -15,7 +15,7 @@ edition = "2018"
|
||||||
path = "src/comm.rs"
|
path = "src/comm.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
getopts = "0.2.18"
|
clap = "2.33"
|
||||||
libc = "0.2.42"
|
libc = "0.2.42"
|
||||||
uucore = { version=">=0.0.7", package="uucore", path="../../uucore" }
|
uucore = { version=">=0.0.7", package="uucore", path="../../uucore" }
|
||||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
|
@ -15,21 +15,34 @@ use std::fs::File;
|
||||||
use std::io::{self, stdin, BufRead, BufReader, Stdin};
|
use std::io::{self, stdin, BufRead, BufReader, Stdin};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
static SYNTAX: &str = "[OPTIONS] FILE1 FILE2";
|
use clap::{App, Arg, ArgMatches};
|
||||||
static SUMMARY: &str = "Compare sorted files line by line";
|
|
||||||
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
static ABOUT: &str = "compare two sorted files line by line";
|
||||||
static LONG_HELP: &str = "";
|
static LONG_HELP: &str = "";
|
||||||
|
|
||||||
fn mkdelim(col: usize, opts: &getopts::Matches) -> String {
|
mod options {
|
||||||
let mut s = String::new();
|
pub const COLUMN_1: &str = "1";
|
||||||
let delim = match opts.opt_str("output-delimiter") {
|
pub const COLUMN_2: &str = "2";
|
||||||
Some(d) => d,
|
pub const COLUMN_3: &str = "3";
|
||||||
None => "\t".to_owned(),
|
pub const DELIMITER: &str = "output-delimiter";
|
||||||
};
|
pub const DELIMITER_DEFAULT: &str = "\t";
|
||||||
|
pub const FILE_1: &str = "FILE1";
|
||||||
|
pub const FILE_2: &str = "FILE2";
|
||||||
|
}
|
||||||
|
|
||||||
if col > 1 && !opts.opt_present("1") {
|
fn get_usage() -> String {
|
||||||
|
format!("{} [OPTION]... FILE1 FILE2", executable!())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mkdelim(col: usize, opts: &ArgMatches) -> String {
|
||||||
|
let mut s = String::new();
|
||||||
|
let delim = opts.value_of(options::DELIMITER).unwrap();
|
||||||
|
|
||||||
|
if col > 1 && !opts.is_present(options::COLUMN_1) {
|
||||||
s.push_str(delim.as_ref());
|
s.push_str(delim.as_ref());
|
||||||
}
|
}
|
||||||
if col > 2 && !opts.opt_present("2") {
|
if col > 2 && !opts.is_present(options::COLUMN_2) {
|
||||||
s.push_str(delim.as_ref());
|
s.push_str(delim.as_ref());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +70,7 @@ impl LineReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) {
|
fn comm(a: &mut LineReader, b: &mut LineReader, opts: &ArgMatches) {
|
||||||
let delim: Vec<String> = (0..4).map(|col| mkdelim(col, opts)).collect();
|
let delim: Vec<String> = (0..4).map(|col| mkdelim(col, opts)).collect();
|
||||||
|
|
||||||
let ra = &mut String::new();
|
let ra = &mut String::new();
|
||||||
|
@ -80,7 +93,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) {
|
||||||
|
|
||||||
match ord {
|
match ord {
|
||||||
Ordering::Less => {
|
Ordering::Less => {
|
||||||
if !opts.opt_present("1") {
|
if !opts.is_present(options::COLUMN_1) {
|
||||||
ensure_nl(ra);
|
ensure_nl(ra);
|
||||||
print!("{}{}", delim[1], ra);
|
print!("{}{}", delim[1], ra);
|
||||||
}
|
}
|
||||||
|
@ -88,7 +101,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) {
|
||||||
na = a.read_line(ra);
|
na = a.read_line(ra);
|
||||||
}
|
}
|
||||||
Ordering::Greater => {
|
Ordering::Greater => {
|
||||||
if !opts.opt_present("2") {
|
if !opts.is_present(options::COLUMN_2) {
|
||||||
ensure_nl(rb);
|
ensure_nl(rb);
|
||||||
print!("{}{}", delim[2], rb);
|
print!("{}{}", delim[2], rb);
|
||||||
}
|
}
|
||||||
|
@ -96,7 +109,7 @@ fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) {
|
||||||
nb = b.read_line(rb);
|
nb = b.read_line(rb);
|
||||||
}
|
}
|
||||||
Ordering::Equal => {
|
Ordering::Equal => {
|
||||||
if !opts.opt_present("3") {
|
if !opts.is_present(options::COLUMN_3) {
|
||||||
ensure_nl(ra);
|
ensure_nl(ra);
|
||||||
print!("{}{}", delim[3], ra);
|
print!("{}{}", delim[3], ra);
|
||||||
}
|
}
|
||||||
|
@ -120,21 +133,42 @@ fn open_file(name: &str) -> io::Result<LineReader> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let args = args.collect_str();
|
let usage = get_usage();
|
||||||
|
|
||||||
let matches = app!(SYNTAX, SUMMARY, LONG_HELP)
|
let matches = App::new(executable!())
|
||||||
.optflag("1", "", "suppress column 1 (lines uniq to FILE1)")
|
.version(VERSION)
|
||||||
.optflag("2", "", "suppress column 2 (lines uniq to FILE2)")
|
.about(ABOUT)
|
||||||
.optflag(
|
.usage(&usage[..])
|
||||||
"3",
|
.after_help(LONG_HELP)
|
||||||
"",
|
.arg(
|
||||||
"suppress column 3 (lines that appear in both files)",
|
Arg::with_name(options::COLUMN_1)
|
||||||
|
.short(options::COLUMN_1)
|
||||||
|
.help("suppress column 1 (lines unique to FILE1)"),
|
||||||
)
|
)
|
||||||
.optopt("", "output-delimiter", "separate columns with STR", "STR")
|
.arg(
|
||||||
.parse(args);
|
Arg::with_name(options::COLUMN_2)
|
||||||
|
.short(options::COLUMN_2)
|
||||||
|
.help("suppress column 2 (lines unique to FILE2)"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(options::COLUMN_3)
|
||||||
|
.short(options::COLUMN_3)
|
||||||
|
.help("suppress column 3 (lines that appear in both files)"),
|
||||||
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(options::DELIMITER)
|
||||||
|
.long(options::DELIMITER)
|
||||||
|
.help("separate columns with STR")
|
||||||
|
.value_name("STR")
|
||||||
|
.default_value(options::DELIMITER_DEFAULT)
|
||||||
|
.hide_default_value(true),
|
||||||
|
)
|
||||||
|
.arg(Arg::with_name(options::FILE_1).required(true))
|
||||||
|
.arg(Arg::with_name(options::FILE_2).required(true))
|
||||||
|
.get_matches_from(args);
|
||||||
|
|
||||||
let mut f1 = open_file(matches.free[0].as_ref()).unwrap();
|
let mut f1 = open_file(matches.value_of(options::FILE_1).unwrap()).unwrap();
|
||||||
let mut f2 = open_file(matches.free[1].as_ref()).unwrap();
|
let mut f2 = open_file(matches.value_of(options::FILE_2).unwrap()).unwrap();
|
||||||
|
|
||||||
comm(&mut f1, &mut f2, &matches);
|
comm(&mut f1, &mut f2, &matches);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue