Skip to content

Commit 9b640ef

Browse files
committed
move definition of cli parser out ot main to avoid clutter
1 parent 325941b commit 9b640ef

File tree

2 files changed

+85
-80
lines changed

2 files changed

+85
-80
lines changed

src/cli.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! This module defines the command line interface (CLI) for the application.
2+
3+
use clap::{Parser, ValueEnum};
4+
use std::path::PathBuf;
5+
use crate::solver;
6+
use crate::nfa;
7+
8+
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
9+
pub enum OutputFormat {
10+
Plain,
11+
Tex,
12+
Csv,
13+
}
14+
15+
#[derive(Parser, Debug)]
16+
#[command(version, about, long_about = None)]
17+
pub struct Args {
18+
#[arg(value_name = "AUTOMATON_FILE", help = "Path to the input")]
19+
pub filename: String,
20+
21+
#[arg(
22+
short = 'f',
23+
long = "from",
24+
value_enum,
25+
default_value = "tikz",
26+
help = "The input format"
27+
)]
28+
pub input_format: nfa::InputFormat,
29+
30+
#[arg(
31+
short = 'v',
32+
long = "verbose",
33+
action = clap::ArgAction::Count,
34+
help = "Increase verbosity level"
35+
)]
36+
pub verbosity: u8,
37+
38+
#[arg(
39+
long,
40+
short = 'l',
41+
value_name = "LOG_FILE",
42+
help = "Optional path to the log file. Defaults to stdout if not specified."
43+
)]
44+
pub log_output: Option<PathBuf>,
45+
46+
#[arg(
47+
value_enum,
48+
short = 't',
49+
long = "to",
50+
default_value = "plain",
51+
help = "The output format"
52+
)]
53+
pub output_format: OutputFormat,
54+
55+
#[arg(
56+
short = 'o',
57+
long = "output",
58+
value_name = "OUTPUT_FILE",
59+
help = "Where to write the strategy; defaults to stdout."
60+
)]
61+
pub output_path: Option<PathBuf>,
62+
63+
#[arg(
64+
short,
65+
long,
66+
value_enum,
67+
default_value = "input",
68+
help = "The state reordering type."
69+
)]
70+
pub state_ordering: nfa::StateOrdering,
71+
72+
#[arg(
73+
long,
74+
value_enum,
75+
default_value = "strategy",
76+
help = "Solver output specification."
77+
)]
78+
pub solver_output: solver::SolverOutput,
79+
}

src/main.rs

Lines changed: 6 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use clap::{Parser, ValueEnum};
1+
use clap::Parser;
22
use std::fs::File;
33
use std::io;
44
use std::io::Write;
5-
use std::path::PathBuf;
65
mod coef;
76
mod downset;
87
mod flow;
@@ -17,86 +16,13 @@ mod solver;
1716
mod strategy;
1817
use log::info;
1918
mod logging;
19+
mod cli;
2020

2121

22-
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
23-
enum OutputFormat {
24-
Plain,
25-
Tex,
26-
Csv,
27-
}
28-
29-
#[derive(clap::Parser, Debug)]
30-
#[command(version, about, long_about = None)]
31-
struct Args {
32-
#[arg(value_name = "AUTOMATON_FILE", help = "path to the input")]
33-
filename: String,
34-
35-
#[arg(
36-
short = 'f',
37-
long = "from",
38-
value_enum,
39-
default_value = "tikz",
40-
help = "The input format"
41-
)]
42-
input_format: nfa::InputFormat,
43-
44-
#[arg(
45-
short = 'v',
46-
long = "verbose",
47-
action = clap::ArgAction::Count,
48-
help = "Increase verbosity level (use multiple times for more verbose output)"
49-
)]
50-
verbosity: u8,
51-
52-
#[arg(
53-
long,
54-
short = 'l',
55-
value_name = "LOG_FILE",
56-
help = "Optional path to the log file. Defaults to stdout if not specified."
57-
)]
58-
log_output: Option<PathBuf>,
59-
60-
#[arg(
61-
value_enum,
62-
short = 't',
63-
long = "to",
64-
default_value = "plain",
65-
help = "The output format"
66-
)]
67-
output_format: OutputFormat,
68-
69-
/// path to write the strategy
70-
#[arg(
71-
short = 'o',
72-
long = "output",
73-
value_name = "OUTPUT_FILE",
74-
help = "where to write the strategy; defaults to stdout."
75-
)]
76-
output_path: Option<PathBuf>,
77-
78-
#[arg(
79-
short,
80-
long,
81-
value_enum,
82-
default_value = "input",
83-
help = format!("The state reordering type: preserves input order, sorts alphabetically or topologically.")
84-
)]
85-
state_ordering: nfa::StateOrdering,
86-
87-
#[arg(
88-
long,
89-
value_enum,
90-
default_value = "strategy",
91-
help = format!("The solver output. Either yes/no and a winning strategy (the faster). Or the full maximal winning strategy.")
92-
)]
93-
solver_output: solver::SolverOutput,
94-
}
95-
9622
fn main() {
9723

9824
// parse CLI arguments
99-
let args = Args::parse();
25+
let args = cli::Args::parse();
10026

10127
// set up logging
10228
logging::setup_logger(args.verbosity, args.log_output);
@@ -147,20 +73,20 @@ fn main() {
14773

14874
// prepare output string
14975
let output = match args.output_format {
150-
OutputFormat::Tex => {
76+
cli::OutputFormat::Tex => {
15177
let is_tikz = args.input_format == nfa::InputFormat::Tikz;
15278
let latex_content =
15379
solution.as_latex(if is_tikz { Some(&args.filename) } else { None });
15480
latex_content.to_string()
15581
}
156-
OutputFormat::Plain => {
82+
cli::OutputFormat::Plain => {
15783
format!(
15884
"States: {}\n {}",
15985
nfa.states_str(),
16086
solution.winning_strategy
16187
)
16288
}
163-
OutputFormat::Csv => {
89+
cli::OutputFormat::Csv => {
16490
format!(
16591
"Σ, {}\n{}\n",
16692
nfa.states().join(","),

0 commit comments

Comments
 (0)