-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathcli-args-clap3-stdin-stdout.rs
More file actions
58 lines (50 loc) · 1.51 KB
/
cli-args-clap3-stdin-stdout.rs
File metadata and controls
58 lines (50 loc) · 1.51 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use clap::{AppSettings, Clap};
use std::error::Error;
use std::fs::File;
use std::io;
use std::io::{Read, Write};
use std::process;
/// A sample-program
///
/// The program is a show-case how to read data either from file or stdin and write data either to
/// file or stdout
#[derive(Clap)]
#[clap(version = "0.1")]
#[clap(setting = AppSettings::ColoredHelp)]
struct Opts {
/// Optional output file; default stdout
#[clap(short, long)]
output: Option<String>,
/// Optional input file; default stdin
input: Option<String>,
}
fn main() {
let opts: Opts = Opts::parse();
let exit = match (opts.input, opts.output) {
(None, None) => run(io::stdin(), io::stdout()),
(None, Some(f)) => run(io::stdin(), open_output(f)),
(Some(f), None) => run(open_input(f), io::stdout()),
(Some(fin), Some(fout)) => run(open_input(fin), open_output(fout)),
};
if let Err(err) = exit {
println!("{}", err);
process::exit(1);
}
}
fn open_input(f: String) -> File {
File::open(f).expect("Could not open input file")
}
fn open_output(f: String) -> File {
File::create(f).expect("Could not open output file")
}
fn run(input: impl Read, output: impl Write) -> Result<(), Box<dyn Error>> {
let mut rdr = csv::Reader::from_reader(input);
let mut wtr = csv::Writer::from_writer(output);
wtr.write_record(rdr.headers()?)?;
for result in rdr.records() {
let record = result?;
wtr.write_record(&record)?;
}
wtr.flush()?;
Ok(())
}