diff --git a/src/lib.rs b/src/lib.rs index 6f2162d..8e21b28 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ type MyResult = Result>; #[derive(Debug)] pub struct Config { - files: Vec<&str>, + files: Vec, number_lines: bool, number_nonblank_lines: bool, } @@ -27,20 +27,21 @@ pub fn get_args() -> MyResult { .arg( Arg::new("file") .value_name("FILE") - .help("Input file(s)") - .num_args(1..), + .help("Input file(s) [default: -]") + .num_args(1..) + .default_values(["-"]), ) .arg( - Arg::new("number_lines") + Arg::new("number") .short('n') - .help("Number the lines (excluding the non-blank lines).") + .help("Number lines") .num_args(0) .action(ArgAction::SetTrue), ) .arg( - Arg::new("number_nonblank_lines") + Arg::new("number_nonblank") .short('b') - .help("Number the lines (including the non-blank lines).") + .help("Number nonblank lines") .num_args(0) .action(ArgAction::SetTrue), ) @@ -49,10 +50,16 @@ pub fn get_args() -> MyResult { Ok(Config { files: matches .get_many::("file") - .expect("Must be a valid file path") - .map(|s| s.as_str()) + .expect("Must contains valid filepaths") + .cloned() .collect(), - number_lines: matches.get_flag("number_lines"), - number_nonblank_lines: matches.get_flag("number_nonblank_lines"), + number_lines: matches.get_flag("number"), + number_nonblank_lines: matches.get_flag("number_nonblank"), }) } + +pub fn run(config: Config) -> MyResult<()> { + dbg!(config); + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 5bf256e..efd1fd0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ fn main() { - println!("Hello world"); + if let Err(e) = catr::get_args().and_then(catr::run) { + eprint!("{}", e); + std::process::exit(1); + } } diff --git a/tests/cli.rs b/tests/cli.rs index 4a27c3b..7f1379f 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -64,11 +64,7 @@ fn run(args: &[&str], expected_file: &str) -> TestResult { } // -------------------------------------------------- -fn run_stdin( - input_file: &str, - args: &[&str], - expected_file: &str, -) -> TestResult { +fn run_stdin(input_file: &str, args: &[&str], expected_file: &str) -> TestResult { let input = fs::read_to_string(input_file)?; let expected = fs::read_to_string(expected_file)?; Command::cargo_bin(PRG)?