Use clap to parse arguments from the CLI #2

Merged
sorceress merged 1 commits from videogame-hacker/bwowww:patch/cli-arguments into mistress 2021-07-21 21:54:31 +00:00
2 changed files with 34 additions and 7 deletions

View File

@ -7,6 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = "2.33.3"
dbus = "0.9.3" dbus = "0.9.3"
midi-control = "0.2.0" midi-control = "0.2.0"
midir = "0.7.0" midir = "0.7.0"

View File

@ -3,6 +3,7 @@ use std::{
time::Duration, time::Duration,
}; };
use clap::clap_app;
use dbus::blocking::Connection; use dbus::blocking::Connection;
use dbus::message::MatchRule; use dbus::message::MatchRule;
use dbus::Message; use dbus::Message;
@ -10,13 +11,39 @@ use dbus::{arg::messageitem::MessageItem, channel::MatchingReceiver};
use midi_control::*; use midi_control::*;
use midir::*; use midir::*;
struct BwowwwConfig {
browser: String,
port: usize,
}
fn parse_cli_opts() -> BwowwwConfig {
let matches = clap_app!(bwowww =>
(version: "1.0")
(author: "Agatha Rose <agatharose@wantscuddl.es>")
(@arg BROWSER: -b --browser +takes_value "Set a custom browser")
(@arg PORT: -p --port +takes_value "Select a MIDI port")
)
.get_matches();
let browser = matches.value_of("BROWSER").unwrap_or("Nightly");
let browser = browser.to_string();
let port = matches
.value_of("PORT")
.and_then(|s| s.parse().ok())
.unwrap_or(1);
BwowwwConfig { browser, port }
}
fn main() { fn main() {
let conf = parse_cli_opts();
let midi_device = MidiOutput::new("synth output meow").expect("could not find device"); let midi_device = MidiOutput::new("synth output meow").expect("could not find device");
let ports = midi_device.ports(); let ports = midi_device.ports();
// assumes that the device is connected on port 1 // assumes that the device is connected on port 1
// TODO: add port selection
let midi_out = midi_device let midi_out = midi_device
.connect(&ports[1], "meow port") .connect(&ports[conf.port], "meow port")
.expect("oh dear"); .expect("oh dear");
let midi_out = Arc::new(Mutex::new(midi_out)); let midi_out = Arc::new(Mutex::new(midi_out));
@ -40,7 +67,7 @@ fn main() {
conn.start_receive( conn.start_receive(
rule, rule,
Box::new(move |msg, _| { Box::new(move |msg, _| {
midi_hook(midi_out.clone(), &msg); midi_hook(&conf, midi_out.clone(), &msg);
true true
}), }),
); );
@ -48,7 +75,7 @@ fn main() {
// Start matching using old scheme // Start matching using old scheme
rule.eavesdrop = true; rule.eavesdrop = true;
conn.add_match(rule, move |_: (), _, msg| { conn.add_match(rule, move |_: (), _, msg| {
midi_hook(midi_out.clone(), &msg); midi_hook(&conf, midi_out.clone(), &msg);
true true
}) })
.expect("add_match failed"); .expect("add_match failed");
@ -60,7 +87,7 @@ fn main() {
// TODO: use the ctrlc crate to handle the midi connection closing // TODO: use the ctrlc crate to handle the midi connection closing
} }
fn midi_hook(midi_out: Arc<Mutex<MidiOutputConnection>>, msg: &Message) { fn midi_hook(conf: &BwowwwConfig, midi_out: Arc<Mutex<MidiOutputConnection>>, msg: &Message) {
let args = msg.get_items(); let args = msg.get_items();
// do nothing if args are empty // do nothing if args are empty
@ -69,8 +96,7 @@ fn midi_hook(midi_out: Arc<Mutex<MidiOutputConnection>>, msg: &Message) {
} }
// only apply to firefox notifications // only apply to firefox notifications
// TODO: make browser name configurable if args[0] == conf.browser.as_str().into() {
if args[0] == "Nightly".into() {
// get the dict of values inside the message's args // get the dict of values inside the message's args
let dict_values = match &args[6] { let dict_values = match &args[6] {
MessageItem::Dict(v) => v.clone().into_vec(), MessageItem::Dict(v) => v.clone().into_vec(),