Fix(lib): Correctly get the filepaths

- Use std::string::String instead of &str
This commit is contained in:
minhtrannhat 2023-08-30 21:19:19 -04:00
parent 634330feca
commit 70e58cdbd2
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
3 changed files with 23 additions and 17 deletions

View File

@ -6,7 +6,7 @@ type MyResult<T> = Result<T, Box<dyn Error>>;
#[derive(Debug)] #[derive(Debug)]
pub struct Config { pub struct Config {
files: Vec<&str>, files: Vec<String>,
number_lines: bool, number_lines: bool,
number_nonblank_lines: bool, number_nonblank_lines: bool,
} }
@ -27,20 +27,21 @@ pub fn get_args() -> MyResult<Config> {
.arg( .arg(
Arg::new("file") Arg::new("file")
.value_name("FILE") .value_name("FILE")
.help("Input file(s)") .help("Input file(s) [default: -]")
.num_args(1..), .num_args(1..)
.default_values(["-"]),
) )
.arg( .arg(
Arg::new("number_lines") Arg::new("number")
.short('n') .short('n')
.help("Number the lines (excluding the non-blank lines).") .help("Number lines")
.num_args(0) .num_args(0)
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
) )
.arg( .arg(
Arg::new("number_nonblank_lines") Arg::new("number_nonblank")
.short('b') .short('b')
.help("Number the lines (including the non-blank lines).") .help("Number nonblank lines")
.num_args(0) .num_args(0)
.action(ArgAction::SetTrue), .action(ArgAction::SetTrue),
) )
@ -49,10 +50,16 @@ pub fn get_args() -> MyResult<Config> {
Ok(Config { Ok(Config {
files: matches files: matches
.get_many::<String>("file") .get_many::<String>("file")
.expect("Must be a valid file path") .expect("Must contains valid filepaths")
.map(|s| s.as_str()) .cloned()
.collect(), .collect(),
number_lines: matches.get_flag("number_lines"), number_lines: matches.get_flag("number"),
number_nonblank_lines: matches.get_flag("number_nonblank_lines"), number_nonblank_lines: matches.get_flag("number_nonblank"),
}) })
} }
pub fn run(config: Config) -> MyResult<()> {
dbg!(config);
Ok(())
}

View File

@ -1,3 +1,6 @@
fn main() { fn main() {
println!("Hello world"); if let Err(e) = catr::get_args().and_then(catr::run) {
eprint!("{}", e);
std::process::exit(1);
}
} }

View File

@ -64,11 +64,7 @@ fn run(args: &[&str], expected_file: &str) -> TestResult {
} }
// -------------------------------------------------- // --------------------------------------------------
fn run_stdin( fn run_stdin(input_file: &str, args: &[&str], expected_file: &str) -> TestResult {
input_file: &str,
args: &[&str],
expected_file: &str,
) -> TestResult {
let input = fs::read_to_string(input_file)?; let input = fs::read_to_string(input_file)?;
let expected = fs::read_to_string(expected_file)?; let expected = fs::read_to_string(expected_file)?;
Command::cargo_bin(PRG)? Command::cargo_bin(PRG)?