From e5c360b79103c457b03e7c1582c4e4f49b447c4f Mon Sep 17 00:00:00 2001 From: p6nj Date: Sat, 24 Aug 2024 14:52:55 +0200 Subject: [PATCH 1/3] full cli args --- src/cli.rs | 48 +++++++++++++++++++++++++++++++++++++++++++----- src/main.rs | 3 ++- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 0c6699a..a2c46b3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,21 +1,59 @@ -use std::{fs::read_to_string, ops::Deref, path::Path, str::FromStr}; +use std::{ + fs::read_to_string, + ops::Deref, + path::{Path, PathBuf}, + str::FromStr, +}; use anyhow::{Context, Error}; use clap::Parser; #[derive(Parser)] +#[cfg_attr(debug_assertions, derive(Debug))] pub(super) struct Args { #[arg(short, long, value_parser = Instrument::from_str)] - instrument: Instrument, + instrument: Option, + #[arg(short, long)] + output: Option, + score: Score, } impl Args { - pub(super) fn instrument(&self) -> &::Target { - &self.instrument + pub(super) fn instrument(&self) -> Option<&::Target> { + self.instrument.as_deref() + } + + pub(super) fn output(&self) -> Option<&PathBuf> { + self.output.as_ref() } } #[derive(Clone)] +#[cfg_attr(debug_assertions, derive(Debug))] +pub(super) struct Score(String); + +impl FromStr for Score { + type Err = Error; + fn from_str(value: &str) -> Result { + let maybe_a_path = Path::new(&value); + Ok(Score(if maybe_a_path.is_file() { + read_to_string(maybe_a_path) + .context("you gave me the path of a real file but reading it is hard!")? + } else { + value.to_owned() // hope it's a good one + })) + } +} + +impl Deref for Score { + type Target = ::Target; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +#[derive(Clone)] +#[cfg_attr(debug_assertions, derive(Debug))] pub(super) struct Instrument(String); impl FromStr for Instrument { @@ -26,7 +64,7 @@ impl FromStr for Instrument { read_to_string(maybe_a_path) .context("you gave me the path of a real file but reading it is hard!")? } else { - value.to_owned() // hope it's good + value.to_owned() // hope it's a good one })) } } diff --git a/src/main.rs b/src/main.rs index 75aa9ce..d3528bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod cli; fn main() -> Result<()> { let args = cli::Args::try_parse()?; - println!("got: {}", args.instrument()); + #[cfg(debug_assertions)] + println!("{:?}", args); Ok(()) } From 0c5bcf66afaa4bdd20d13545c146e0ae1b8470a6 Mon Sep 17 00:00:00 2001 From: p6nj Date: Sat, 24 Aug 2024 14:55:26 +0200 Subject: [PATCH 2/3] full cli args --- src/cli.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index a2c46b3..3cba449 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -18,16 +18,6 @@ pub(super) struct Args { score: Score, } -impl Args { - pub(super) fn instrument(&self) -> Option<&::Target> { - self.instrument.as_deref() - } - - pub(super) fn output(&self) -> Option<&PathBuf> { - self.output.as_ref() - } -} - #[derive(Clone)] #[cfg_attr(debug_assertions, derive(Debug))] pub(super) struct Score(String); From 7b52fc000ded6e24ce322e71c57616d6d803bdd4 Mon Sep 17 00:00:00 2001 From: p6nj Date: Sat, 24 Aug 2024 15:09:50 +0200 Subject: [PATCH 3/3] conditional cli args --- Cargo.toml | 4 ++++ src/cli.rs | 17 +++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c360204..8291a4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,11 +5,15 @@ edition = "2021" [dependencies] anyhow = "1.0" +cfg-if = "1.0.0" clap = { version = "4.5", features = ["derive"] } fasteval = "0.2.4" tinyaudio = { version = "0.1", optional = true } [features] +# default = [] +# default = ["save"] +# default = ["play"] default = ["play", "save"] play = ["dep:tinyaudio"] save = [] diff --git a/src/cli.rs b/src/cli.rs index 3cba449..e09a10b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,11 +1,12 @@ -use std::{ - fs::read_to_string, - ops::Deref, - path::{Path, PathBuf}, - str::FromStr, -}; +use std::{fs::read_to_string, ops::Deref, path::Path, str::FromStr}; +cfg_if! { + if #[cfg(save)] { + use std::path::PathBuf; + } +} use anyhow::{Context, Error}; +use cfg_if::cfg_if; use clap::Parser; #[derive(Parser)] @@ -13,8 +14,12 @@ use clap::Parser; pub(super) struct Args { #[arg(short, long, value_parser = Instrument::from_str)] instrument: Option, + #[cfg(save)] #[arg(short, long)] output: Option, + #[cfg(play)] + #[arg(short, long)] + silent: bool, score: Score, }