diff options
author | Sam Tay <sam.chong.tay@gmail.com> | 2020-06-02 23:34:11 -0700 |
---|---|---|
committer | Sam Tay <sam.chong.tay@gmail.com> | 2020-06-02 23:34:11 -0700 |
commit | 224547551bd7b525fd9f8c0d137c593a143d0008 (patch) | |
tree | 32dbdc6bb3e189dc316703290366740640e46d25 /src | |
parent | 9a9594870a0068c74b7fee0aff5944ec02864cf6 (diff) |
Parse CLI args with clap
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index e7a11a9..45788c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,37 @@ +use clap::clap_app; +use clap::App; + +// TODO pull defaults from config file +// may require dropping the macros +fn mk_app<'a, 'b>() -> App<'a, 'b> { + clap_app!(so => + (version: clap::crate_version!()) + (author: clap::crate_authors!()) + (about: clap::crate_description!()) + (@arg site: -s --site +takes_value default_value("stackoverflow") "StackExchange site") + (@arg query: ... +required "Query to search") + ) +} + fn main() { - println!("Hello, world!"); + let matches = mk_app().get_matches(); + println!("{:?}", matches); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_cli() { + let m = mk_app().get_matches_from(vec![ + "so", "--site", "meta", "how", "do", "I", "exit", "Vim", + ]); + println!("{:?}", m); + assert_eq!(m.value_of("site"), Some("meta")); + assert_eq!( + m.values_of("query").unwrap().collect::<Vec<_>>().join(" "), + "how do I exit Vim" + ); + } } |