blob: 9fc9293975c7d2096af5fa8778a3adde9051c4f4 [file] [log] [blame]
Jakub Koturc72d7202020-12-21 17:28:15 +01001use std::env;
2use std::error::Error;
3use std::ffi::OsString;
4use std::fs::File;
5use std::process;
6
7fn run() -> Result<(), Box<dyn Error>> {
8 let file_path = get_first_arg()?;
9 let file = File::open(file_path)?;
10 let mut rdr = csv::Reader::from_reader(file);
11 for result in rdr.records() {
12 let record = result?;
13 println!("{:?}", record);
14 }
15 Ok(())
16}
17
18/// Returns the first positional argument sent to this process. If there are no
19/// positional arguments, then this returns an error.
20fn get_first_arg() -> Result<OsString, Box<dyn Error>> {
21 match env::args_os().nth(1) {
22 None => Err(From::from("expected 1 argument, but got none")),
23 Some(file_path) => Ok(file_path),
24 }
25}
26
27fn main() {
28 if let Err(err) = run() {
29 println!("{}", err);
30 process::exit(1);
31 }
32}