From 0a5d3b531a409ac901dd3ade328d12b4b5820ba9 Mon Sep 17 00:00:00 2001 From: tahahawa Date: Mon, 14 Apr 2025 11:03:47 -0400 Subject: [PATCH] Updates to address comments --- README.md | 3 ++- harmony_cli/Cargo.toml | 1 + harmony_cli/src/lib.rs | 42 +++++++++++++++++++++++++++++++----------- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d5adee6..20eea14 100644 --- a/README.md +++ b/README.md @@ -15,13 +15,14 @@ This is the harmony CLI, a minimal implementation The current help text: ``` -Usage: example-cli [OPTIONS] --run +Usage: example-cli [OPTIONS] Options: -r, --run Name of score to run -i, --interactive Run interactive or not -a, --all Run all or nth -n, --number Run nth matching, zero indexed [default: 0] + -l, --list list scores, will also be affected by run filter -h, --help Print help -V, --version Print version ``` diff --git a/harmony_cli/Cargo.toml b/harmony_cli/Cargo.toml index ee8ccc2..525dc86 100644 --- a/harmony_cli/Cargo.toml +++ b/harmony_cli/Cargo.toml @@ -12,4 +12,5 @@ harmony_tui = { path = "../harmony_tui", optional = true } [features] +default = ["tui"] tui = ["dep:harmony_tui"] diff --git a/harmony_cli/src/lib.rs b/harmony_cli/src/lib.rs index e9c82b6..c6bdcbc 100644 --- a/harmony_cli/src/lib.rs +++ b/harmony_cli/src/lib.rs @@ -1,5 +1,5 @@ -use clap::Parser; use clap::builder::ArgPredicate; +use clap::{Arg, CommandFactory, Parser}; use harmony; use harmony::{score::Score, topology::Topology}; @@ -14,9 +14,9 @@ struct Args { #[arg( short, long, - default_value_if("interactive", ArgPredicate::IsPresent, Some("unused")) + default_value_if("interactive", ArgPredicate::IsPresent, Some("")) )] - run: String, + run: Option, /// Name of score to run #[cfg(not(feature = "tui"))] @@ -34,6 +34,10 @@ struct Args { /// Run nth matching, zero indexed #[arg(short, long, default_value_t = 0)] number: u8, + + /// list scores, will also be affected by run filter + #[arg(short, long, default_value_t = false)] + list: bool, } pub async fn init( @@ -51,24 +55,40 @@ pub async fn init( return Err("Not compiled with interactive support".into()); } + // if no score to run provided, and list isn't specified, print help + if args.run.is_none() && !args.list { + Args::command().print_help(); + return Ok(()); + } + let scores = maestro.scores(); let scores_read = scores.read().expect("Should be able to read scores"); - let filtered: Vec>> = scores_read - .iter() - .map(|s| s.clone_box()) - .filter(|s| s.name().contains(&args.run)) - .collect(); + let scores_vec: Vec>> = match args.run.clone() { + Some(filter) => scores_read + .iter() + .map(|s| s.clone_box()) + .filter(|s| s.name().contains(&filter)) + .collect(), + None => scores_read.iter().map(|s| s.clone_box()).collect(), + }; - if filtered.len() == 0 { + // 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 filtered { + for s in scores_vec { println!("Running: {}", s.clone_box().name()); } } else { - let score = filtered.get(args.number as usize); + let score = scores_vec.get(args.number as usize); match score { Some(s) => println!("Running: {}", s.clone_box().name()), None => return Err("Invalid index given".into()),