summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Tay <sam.chong.tay@gmail.com>2020-06-02 23:34:11 -0700
committerSam Tay <sam.chong.tay@gmail.com>2020-06-02 23:34:11 -0700
commit224547551bd7b525fd9f8c0d137c593a143d0008 (patch)
tree32dbdc6bb3e189dc316703290366740640e46d25
parent9a9594870a0068c74b7fee0aff5944ec02864cf6 (diff)
Parse CLI args with clap
-rw-r--r--Cargo.lock113
-rw-r--r--Cargo.toml1
-rw-r--r--TODO.md3
-rw-r--r--src/main.rs36
4 files changed, 149 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..67e85eb
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,113 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "ansi_term"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "atty"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+dependencies = [
+ "hermit-abi",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "bitflags"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+
+[[package]]
+name = "clap"
+version = "2.33.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129"
+dependencies = [
+ "ansi_term",
+ "atty",
+ "bitflags",
+ "strsim",
+ "textwrap",
+ "unicode-width",
+ "vec_map",
+]
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "91780f809e750b0a89f5544be56617ff6b1227ee485bcb06ebe10cdf89bd3b71"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.71"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
+
+[[package]]
+name = "so"
+version = "0.1.0"
+dependencies = [
+ "clap",
+]
+
+[[package]]
+name = "strsim"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
+
+[[package]]
+name = "textwrap"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
+dependencies = [
+ "unicode-width",
+]
+
+[[package]]
+name = "unicode-width"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
+
+[[package]]
+name = "vec_map"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
+
+[[package]]
+name = "winapi"
+version = "0.3.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/Cargo.toml b/Cargo.toml
index b03e6ad..e7411ec 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+clap = "2.33.1"
diff --git a/TODO.md b/TODO.md
index 8bc6e8f..ab6b703 100644
--- a/TODO.md
+++ b/TODO.md
@@ -1,8 +1,5 @@
# TODO
-1. Clap
- - [setup](https://rust-lang-nursery.github.io/rust-cookbook/cli/arguments.html#parse-command-line-arguments)
- - [handle version properly](https://docs.rs/clap/2.33.1/clap/macro.crate_version.html)
2. Hit SO API
- [json specs](https://rust-lang-nursery.github.io/rust-cookbook/encoding/complex.html#serialize-and-deserialize-unstructured-json)
- [hitting api](https://rust-lang-nursery.github.io/rust-cookbook/web/clients/apis.html)
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"
+ );
+ }
}