Skip to content

Commit 1d6ecd0

Browse files
committed
Use arguments from cli file
1 parent 608373a commit 1d6ecd0

2 files changed

Lines changed: 34 additions & 30 deletions

File tree

src/cli.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,30 @@ pub fn get_app_cli<'a, 'b>(version: &'b str) -> App<'a, 'b> {
55
.version(&*version)
66
.author("Jessica Deen <jessica.deen@microsoft.com>")
77
.about("Easy CLI to control Elgato Keylight")
8+
.arg(
9+
Arg::with_name("switch")
10+
.index(1)
11+
.required(true)
12+
.short("s")
13+
.long("switch")
14+
.takes_value(true)
15+
.value_name("ON/OFF")
16+
.help("Switch value for light status: Accepted values are: on, off.")
17+
)
18+
.arg(
19+
Arg::with_name("brightness")
20+
.help("Brightness value for light: Accepted values are: low, medium, high.")
21+
.required(true)
22+
.index(2)
23+
.default_value("20"),
24+
)
25+
.arg(
26+
Arg::with_name("temperature")
27+
.help("Temperature value for light: Accepted values are: warm, medium, cool.")
28+
.required(true)
29+
.index(3)
30+
.default_value("213"),
31+
)
832
.arg(
933
Arg::with_name("ELGATO_IP")
1034
.long("elgato-ip-address")
@@ -27,25 +51,4 @@ pub fn get_app_cli<'a, 'b>(version: &'b str) -> App<'a, 'b> {
2751
.env("NUMBER_OF_LIGHTS")
2852
.takes_value(true),
2953
)
30-
.arg(
31-
Arg::with_name("SWITCH")
32-
.help("Switch value for light status: Accepted values are: on, off.")
33-
.required(true)
34-
.index(1)
35-
.default_value("off"),
36-
)
37-
.arg(
38-
Arg::with_name("BRIGHTNESS")
39-
.help("Brightness value for light: Accepted values are: low, medium, high.")
40-
.required(true)
41-
.index(2)
42-
.default_value("medium"),
43-
)
44-
.arg(
45-
Arg::with_name("TEMPERATURE")
46-
.help("Temperature value for light: Accepted values are: warm, medium, cool.")
47-
.required(true)
48-
.index(3)
49-
.default_value("medium"),
50-
)
5154
}

src/main.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ mod cli;
44
use cli::get_app_cli;
55
use reqwest::Client;
66
use serde_json::json;
7-
use std::env;
7+
use std::collections::HashMap;
88

99
#[tokio::main]
1010
async fn main() -> Result<(), Box<dyn std::error::Error>> {
11-
let version = format!(
12-
"{}.{}",
13-
env!("CARGO_PKG_VERSION"),
14-
option_env!("BUILD_BUILDID").unwrap_or("0")
15-
);
11+
let version = format!("v{}", env!("CARGO_PKG_VERSION"));
1612

1713
let matches = get_app_cli(&version).get_matches();
1814
let elgato_ip = matches.value_of("ELGATO_IP").unwrap();
1915
let numberoflights = matches.value_of("NUMBER_OF_LIGHTS").unwrap();
16+
let switch = matches.value_of("switch").and_then(|s| s.parse::<u8>().ok())
17+
.unwrap();;
18+
let brightness = matches.value_of("brightness").and_then(|s| s.parse::<u8>().ok())
19+
.unwrap();;
20+
let temperature = matches.value_of("temperature").and_then(|s| s.parse::<u8>().ok())
21+
.unwrap();;
2022

21-
let switch = matches.value_of("SWITCH").unwrap();
22-
let brightness = matches.value_of("BRIGHTNESS").unwrap();
23-
let temperature = matches.value_of("TEMPERATURE").unwrap();
23+
println!("Value for switch: {}", switch);
2424

2525
let body = json!({
2626
"numberOfLights":numberoflights,
@@ -32,6 +32,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3232
}
3333
]
3434
});
35+
3536
let url = format!("http://{}:{}", elgato_ip, "9123/elgato/lights");
3637

3738
println!("State: {}", url);

0 commit comments

Comments
 (0)