Skip to content

Commit

Permalink
Add macro for CLI subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
t4ccer committed Jul 30, 2024
1 parent 20b3c98 commit 128e0dc
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 171 deletions.
24 changes: 24 additions & 0 deletions cgt_cli/src/clap_utils.rs
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 8 additions & 6 deletions cgt_cli/src/commands.rs
Original file line number Diff line number Diff line change
@@ -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,
}
22 changes: 2 additions & 20 deletions cgt_cli/src/commands/amazons.rs
Original file line number Diff line number Diff line change
@@ -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,
}
22 changes: 2 additions & 20 deletions cgt_cli/src/commands/canonical_form.rs
Original file line number Diff line number Diff line change
@@ -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,
}
33 changes: 5 additions & 28 deletions cgt_cli/src/commands/domineering.rs
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 1 addition & 1 deletion cgt_cli/src/commands/domineering/genetic_search.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
domineering::common::DomineeringResult,
commands::domineering::common::DomineeringResult,
io::{FileOrStderr, FileOrStdout},
};
use anyhow::{bail, Context, Result};
Expand Down
22 changes: 2 additions & 20 deletions cgt_cli/src/commands/quicksort.rs
Original file line number Diff line number Diff line change
@@ -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,
}
33 changes: 5 additions & 28 deletions cgt_cli/src/commands/snort.rs
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 1 addition & 1 deletion cgt_cli/src/commands/snort/genetic.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
2 changes: 1 addition & 1 deletion cgt_cli/src/commands/snort/graph.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cgt_cli/src/commands/snort/latex.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
22 changes: 2 additions & 20 deletions cgt_cli/src/commands/wind_up.rs
Original file line number Diff line number Diff line change
@@ -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,
}
28 changes: 3 additions & 25 deletions cgt_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
use crate::commands::*;
use anyhow::Result;
use clap::{self, Parser, Subcommand};
use clap::Parser;

pub(crate) mod clap_utils;
mod commands;
mod io;

#[cfg(not(windows))]
#[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)
}

0 comments on commit 128e0dc

Please sign in to comment.