-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
43 lines (40 loc) · 1.41 KB
/
main.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
42
43
use clap::{arg, App};
use std::fs::File;
use std::io::{self, BufRead, BufReader};
fn main() {
let matches = App::new("cat")
.about("Rust version cat")
.arg(arg!([file] ...))
.arg(arg!(number: -n "Number the output lines"))
.get_matches();
let number_lines: bool = matches.is_present("number");
match matches.values_of("file") {
Some(files) => {
for filename in files {
match File::open(filename) {
Ok(file) => {
for (num, line_result) in BufReader::new(file).lines().enumerate() {
let line = line_result.unwrap();
if number_lines {
println!("{:>6}\t{}", num + 1, line);
} else {
println!("{}", line);
}
}
}
Err(err) => eprintln!("Failed to open {}: {}", filename, err),
}
}
}
None => {
for (num, line_result) in BufReader::new(io::stdin()).lines().enumerate() {
let line = line_result.unwrap();
if number_lines {
println!("{:>6}\t{}", num + 1, line)
} else {
println!("{}", line)
}
}
}
}
}