-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from CleanCut/clap-derive
Clap derive
- Loading branch information
Showing
2 changed files
with
22 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,33 @@ | ||
use clap::{App, Arg}; | ||
use clap::Parser; | ||
|
||
#[derive(Debug)] | ||
#[derive(Debug, clap::Parser)] | ||
pub struct Opts { | ||
#[clap(help = "Read from a file instead of stdin")] | ||
pub filename: Option<String>, | ||
#[clap( | ||
help = "Number of first lines of a file to display", | ||
short = 'H', | ||
long = "head", | ||
default_value_t = 10 | ||
)] | ||
pub head: usize, | ||
#[clap( | ||
help = "Number of last lines of a file to display", | ||
short = 'T', | ||
long = "tail", | ||
default_value_t = 10 | ||
)] | ||
pub tail: usize, | ||
#[clap( | ||
help = "Wait for additional data to be appended to a file. Ignored if standard input is a pipe.", | ||
short = 'f', | ||
long = "follow" | ||
)] | ||
pub follow: bool, | ||
} | ||
|
||
impl Opts { | ||
pub fn parse_args() -> Self { | ||
let matches = App::new("headtail") | ||
.arg(Arg::with_name("filename").help("Read from a file instead of stdin")) | ||
.arg( | ||
Arg::with_name("head") | ||
.short('H') | ||
.long("head") | ||
.takes_value(true) | ||
.default_value("10") | ||
.help("Number of first lines of a file to display"), | ||
) | ||
.arg( | ||
Arg::with_name("tail") | ||
.short('T') | ||
.long("tail") | ||
.takes_value(true) | ||
.default_value("10") | ||
.help("Number of last lines of a file to display"), | ||
) | ||
.arg( | ||
Arg::with_name("follow") | ||
.short('f') | ||
.long("follow") | ||
.help("Wait for additional data to be appended to a file. Ignored if standard input is a pipe."), | ||
) | ||
.get_matches(); | ||
let filename = matches.value_of("filename").map(|f| f.to_string()); | ||
let head = matches | ||
.value_of("head") | ||
.unwrap() | ||
.parse::<usize>() | ||
.expect("could not parse number for head"); // TODO: more graceful error handling | ||
let tail = matches | ||
.value_of("tail") | ||
.unwrap() | ||
.parse::<usize>() | ||
.expect("could not parse number for tail"); // TODO: more graceful error handling | ||
let follow = matches.is_present("follow"); | ||
Self { | ||
filename, | ||
head, | ||
tail, | ||
follow, | ||
} | ||
Self::parse() | ||
} | ||
} |