started cli
This commit is contained in:
parent
5f4ffa4705
commit
2ac18bb640
3 changed files with 64 additions and 0 deletions
15
Cargo.toml
Normal file
15
Cargo.toml
Normal file
|
@ -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 = []
|
39
src/cli.rs
Normal file
39
src/cli.rs
Normal file
|
@ -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) -> &<Instrument as Deref>::Target {
|
||||
&self.instrument
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct Instrument(String);
|
||||
|
||||
impl FromStr for Instrument {
|
||||
type Err = Error;
|
||||
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||
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 = <String as Deref>::Target;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
10
src/main.rs
Normal file
10
src/main.rs
Normal file
|
@ -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(())
|
||||
}
|
Loading…
Reference in a new issue