full cli args
This commit is contained in:
parent
2ac18bb640
commit
e5c360b791
2 changed files with 45 additions and 6 deletions
48
src/cli.rs
48
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 anyhow::{Context, Error};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
#[cfg_attr(debug_assertions, derive(Debug))]
|
||||||
pub(super) struct Args {
|
pub(super) struct Args {
|
||||||
#[arg(short, long, value_parser = Instrument::from_str)]
|
#[arg(short, long, value_parser = Instrument::from_str)]
|
||||||
instrument: Instrument,
|
instrument: Option<Instrument>,
|
||||||
|
#[arg(short, long)]
|
||||||
|
output: Option<PathBuf>,
|
||||||
|
score: Score,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Args {
|
impl Args {
|
||||||
pub(super) fn instrument(&self) -> &<Instrument as Deref>::Target {
|
pub(super) fn instrument(&self) -> Option<&<Instrument as Deref>::Target> {
|
||||||
&self.instrument
|
self.instrument.as_deref()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn output(&self) -> Option<&PathBuf> {
|
||||||
|
self.output.as_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[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<Self, Self::Err> {
|
||||||
|
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 = <String as Deref>::Target;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
#[cfg_attr(debug_assertions, derive(Debug))]
|
||||||
pub(super) struct Instrument(String);
|
pub(super) struct Instrument(String);
|
||||||
|
|
||||||
impl FromStr for Instrument {
|
impl FromStr for Instrument {
|
||||||
|
@ -26,7 +64,7 @@ impl FromStr for Instrument {
|
||||||
read_to_string(maybe_a_path)
|
read_to_string(maybe_a_path)
|
||||||
.context("you gave me the path of a real file but reading it is hard!")?
|
.context("you gave me the path of a real file but reading it is hard!")?
|
||||||
} else {
|
} else {
|
||||||
value.to_owned() // hope it's good
|
value.to_owned() // hope it's a good one
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ mod cli;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let args = cli::Args::try_parse()?;
|
let args = cli::Args::try_parse()?;
|
||||||
println!("got: {}", args.instrument());
|
#[cfg(debug_assertions)]
|
||||||
|
println!("{:?}", args);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue