generated from GarandPLG/rust-flake-template
Refactor keybindings and add validated CLI arguments
Extracted common quit/esc handling into `common_keybindings`. Introduced `Group::CtrlMovement` for Ctrl‑arrow keys and updated keybinding definitions. Removed duplicated quit logic in main_menu and skirmish modules. Bumped clap to 4.6.0 and updated Cargo.lock. Added range‑checked parsers for map size, resources, supply limit, XP modifier and skill points in the CLI.
This commit is contained in:
+33
-11
@@ -2,7 +2,8 @@ use crate::app::{
|
||||
states::{GameMode, PerkDecks},
|
||||
view::View,
|
||||
};
|
||||
use clap::Parser;
|
||||
use clap::{Error, Parser, error::ErrorKind, value_parser};
|
||||
use std::num::ParseFloatError;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about = "War in Tunnels", long_about = "War in Tunnels")]
|
||||
@@ -37,7 +38,8 @@ pub struct Cli {
|
||||
long,
|
||||
help = "Map width",
|
||||
value_name = "Positive integer [20; 100]",
|
||||
default_value = "50"
|
||||
default_value = "50",
|
||||
value_parser = value_parser!(u8).range(20..=100)
|
||||
)]
|
||||
pub map_width: u8,
|
||||
|
||||
@@ -45,7 +47,8 @@ pub struct Cli {
|
||||
long,
|
||||
help = "Map height",
|
||||
value_name = "Positive integer [11; 50]",
|
||||
default_value = "25"
|
||||
default_value = "25",
|
||||
value_parser = value_parser!(u8).range(25..=50)
|
||||
)]
|
||||
pub map_height: u8,
|
||||
|
||||
@@ -61,24 +64,27 @@ pub struct Cli {
|
||||
#[arg(
|
||||
long,
|
||||
help = "Starting wood",
|
||||
value_name = "Positive integer",
|
||||
default_value = "50"
|
||||
value_name = "Positive integer [1; 150]",
|
||||
default_value = "50",
|
||||
value_parser = value_parser!(u16).range(1..=150)
|
||||
)]
|
||||
pub starting_wood: u16,
|
||||
|
||||
#[arg(
|
||||
long,
|
||||
help = "Starting iron",
|
||||
value_name = "Positive integer",
|
||||
default_value = "25"
|
||||
value_name = "Positive integer [1; 150]",
|
||||
default_value = "25",
|
||||
value_parser = value_parser!(u16).range(1..=150)
|
||||
)]
|
||||
pub starting_iron: u16,
|
||||
|
||||
#[arg(
|
||||
long,
|
||||
help = "Supply limit",
|
||||
value_name = "Positive integer",
|
||||
default_value = "99"
|
||||
value_name = "Positive integer [49; 149]",
|
||||
default_value = "99",
|
||||
value_parser = value_parser!(u8).range(49..=149)
|
||||
)]
|
||||
pub supply_limit: u8,
|
||||
|
||||
@@ -86,7 +92,8 @@ pub struct Cli {
|
||||
long,
|
||||
help = "XP modifier",
|
||||
value_name = "Float [0.5; 2.0]",
|
||||
default_value = "1.0"
|
||||
default_value = "1.0",
|
||||
value_parser = parse_xp
|
||||
)]
|
||||
pub xp_modifier: f32,
|
||||
|
||||
@@ -94,7 +101,8 @@ pub struct Cli {
|
||||
long,
|
||||
help = "Skill points limit",
|
||||
value_name = "Positive integer [120; 690]",
|
||||
default_value = "120"
|
||||
default_value = "120",
|
||||
value_parser = value_parser!(u16).range(120..=690)
|
||||
)]
|
||||
pub skill_points_limit: u16,
|
||||
|
||||
@@ -109,3 +117,17 @@ pub struct Cli {
|
||||
pub fn get_args() -> Cli {
|
||||
Cli::parse()
|
||||
}
|
||||
|
||||
fn parse_xp(s: &str) -> Result<f32, Error> {
|
||||
let v: f32 = s
|
||||
.parse()
|
||||
.map_err(|e: ParseFloatError| Error::raw(ErrorKind::InvalidValue, e.to_string()))?;
|
||||
if (0.5..=2.0).contains(&v) {
|
||||
Ok(v)
|
||||
} else {
|
||||
Err(Error::raw(
|
||||
ErrorKind::InvalidValue,
|
||||
format!("Value {} is out of range 0.5..=2.0", v),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user