diff --git a/README.md b/README.md index 20eea14..4f53d25 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,14 @@ This is the harmony CLI, a minimal implementation The current help text: -``` -Usage: example-cli [OPTIONS] +```` +Usage: harmony_cli [OPTIONS] Options: - -r, --run Name of score to run - -i, --interactive Run interactive or not - -a, --all Run all or nth + -r, --run Run score(s) or not + -f, --filter Filter query + -i, --interactive Run interactive TUI or not + -a, --all Run all or nth, defaults to all -n, --number Run nth matching, zero indexed [default: 0] -l, --list list scores, will also be affected by run filter -h, --help Print help @@ -30,3 +31,4 @@ Options: ## Core architecture ![Harmony Core Architecture](docs/diagrams/Harmony_Core_Architecture.drawio.svg) +```` diff --git a/harmony_cli/src/lib.rs b/harmony_cli/src/lib.rs index c6bdcbc..4d4538f 100644 --- a/harmony_cli/src/lib.rs +++ b/harmony_cli/src/lib.rs @@ -1,3 +1,5 @@ +use std::error::Error; + use clap::builder::ArgPredicate; use clap::{Arg, CommandFactory, Parser}; use harmony; @@ -9,26 +11,20 @@ use harmony_tui; #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { - /// Name of score to run - #[cfg(feature = "tui")] - #[arg( - short, - long, - default_value_if("interactive", ArgPredicate::IsPresent, Some("")) - )] - run: Option, + /// Run score(s) or not + #[arg(short, long, default_value_t = false)] + run: bool, - /// Name of score to run - #[cfg(not(feature = "tui"))] + /// Filter query #[arg(short, long)] - run: String, + filter: Option, - /// Run interactive or not + /// Run interactive TUI or not #[arg(short, long, default_value_t = false)] interactive: bool, - /// Run all or nth - #[arg(short, long, default_value_t = false)] + /// Run all or nth, defaults to all + #[arg(short, long, default_value_t = true)] all: bool, /// Run nth matching, zero indexed @@ -56,41 +52,46 @@ pub async fn init( } // if no score to run provided, and list isn't specified, print help - if args.run.is_none() && !args.list { + if !args.run && !args.list { + println!("Please include at least --run or --list"); Args::command().print_help(); return Ok(()); } let scores = maestro.scores(); let scores_read = scores.read().expect("Should be able to read scores"); - let scores_vec: Vec>> = match args.run.clone() { - Some(filter) => scores_read + let scores_vec: Vec>> = match args.filter.clone() { + Some(f) => scores_read .iter() .map(|s| s.clone_box()) - .filter(|s| s.name().contains(&filter)) + .filter(|s| s.name().contains(&f)) .collect(), None => scores_read.iter().map(|s| s.clone_box()).collect(), }; + if scores_vec.len() == 0 { + return Err("No score found".into()); + } + // if list option is specified, print filtered list and exit if args.list { println!("Available scores: {:#?}", scores_vec); return Ok(()); } - if scores_vec.len() == 0 { - return Err("No score containing query found".into()); - } - // if all is specified, run all. Otherwise, run the first match or specified index if args.all { for s in scores_vec { - println!("Running: {}", s.clone_box().name()); + println!("Running: {}", s.name()); + maestro.interpret(s).await?; } } else { let score = scores_vec.get(args.number as usize); match score { - Some(s) => println!("Running: {}", s.clone_box().name()), + Some(s) => { + println!("Running: {}", s.name()); + maestro.interpret(s.clone_box()).await?; + } None => return Err("Invalid index given".into()), } }