From e216c5ca7921d5fb4e37c7632e52dc1ff6cd3cc3 Mon Sep 17 00:00:00 2001 From: Michael Gehring Date: Sun, 18 May 2014 21:21:35 +0200 Subject: [PATCH] comm: add --output-delimiter --- comm/comm.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/comm/comm.rs b/comm/comm.rs index 7a9ad0063..dfe8bfc42 100644 --- a/comm/comm.rs +++ b/comm/comm.rs @@ -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, b: &mut Box, 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, b: &mut Box, 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,6 +93,7 @@ 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"), ];