From 128e0dc718b08aae4694c8d3b2879b2239d96e61 Mon Sep 17 00:00:00 2001 From: t4ccer Date: Tue, 30 Jul 2024 10:11:29 -0600 Subject: [PATCH] Add macro for CLI subcommands --- cgt_cli/src/clap_utils.rs | 24 ++++++++++++++ cgt_cli/src/commands.rs | 14 ++++---- cgt_cli/src/commands/amazons.rs | 22 ++----------- cgt_cli/src/commands/canonical_form.rs | 22 ++----------- cgt_cli/src/commands/domineering.rs | 33 +++---------------- .../commands/domineering/genetic_search.rs | 2 +- cgt_cli/src/commands/quicksort.rs | 22 ++----------- cgt_cli/src/commands/snort.rs | 33 +++---------------- cgt_cli/src/commands/snort/genetic.rs | 2 +- cgt_cli/src/commands/snort/graph.rs | 2 +- cgt_cli/src/commands/snort/latex.rs | 2 +- cgt_cli/src/commands/wind_up.rs | 22 ++----------- cgt_cli/src/main.rs | 28 ++-------------- 13 files changed, 57 insertions(+), 171 deletions(-) create mode 100644 cgt_cli/src/clap_utils.rs diff --git a/cgt_cli/src/clap_utils.rs b/cgt_cli/src/clap_utils.rs new file mode 100644 index 0000000..4da1975 --- /dev/null +++ b/cgt_cli/src/clap_utils.rs @@ -0,0 +1,24 @@ +macro_rules! mk_subcommand { + ($($variant:ident => $module:ident),* $(,)?) => { + $(mod $module;)* + + #[derive(::clap::Subcommand, Debug)] + pub enum Command { + $($variant($module::Args),)* + } + + #[derive(::clap::Parser, Debug)] + pub struct Args { + #[clap(subcommand)] + command: Command, + } + + pub fn run(args: Args) -> ::anyhow::Result<()> { + match args.command { + $(Command::$variant(args) => $module::run(args),)* + } + } + }; +} + +pub(crate) use mk_subcommand; diff --git a/cgt_cli/src/commands.rs b/cgt_cli/src/commands.rs index 31adbb3..3108a57 100644 --- a/cgt_cli/src/commands.rs +++ b/cgt_cli/src/commands.rs @@ -1,6 +1,8 @@ -pub(crate) mod amazons; -pub(crate) mod canonical_form; -pub(crate) mod domineering; -pub(crate) mod quicksort; -pub(crate) mod snort; -pub(crate) mod wind_up; +crate::clap_utils::mk_subcommand! { + Domineering => domineering, + Snort => snort, + Quicksort => quicksort, + WindUp => wind_up, + CanonicalForm => canonical_form, + Amazons => amazons, +} diff --git a/cgt_cli/src/commands/amazons.rs b/cgt_cli/src/commands/amazons.rs index 6fae3e0..c512cdd 100644 --- a/cgt_cli/src/commands/amazons.rs +++ b/cgt_cli/src/commands/amazons.rs @@ -1,21 +1,3 @@ -use anyhow::Result; -use clap::{self, Parser, Subcommand}; - -mod evaluate; - -#[derive(Subcommand, Debug)] -pub enum Command { - Evaluate(evaluate::Args), -} - -#[derive(Parser, Debug)] -pub struct Args { - #[clap(subcommand)] - pub command: Command, -} - -pub fn run(args: Args) -> Result<()> { - match args.command { - Command::Evaluate(args) => evaluate::run(args), - } +crate::clap_utils::mk_subcommand! { + Evaluate => evaluate, } diff --git a/cgt_cli/src/commands/canonical_form.rs b/cgt_cli/src/commands/canonical_form.rs index 36707f9..1b6f966 100644 --- a/cgt_cli/src/commands/canonical_form.rs +++ b/cgt_cli/src/commands/canonical_form.rs @@ -1,21 +1,3 @@ -use anyhow::Result; -use clap::{self, Parser, Subcommand}; - -mod sum; - -#[derive(Subcommand, Debug)] -pub enum Command { - Sum(sum::Args), -} - -#[derive(Parser, Debug)] -pub struct Args { - #[clap(subcommand)] - pub command: Command, -} - -pub fn run(args: Args) -> Result<()> { - match args.command { - Command::Sum(args) => sum::run(args), - } +crate::clap_utils::mk_subcommand! { + Sum => sum, } diff --git a/cgt_cli/src/commands/domineering.rs b/cgt_cli/src/commands/domineering.rs index 8465460..5c664d2 100644 --- a/cgt_cli/src/commands/domineering.rs +++ b/cgt_cli/src/commands/domineering.rs @@ -1,31 +1,8 @@ -use anyhow::Result; -use clap::{self, Parser, Subcommand}; - mod common; -mod evaluate; -mod exhaustive_search; -mod genetic_search; -mod latex_table; - -#[derive(Subcommand, Debug)] -pub enum Command { - ExhaustiveSearch(exhaustive_search::Args), - GeneticSearch(genetic_search::Args), - Evaluate(evaluate::Args), - LatexTable(latex_table::Args), -} - -#[derive(Parser, Debug)] -pub struct Args { - #[clap(subcommand)] - pub command: Command, -} -pub fn run(args: Args) -> Result<()> { - match args.command { - Command::ExhaustiveSearch(args) => exhaustive_search::run(args), - Command::GeneticSearch(args) => genetic_search::run(args), - Command::Evaluate(args) => evaluate::run(args), - Command::LatexTable(args) => latex_table::run(args), - } +crate::clap_utils::mk_subcommand! { + ExhaustiveSearch => exhaustive_search, + GeneticSearch => genetic_search, + Evaluate => evaluate, + LatexTable => latex_table, } diff --git a/cgt_cli/src/commands/domineering/genetic_search.rs b/cgt_cli/src/commands/domineering/genetic_search.rs index 9857cdb..74e9435 100644 --- a/cgt_cli/src/commands/domineering/genetic_search.rs +++ b/cgt_cli/src/commands/domineering/genetic_search.rs @@ -1,5 +1,5 @@ use crate::{ - domineering::common::DomineeringResult, + commands::domineering::common::DomineeringResult, io::{FileOrStderr, FileOrStdout}, }; use anyhow::{bail, Context, Result}; diff --git a/cgt_cli/src/commands/quicksort.rs b/cgt_cli/src/commands/quicksort.rs index 59823b8..e533eab 100644 --- a/cgt_cli/src/commands/quicksort.rs +++ b/cgt_cli/src/commands/quicksort.rs @@ -1,21 +1,3 @@ -use anyhow::Result; -use clap::{self, Parser, Subcommand}; - -mod range; - -#[derive(Subcommand, Debug)] -pub enum Command { - Range(range::Args), -} - -#[derive(Parser, Debug)] -pub struct Args { - #[clap(subcommand)] - pub command: Command, -} - -pub fn run(args: Args) -> Result<()> { - match args.command { - Command::Range(args) => range::run(args), - } +crate::clap_utils::mk_subcommand! { + Range => range, } diff --git a/cgt_cli/src/commands/snort.rs b/cgt_cli/src/commands/snort.rs index b58d60b..63182c5 100644 --- a/cgt_cli/src/commands/snort.rs +++ b/cgt_cli/src/commands/snort.rs @@ -1,31 +1,8 @@ -use anyhow::Result; -use clap::{self, Parser, Subcommand}; - mod common; -pub mod genetic; -pub mod graph; -pub mod latex; -pub mod three_caterpillar; - -#[derive(Subcommand, Debug)] -pub enum Command { - Genetic(genetic::Args), - Latex(latex::Args), - Graph(graph::Args), - ThreeCaterpillar(three_caterpillar::Args), -} - -#[derive(Parser, Debug)] -pub struct Args { - #[clap(subcommand)] - pub command: Command, -} -pub fn run(args: Args) -> Result<()> { - match args.command { - Command::Genetic(args) => genetic::run(args), - Command::Latex(args) => latex::run(args), - Command::Graph(args) => graph::run(args), - Command::ThreeCaterpillar(args) => three_caterpillar::run(args), - } +crate::clap_utils::mk_subcommand! { + Genetic => genetic, + Latex => latex, + Graph => graph, + ThreeCaterpillar => three_caterpillar, } diff --git a/cgt_cli/src/commands/snort/genetic.rs b/cgt_cli/src/commands/snort/genetic.rs index e11a4c5..f3c537d 100644 --- a/cgt_cli/src/commands/snort/genetic.rs +++ b/cgt_cli/src/commands/snort/genetic.rs @@ -1,4 +1,4 @@ -use crate::{io::FileOrStderr, snort::common::Log}; +use crate::{commands::snort::common::Log, io::FileOrStderr}; use anyhow::{Context, Result}; use cgt::{ genetic_algorithm::{Algorithm, GeneticAlgorithm, Scored}, diff --git a/cgt_cli/src/commands/snort/graph.rs b/cgt_cli/src/commands/snort/graph.rs index 80707e2..f680990 100644 --- a/cgt_cli/src/commands/snort/graph.rs +++ b/cgt_cli/src/commands/snort/graph.rs @@ -1,4 +1,4 @@ -use crate::snort::common::{analyze_position, Edge}; +use crate::commands::snort::common::{analyze_position, Edge}; use anyhow::Result; use cgt::{ graph::undirected::Graph, diff --git a/cgt_cli/src/commands/snort/latex.rs b/cgt_cli/src/commands/snort/latex.rs index b81103b..8083a45 100644 --- a/cgt_cli/src/commands/snort/latex.rs +++ b/cgt_cli/src/commands/snort/latex.rs @@ -1,6 +1,6 @@ use crate::{ + commands::snort::common::Log, io::{FileOrStdin, FileOrStdout}, - snort::common::Log, }; use anyhow::{bail, Context, Result}; use cgt::numeric::rational::Rational; diff --git a/cgt_cli/src/commands/wind_up.rs b/cgt_cli/src/commands/wind_up.rs index 59823b8..e533eab 100644 --- a/cgt_cli/src/commands/wind_up.rs +++ b/cgt_cli/src/commands/wind_up.rs @@ -1,21 +1,3 @@ -use anyhow::Result; -use clap::{self, Parser, Subcommand}; - -mod range; - -#[derive(Subcommand, Debug)] -pub enum Command { - Range(range::Args), -} - -#[derive(Parser, Debug)] -pub struct Args { - #[clap(subcommand)] - pub command: Command, -} - -pub fn run(args: Args) -> Result<()> { - match args.command { - Command::Range(args) => range::run(args), - } +crate::clap_utils::mk_subcommand! { + Range => range, } diff --git a/cgt_cli/src/main.rs b/cgt_cli/src/main.rs index affd0ad..06cf754 100644 --- a/cgt_cli/src/main.rs +++ b/cgt_cli/src/main.rs @@ -1,7 +1,8 @@ use crate::commands::*; use anyhow::Result; -use clap::{self, Parser, Subcommand}; +use clap::Parser; +pub(crate) mod clap_utils; mod commands; mod io; @@ -9,30 +10,7 @@ mod io; #[global_allocator] static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; -#[derive(Subcommand, Debug)] -enum Command { - Domineering(domineering::Args), - Snort(snort::Args), - Quicksort(quicksort::Args), - WindUp(wind_up::Args), - CanonicalForm(canonical_form::Args), - Amazons(amazons::Args), -} - -#[derive(Parser)] -struct Args { - #[clap(subcommand)] - command: Command, -} - fn main() -> Result<()> { let args = Args::parse(); - match args.command { - Command::Domineering(args) => domineering::run(args), - Command::Snort(args) => snort::run(args), - Command::Quicksort(args) => quicksort::run(args), - Command::WindUp(args) => wind_up::run(args), - Command::CanonicalForm(args) => canonical_form::run(args), - Command::Amazons(args) => amazons::run(args), - } + crate::commands::run(args) }