From 2ac18bb6402fee0587594c5640cf0a4e5bb3a2a3 Mon Sep 17 00:00:00 2001 From: p6nj Date: Wed, 21 Aug 2024 01:34:56 +0200 Subject: [PATCH] started cli --- Cargo.toml | 15 +++++++++++++++ src/cli.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 10 ++++++++++ 3 files changed, 64 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/cli.rs create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c360204 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "bng" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow = "1.0" +clap = { version = "4.5", features = ["derive"] } +fasteval = "0.2.4" +tinyaudio = { version = "0.1", optional = true } + +[features] +default = ["play", "save"] +play = ["dep:tinyaudio"] +save = [] diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..0c6699a --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,39 @@ +use std::{fs::read_to_string, ops::Deref, path::Path, str::FromStr}; + +use anyhow::{Context, Error}; +use clap::Parser; + +#[derive(Parser)] +pub(super) struct Args { + #[arg(short, long, value_parser = Instrument::from_str)] + instrument: Instrument, +} + +impl Args { + pub(super) fn instrument(&self) -> &::Target { + &self.instrument + } +} + +#[derive(Clone)] +pub(super) struct Instrument(String); + +impl FromStr for Instrument { + type Err = Error; + fn from_str(value: &str) -> Result { + let maybe_a_path = Path::new(&value); + Ok(Instrument(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 good + })) + } +} + +impl Deref for Instrument { + type Target = ::Target; + fn deref(&self) -> &Self::Target { + &self.0 + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..75aa9ce --- /dev/null +++ b/src/main.rs @@ -0,0 +1,10 @@ +use anyhow::Result; +use clap::Parser; + +mod cli; + +fn main() -> Result<()> { + let args = cli::Args::try_parse()?; + println!("got: {}", args.instrument()); + Ok(()) +}