trictrac/client_cli/src/main.rs

84 lines
2.1 KiB
Rust
Raw Normal View History

2024-02-09 17:47:34 +01:00
// Application.
pub mod app;
2024-10-16 17:37:38 +02:00
mod game_runner;
2024-02-09 17:47:34 +01:00
use anyhow::Result;
2024-03-11 20:45:36 +01:00
use app::{App, AppArgs};
2024-02-09 17:47:34 +01:00
use std::io;
2024-03-11 20:45:36 +01:00
// see pico-args example at https://github.com/RazrFalcon/pico-args/blob/master/examples/app.rs
const HELP: &str = "\
Trictrac CLI
USAGE:
trictrac-cli [OPTIONS]
FLAGS:
-h, --help Prints help information
OPTIONS:
2024-11-05 18:03:14 +01:00
--seed SEED Sets the random generator seed
--bot STRATEGY_BOT Add a bot player with strategy STRATEGY, a second bot may be added to play against the first : --bot STRATEGY_BOT1,STRATEGY_BOT2
2024-03-11 20:45:36 +01:00
ARGS:
<INPUT>
";
2024-02-09 17:47:34 +01:00
fn main() -> Result<()> {
2025-01-12 16:38:43 +01:00
env_logger::init();
2024-03-11 20:45:36 +01:00
let args = match parse_args() {
Ok(v) => v,
Err(e) => {
eprintln!("Error: {}.", e);
std::process::exit(1);
}
};
// println!("{:#?}", args);
2024-02-09 17:47:34 +01:00
// Create an application.
2024-03-11 20:45:36 +01:00
let mut app = App::new(args);
2024-02-09 17:47:34 +01:00
// Start the main loop.
while !app.should_quit {
2024-02-17 12:55:36 +01:00
println!("whot?>");
2024-02-09 17:47:34 +01:00
let mut input = String::new();
let _bytecount = io::stdin().read_line(&mut input)?;
app.input(input.trim());
}
2025-02-08 08:52:01 +01:00
// display app final state
println!("{}", app.display());
2024-02-09 17:47:34 +01:00
Ok(())
}
2024-03-11 20:45:36 +01:00
fn parse_args() -> Result<AppArgs, pico_args::Error> {
let mut pargs = pico_args::Arguments::from_env();
// Help has a higher priority and should be handled separately.
if pargs.contains(["-h", "--help"]) {
print!("{}", HELP);
std::process::exit(0);
}
let args = AppArgs {
// Parses an optional value that implements `FromStr`.
seed: pargs.opt_value_from_str("--seed")?,
2024-11-05 18:03:14 +01:00
bot: pargs.opt_value_from_str("--bot")?,
2024-03-11 20:45:36 +01:00
// Parses an optional value from `&str` using a specified function.
// width: pargs.opt_value_from_fn("--width", parse_width)?.unwrap_or(10),
};
// It's up to the caller what to do with the remaining arguments.
let remaining = pargs.finish();
if !remaining.is_empty() {
eprintln!("Warning: unused arguments left: {:?}.", remaining);
}
Ok(args)
}
// fn parse_width(s: &str) -> Result<u32, &'static str> {
// s.parse().map_err(|_| "not a number")
// }