fix(lib): handles the Option for bytes

- use the usize value_parser
This commit is contained in:
minhtrannhat 2023-09-20 21:47:56 -04:00
parent 2b1a912efe
commit c2fdf88ed4
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062

View File

@ -35,14 +35,14 @@ pub fn get_args() -> HeadrResult<Config> {
.default_values(["-"]), .default_values(["-"]),
) )
.arg( .arg(
Arg::new("number") Arg::new("lines")
.short('n') .short('n')
.long("lines") .long("lines")
.help( .help(
"Print the first K lines instead of the first 10 with the leading '-', print all but the last K lines of each file") "Print the first K lines instead of the first 10 with the leading '-', print all but the last K lines of each file")
.num_args(1) .num_args(1)
.default_value("10") .default_value("10")
.value_parser(value_parser!(u16).range(0..)) .value_parser(value_parser!(usize))
.action(ArgAction::Set) .action(ArgAction::Set)
) )
.arg( .arg(
@ -52,7 +52,7 @@ pub fn get_args() -> HeadrResult<Config> {
.help( .help(
"print the first K bytes of each file; with the leading '-', print all but the last K lines of each file") "print the first K bytes of each file; with the leading '-', print all but the last K lines of each file")
.num_args(1) .num_args(1)
.value_parser(value_parser!(u16).range(0..)) .value_parser(value_parser!(usize))
.action(ArgAction::Set) .action(ArgAction::Set)
) )
.get_matches(); .get_matches();
@ -63,7 +63,16 @@ pub fn get_args() -> HeadrResult<Config> {
.expect("Must contains valid filepaths") .expect("Must contains valid filepaths")
.cloned() .cloned()
.collect(), .collect(),
lines: *matches.get_one("number").expect(""), lines: *matches.get_one::<usize>("lines").unwrap(),
bytes: *matches.get_one("bytes").expect(""), bytes: match matches.get_one::<usize>("bytes") {
Some(bytes) => Some(*bytes),
None => None,
},
}) })
} }
pub fn run(config: Config) -> HeadrResult<()> {
println!("{:#?}", config);
Ok(())
}