Skip to content

Commit 47d80aa

Browse files
authored
Merge pull request #9 from jakewilliami/use-clap-derive
Use clap `Derive` for cleaner command line interface
2 parents 44f34d1 + 50f0f78 commit 47d80aa

File tree

3 files changed

+73
-52
lines changed

3 files changed

+73
-52
lines changed

Cargo.toml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
[package]
22
name = "rohe"
3-
version = "2.0.0"
3+
version = "2.0.1"
44
authors = ["Jake W. Ireland <jakewilliami@icloud.com>"]
55
edition = "2018"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
# tokio = {version = "0.2", features = ["full"]}
11-
tokio = { version = "1", features = ["full"] }
12-
# async-std = { verson = "1", features = ["attributes", "tokio1"] }
13-
base64 = "0.13.0"
14-
reqwest = "0.11.4"
10+
base64 = "0.13.1"
11+
clap = { version = "4.0.19", features = ["cargo", "wrap_help", "derive"] }
12+
hyper = "0.14.22"
1513
lazy_static = "1.4.0"
16-
clap = "2.33.3"
17-
hyper = "0.14.11"
18-
serde_json = "1.0.66"
19-
serde = { version = "1.0", features = ["derive"] }
14+
reqwest = "0.11.12"
15+
serde = { version = "1.0.147", features = ["derive"] }
2016
# serde = "1.0.127"
21-
17+
serde_json = "1.0.87"
18+
# tokio = {version = "0.2", features = ["full"]}
19+
tokio = { version = "1.21.2", features = ["full"] }
20+
# async-std = { verson = "1", features = ["attributes", "tokio1"] }

src/main.rs

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,65 @@ use postcodes::*;
88
use response::*;
99

1010
extern crate clap;
11-
use clap::{Arg, App, crate_version};
11+
use clap::{ArgAction, crate_version, Parser};
12+
13+
#[derive(Parser)]
14+
#[command(
15+
name = "rohe",
16+
author = "Jake·W.·Ireland.·<jakewilliami@icloud.com>",
17+
version = crate_version!(),
18+
about = "A command line interface for NZP's locator API.",
19+
long_about = "A command line interface for NZP's locator API. The name 'rohe' is the Māori word for 'areas'.",
20+
)]
21+
struct Cli {
22+
/// Takes address as input. Default return value is the address' associated postcode
23+
#[arg(
24+
short = 'a',
25+
long = "address",
26+
action = ArgAction::Set,
27+
num_args = 0..=1,
28+
value_name = "address",
29+
)]
30+
addr: Option<String>,
31+
32+
/// Takes postcode as input. Default return value is the postcode's associated region
33+
#[arg(
34+
short = 'p',
35+
long = "postcode",
36+
action = ArgAction::Set,
37+
num_args = 0..=1,
38+
value_name = "postcode"
39+
)]
40+
postcode: Option<String>,
41+
42+
/// Return address as (latitude, longitude)
43+
#[arg(
44+
short = 'c',
45+
long = "coordinates",
46+
action = ArgAction::Set,
47+
num_args = 0..=1,
48+
value_name = "address",
49+
)]
50+
coords: Option<String>,
51+
}
1252

1353
#[tokio::main]
1454
async fn main() {
15-
let matches = App::new("rohe")
16-
.version(crate_version!())
17-
.author("Jake W. Ireland. <jakewilliami@icloud.com>")
18-
.about("A command line interface for NZP's locator API. The name 'rohe' is the Māori word for 'areas'.")
19-
.arg(Arg::with_name("ADDR")
20-
.short("a")
21-
.long("address")
22-
.help("Takes address as input. Default return value is the address' associated postcode.")
23-
.takes_value(true)
24-
.required(false)
25-
.multiple(false)
26-
)
27-
.arg(Arg::with_name("POSTCODE")
28-
.short("p")
29-
.long("postcode")
30-
.help("Takes postcode as input. Default return value is the postcode's associated region.")
31-
.takes_value(true)
32-
.required(false)
33-
.multiple(false)
34-
)
35-
.arg(Arg::with_name("ADDR_FOR_COORDS")
36-
.short("c")
37-
.long("coordinates")
38-
.help("Return addresses as (latitude, longitude).")
39-
.takes_value(true)
40-
.required(false)
41-
.multiple(false)
42-
)
43-
.get_matches();
55+
let cli = Cli::parse();
4456

4557
// Find postcode information
46-
if matches.is_present("POSTCODE") {
58+
if let Some(postcode_str) = cli.postcode {
4759
let bad_response: &str = "There was no postcode in the database that matched your input.";
4860

4961
// get value of postcode
50-
let postcode = matches.value_of("POSTCODE").unwrap().parse_postcode();
62+
let postcode = postcode_str.parse_postcode();
5163

5264
// request postcodes from the API
5365
let matched_postcodes: Option<Vec<EachPostcode>> = request::get_suggested_postcodes(postcode).await;
5466

5567
// initialise the response string
5668
let mut resp = String::new();
57-
if matched_postcodes.as_ref().is_none() || matched_postcodes.as_ref().unwrap().len() == 0 {
69+
if matched_postcodes.as_ref().is_none() || matched_postcodes.as_ref().unwrap().is_empty() {
5870
resp.push_str(bad_response);
5971
} else {
6072
let postcodes = &matched_postcodes.unwrap();
@@ -69,15 +81,15 @@ async fn main() {
6981
let details: Option<serde_json::Map<String, serde_json::Value>> = request::get_postcode_details(*unique_id).await;
7082

7183
// construct the response string
72-
if details.is_none() {
73-
resp.push_str(bad_response);
74-
} else {
84+
if let Some(details) = details {
7585
resp.push_str(full_partial);
7686
resp.push_str(" ∈ ");
77-
resp.push_str(details.unwrap()["CityTown"].as_str().unwrap());
87+
resp.push_str(details["CityTown"].as_str().unwrap());
7888
if i != (postcodes.len() - 1) {
79-
resp.push_str("\n")
89+
resp.push('\n');
8090
}
91+
} else {
92+
resp.push_str(bad_response);
8193
}
8294
}
8395
}
@@ -86,10 +98,14 @@ async fn main() {
8698
}
8799

88100
// Find address information
89-
if matches.is_present("ADDR") {
101+
if let Some(addr) = cli.addr {
90102
// get value of address
91-
let addr = matches.value_of("ADDR").unwrap();
92-
let resp: Option<Vec<EachAddress>> = request::get_suggested_addresses(addr.to_string()).await;
103+
let resp: Option<Vec<EachAddress>> = request::get_suggested_addresses(addr).await;
93104
println!("{:?}", resp);
94105
}
106+
107+
// Get address coordinated
108+
if let Some(_addr_for_coords) = cli.coords {
109+
todo!();
110+
}
95111
}

tests/tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-directory
2+
3+
#[test]
4+
fn test() {
5+
true;
6+
}

0 commit comments

Comments
 (0)