From e76e1de3a3ded101a011677c8d206aa6587c5e47 Mon Sep 17 00:00:00 2001 From: Alex Moon Date: Tue, 22 Dec 2020 09:25:36 -0700 Subject: [PATCH] Switch from `structopt` to `clap` to reduce dependencies. --- Cargo.toml | 10 +------ examples/beep.rs | 36 ++++++++++++++++++++---- examples/feedback.rs | 63 ++++++++++++++++++++++++++++++++++-------- examples/record_wav.rs | 36 ++++++++++++++++++++---- 4 files changed, 112 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f7ce079..0e8f68c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ thiserror = "1.0.2" anyhow = "1.0.12" hound = "3.4" ringbuf = "0.2" -structopt = "0.3.21" +clap = { version = "2.33.3", default-features = false } [target.'cfg(target_os = "windows")'.dependencies] winapi = { version = "0.3", features = ["audiosessiontypes", "audioclient", "coml2api", "combaseapi", "debug", "devpkey", "handleapi", "ksmedia", "mmdeviceapi", "objbase", "profileapi", "std", "synchapi", "winbase", "winuser"] } @@ -60,20 +60,12 @@ crate-type = ["cdylib"] [[example]] name = "beep" -path = "examples/beep.rs" -crate-type = ["bin"] [[example]] name = "enumerate" -path = "examples/enumerate.rs" -crate-type = ["bin"] [[example]] name = "feedback" -path = "examples/feedback.rs" -crate-type = ["bin"] [[example]] name = "record_wav" -path = "examples/record_wav.rs" -crate-type = ["bin"] diff --git a/examples/beep.rs b/examples/beep.rs index 80920fd..ed15204 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -1,24 +1,48 @@ extern crate anyhow; +extern crate clap; extern crate cpal; -extern crate structopt; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; -use structopt::StructOpt; -#[derive(Debug, StructOpt)] -#[structopt(name = "beep", about = "Simple example of audio playback.")] +#[derive(Debug)] struct Opt { #[cfg(all( any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), feature = "jack" ))] - #[structopt(short, long)] jack: bool, - #[structopt(default_value = "default")] device: String, } +impl Opt { + fn from_args() -> Self { + let app = clap::App::new("beep").arg_from_usage("[DEVICE] 'The audio device to use'"); + #[cfg(all( + any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), + feature = "jack" + ))] + let app = app.arg_from_usage("-j, --jack 'Use the JACK host"); + let matches = app.get_matches(); + let device = matches.value_of("DEVICE").unwrap_or("default").to_string(); + + #[cfg(all( + any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), + feature = "jack" + ))] + return Opt { + jack: matches.is_present("jack"), + device, + }; + + #[cfg(any( + not(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd")), + not(feature = "jack") + ))] + Opt { device } + } +} + fn main() -> anyhow::Result<()> { let opt = Opt::from_args(); diff --git a/examples/feedback.rs b/examples/feedback.rs index cb4cac5..f0656b6 100644 --- a/examples/feedback.rs +++ b/examples/feedback.rs @@ -7,36 +7,75 @@ //! precisely synchronised. extern crate anyhow; +extern crate clap; extern crate cpal; extern crate ringbuf; -extern crate structopt; +use anyhow::Context; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use ringbuf::RingBuffer; -use structopt::StructOpt; -#[derive(Debug, StructOpt)] -#[structopt(name = "beep", about = "Simple example of audio playback.")] +#[derive(Debug)] struct Opt { #[cfg(all( any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), feature = "jack" ))] - #[structopt(short, long)] jack: bool, - #[structopt(short, long, default_value = "150.0")] latency: f32, - - #[structopt(default_value = "default")] input_device: String, - - #[structopt(default_value = "default")] output_device: String, } -fn main() -> Result<(), anyhow::Error> { - let opt = Opt::from_args(); +impl Opt { + fn from_args() -> anyhow::Result { + let app = clap::App::new("beep") + .arg_from_usage( + "-l, --latency [DELAY_MS] 'Specify the delay between input and output [default: 150]'", + ) + .arg_from_usage("[IN] 'The input audio device to use'") + .arg_from_usage("[OUT] 'The output audio device to use'"); + + #[cfg(all( + any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), + feature = "jack" + ))] + let app = app.arg_from_usage("-j, --jack 'Use the JACK host"); + let matches = app.get_matches(); + let latency: f32 = matches + .value_of("latency") + .unwrap_or("150") + .parse() + .context("parsing latency option")?; + let input_device = matches.value_of("IN").unwrap_or("default").to_string(); + let output_device = matches.value_of("OUT").unwrap_or("default").to_string(); + + #[cfg(all( + any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), + feature = "jack" + ))] + return Ok(Opt { + jack: matches.is_present("jack"), + latency, + input_device, + output_device, + }); + + #[cfg(any( + not(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd")), + not(feature = "jack") + ))] + Ok(Opt { + latency, + input_device, + output_device, + }) + } +} + +fn main() -> anyhow::Result<()> { + let opt = Opt::from_args()?; // Conditionally compile with jack if the feature is specified. #[cfg(all( diff --git a/examples/record_wav.rs b/examples/record_wav.rs index e4c9090..7746f56 100644 --- a/examples/record_wav.rs +++ b/examples/record_wav.rs @@ -3,30 +3,54 @@ //! The input data is recorded to "$CARGO_MANIFEST_DIR/recorded.wav". extern crate anyhow; +extern crate clap; extern crate cpal; extern crate hound; -extern crate structopt; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use std::fs::File; use std::io::BufWriter; use std::sync::{Arc, Mutex}; -use structopt::StructOpt; -#[derive(Debug, StructOpt)] -#[structopt(name = "beep", about = "Simple example of audio playback.")] +#[derive(Debug)] struct Opt { #[cfg(all( any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), feature = "jack" ))] - #[structopt(short, long)] jack: bool, - #[structopt(default_value = "default")] device: String, } +impl Opt { + fn from_args() -> Self { + let app = clap::App::new("beep").arg_from_usage("[DEVICE] 'The audio device to use'"); + #[cfg(all( + any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), + feature = "jack" + ))] + let app = app.arg_from_usage("-j, --jack 'Use the JACK host"); + let matches = app.get_matches(); + let device = matches.value_of("DEVICE").unwrap_or("default").to_string(); + + #[cfg(all( + any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), + feature = "jack" + ))] + return Opt { + jack: matches.is_present("jack"), + device, + }; + + #[cfg(any( + not(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd")), + not(feature = "jack") + ))] + Opt { device } + } +} + fn main() -> Result<(), anyhow::Error> { let opt = Opt::from_args();