-
Notifications
You must be signed in to change notification settings - Fork 71
/
behead.rs
41 lines (33 loc) · 1.06 KB
/
behead.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::config::{Config, Delimiter};
use crate::util;
use crate::CliResult;
use serde::Deserialize;
static USAGE: &str = "
Drop a CSV file's header.
Usage:
qsv behead [options] [<input>]
Common options:
-h, --help Display this message
-o, --output <file> Write output to <file> instead of stdout.
-d, --delimiter <arg> The field delimiter for reading CSV data.
Must be a single character. (default: ,)
";
#[derive(Deserialize)]
struct Args {
arg_input: Option<String>,
flag_delimiter: Option<Delimiter>,
flag_output: Option<String>,
}
pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let conf = Config::new(&args.arg_input)
.delimiter(args.flag_delimiter)
.no_headers(false);
let mut rdr = conf.reader()?;
let mut wtr = Config::new(&args.flag_output).writer()?;
let mut record = csv::ByteRecord::new();
while rdr.read_byte_record(&mut record)? {
wtr.write_byte_record(&record)?;
}
Ok(wtr.flush()?)
}