1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #178 from ebfe/comm

comm: add --version and --output-delimiter
This commit is contained in:
Arcterus 2014-05-18 12:43:45 -07:00
commit 99dca744ed

View file

@ -21,21 +21,27 @@ use std::path::Path;
static NAME : &'static str = "comm";
static VERSION : &'static str = "1.0.0";
fn delim(col: int, opts: &getopts::Matches) -> ~str {
fn mkdelim(col: uint, opts: &getopts::Matches) -> StrBuf {
let mut s = StrBuf::new();
let delim = match opts.opt_str("output-delimiter") {
Some(d) => d.clone(),
None => "\t".to_strbuf(),
};
if col > 1 && !opts.opt_present("1") {
s.push_str("\t");
s.push_str(delim.as_slice());
}
if col > 2 && !opts.opt_present("2") {
s.push_str("\t");
s.push_str(delim.as_slice());
}
s.to_owned()
s
}
fn comm(a: &mut Box<Buffer>, b: &mut Box<Buffer>, opts: &getopts::Matches) {
let delim = Vec::from_fn(4, |col| mkdelim(col, opts));
let mut ra = a.read_line();
let mut rb = b.read_line();
@ -50,19 +56,19 @@ fn comm(a: &mut Box<Buffer>, b: &mut Box<Buffer>, opts: &getopts::Matches) {
match ord {
Less => {
if !opts.opt_present("1") {
print!("{}{}", delim(1, opts), ra.unwrap());
print!("{}{}", delim.get(1), ra.unwrap());
}
ra = a.read_line();
}
Greater => {
if !opts.opt_present("2") {
print!("{}{}", delim(2, opts), rb.unwrap());
print!("{}{}", delim.get(2), rb.unwrap());
}
rb = b.read_line();
}
Equal => {
if !opts.opt_present("3") {
print!("{}{}", delim(3, opts), ra.unwrap());
print!("{}{}", delim.get(3), ra.unwrap());
}
ra = a.read_line();
rb = b.read_line();
@ -87,7 +93,9 @@ pub fn main() {
getopts::optflag("1", "", "suppress column 1 (lines uniq to FILE1)"),
getopts::optflag("2", "", "suppress column 2 (lines uniq to FILE2)"),
getopts::optflag("3", "", "suppress column 3 (lines that appear in both files)"),
getopts::optopt("", "output-delimiter", "separate columns with STR", "STR"),
getopts::optflag("h", "help", "display this help and exit"),
getopts::optflag("V", "version", "output version information and exit"),
];
let matches = match getopts::getopts(args.tail(), opts) {
@ -95,6 +103,11 @@ pub fn main() {
Err(err) => fail!("{}", err.to_err_msg()),
};
if matches.opt_present("version") {
println!("{} {}", NAME, VERSION);
return;
}
if matches.opt_present("help") || matches.free.len() != 2 {
println!("{} {}", NAME, VERSION);
println!("");