fnmain() { letmatches = App::new("kt") .version("0.1.0") .author("Jérémie Veillet. jeremie@example.com") .about("A drop in cat replacement written in Rust") .arg(Arg::with_name("FILE") .help("File to print.") .empty_values(false) ) .get_matches(); }
$ cargo run -- -h Finished dev [unoptimized + debuginfo] target(s) in 0.03s Running `target/debug/kt -h` kt 0.1.0 Jérémie Veillet. jeremie@example.com A drop-in cat replacement written in Rust
USAGE: kt [FILE]
FLAGS: -h, --help Prints help information -V, --version Prints version information
ARGS: <FILE> File to print.
$ cargo run --- -V Finished dev [unoptimized + debuginfo] target(s) in 0.04s Running target/debug/kt -V kt 0.1.0
我们还可以尝试不带任何参数,启动程序,看看会发生什么。
1 2 3
$ cargo run -- Finished dev [unoptimized + debuginfo] target(s) in 0.03s Running `target/debug/kt`
fnmain() { letmatches = App::new("kt") .version("0.1.0") .author("Jérémie Veillet. jeremie@example.com") .about("A drop in cat replacement written in Rust") .arg(Arg::with_name("FILE") .help("File to print.") .empty_values(false) ) .get_matches();
use clap::{Arg, App}; use std::path::Path; use std::process;
fnmain() { letmatches = App::new("kt") .version("0.1.0") .author("Jérémie Veillet. jeremie@example.com") .about("A drop in cat replacement written in Rust") .arg(Arg::with_name("FILE") .help("File to print.") .empty_values(false) ) .get_matches();
ifletSome(file) = matches.value_of("FILE") { println!("Value for file argument: {}", file); if Path::new(&file).exists() { println!("File exist!!"); } else { eprintln!("[kt Error] No such file or directory."); process::exit(1); // 程序错误终止时的标准退出码 } } }
我们快要完成了!现在我们需要读取文件的内容并将结果显示在 stdout 中。
同样,我们将使用一个名为 File 的标准库来读取文件。我们将使用 open 方法读取文件的内容,然后将其写入一个字符串对象,该对象将在 stdout 中显示。
use clap::{Arg, App}; use std::path::Path; use std::process; use std::fs::File; use std::io::{Read};
fnmain() { letmatches = App::new("kt") .version("0.1.0") .author("Jérémie Veillet. jeremie@example.com") .about("A drop in cat replacement written in Rust") .arg(Arg::with_name("FILE") .help("File to print.") .empty_values(false) ) .get_matches(); ifletSome(file) = matches.value_of("FILE") { if Path::new(&file).exists() { println!("File exist!!"); letmut f = File::open(file).expect("[kt Error] File not found."); letmut data = String::new(); f.read_to_string(&mut data).expect("[kt Error] Unable to read the file."); println!("{}", data); } else { eprintln!("[kt Error] No such file or directory."); process::exit(1); } } }
$ cargo build Compiling kt v0.1.0 (/home/jeremie/Development/kt) Finished dev [unoptimized + debuginfo] target(s) in 0.70s $ cargo run -- ./src/main.rs Finished dev [unoptimized + debuginfo] target(s) in 0.03s Running `target/debug/kt ./src/main.rs` File exist!! extern crate clap;
use clap::{Arg, App}; use std::path::Path; use std::process; use std::fs::File; use std::io::{Read};
fn main() { let matches = App::new("kt") .version("0.1.0") .author("Jérémie Veillet. jeremie@example.com") .about("A drop in cat replacement written in Rust") .arg(Arg::with_name("FILE") .help("File to print.") .empty_values(false) ) .get_matches();
iflet Some(file) = matches.value_of("FILE") { if Path::new(&file).exists() { println!("File exist!!"); let mut f = File::open(file).expect("[kt Error] File not found."); let mut data = String::new(); f.read_to_string(&mut data).expect("[kt Error] Unable to read the file."); println!("{}", data); } else { eprintln!("[kt Error] No such file or directory."); process::exit(1); } } }
use clap::{Arg, App}; use std::path::Path; use std::process; use std::fs::File; use std::io::{Read, Write};
fnmain() { letmatches = App::new("kt") .version("0.1.0") .author("Jérémie Veillet. jeremie@example.com") .about("A drop in cat replacement written in Rust") .arg(Arg::with_name("FILE") .help("File to print.") .empty_values(false) ) .get_matches();
ifletSome(file) = matches.value_of("FILE") { if Path::new(&file).exists() { match File::open(file) { Ok(mut f) => { letmut data = String::new(); f.read_to_string(&mut data).expect("[kt Error] Unable to read the file."); letstdout = std::io::stdout(); // 获取全局 stdout 对象 letmut handle = std::io::BufWriter::new(stdout); // 可选项:将 handle 包装在缓冲区中 matchwriteln!(handle, "{}", data) { Ok(_res) => {}, Err(err) => { eprintln!("[kt Error] Unable to display the file contents. {:?}", err); process::exit(1); }, } } Err(err) => { eprintln!("[kt Error] Unable to read the file. {:?}", err); process::exit(1); }, } } else { eprintln!("[kt Error] No such file or directory."); process::exit(1); } } }
$ cargo run -- ./src/main.rs Finished dev [unoptimized + debuginfo] target(s) in 0.02s Running `target/debug/kt ./src/main.rs` extern crate clap;
use clap::{Arg, App}; use std::path::Path; use std::process; use std::fs::File; use std::io::{Read, Write};
fn main() { let matches = App::new("kt") .version("0.1.0") .author("Jérémie Veillet. jeremie@example.com") .about("A drop in cat replacement written in Rust") .arg(Arg::with_name("FILE") .help("File to print.") .empty_values(false) ) .get_matches();
iflet Some(file) = matches.value_of("FILE") { if Path::new(&file).exists() { match File::open(file) { Ok(mut f) => { let mut data = String::new(); f.read_to_string(&mut data).expect("[kt Error] Unable to read the file."); let stdout = std::io::stdout(); // 获取全局 stdout 对象 let mut handle = std::io::BufWriter::new(stdout); // 可选项:将 handle 包装在缓冲区中 match writeln!(handle, "{}", data) { Ok(_res) => {}, Err(err) => { eprintln!("[kt Error] Unable to display the file contents. {:?}", err); process::exit(1); }, } } Err(err) => { eprintln!("[kt Error] Unable to read the file. {:?}", err); process::exit(1); }, } } else { eprintln!("[kt Error] No such file or directory."); process::exit(1); } } }