SeaBattle/rust/sea_battle_cli_player/src/cli_args.rs

75 lines
1.5 KiB
Rust
Raw Normal View History

2022-10-10 15:17:57 +00:00
use clap::{Parser, ValueEnum};
#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum TestDevScreen {
Popup,
Input,
2022-10-10 15:51:51 +00:00
Confirm,
SelectBotType,
SelectPlayMode,
SetBoatsLayout,
ConfigureGameRules,
2022-10-10 15:17:57 +00:00
}
2022-10-01 17:25:41 +00:00
#[derive(Parser, Debug)]
pub struct CliArgs {
/// Upstream server to use
#[clap(
short,
long,
value_parser,
2022-10-16 15:47:41 +00:00
default_value = "https://seabattleapi.communiquons.org"
2022-10-01 17:25:41 +00:00
)]
2022-10-18 06:58:36 +00:00
pub remote_server: String,
2022-10-01 18:44:01 +00:00
/// Local server listen address
#[clap(short, long, default_value = "127.0.0.1:5679")]
pub listen_address: String,
2022-10-10 15:17:57 +00:00
#[clap(long, value_enum)]
pub dev_screen: Option<TestDevScreen>,
2022-10-16 17:52:34 +00:00
/// Run as server instead of as client
#[clap(long, short)]
pub serve: bool,
2022-10-01 18:44:01 +00:00
}
impl CliArgs {
/// Get local listen port
pub fn listen_port(&self) -> u16 {
self.listen_address
.rsplit(':')
.next()
.expect("Failed to split listen address!")
.parse::<u16>()
.expect("Failed to parse listen port!")
}
/// Get local server address
pub fn local_server_address(&self) -> String {
format!("http://localhost:{}", self.listen_port())
}
2022-10-01 18:44:01 +00:00
}
lazy_static::lazy_static! {
static ref ARGS: CliArgs = {
CliArgs::parse()
};
}
/// Get parsed command line arguments
pub fn cli_args() -> &'static CliArgs {
&ARGS
2022-10-01 17:25:41 +00:00
}
2022-10-18 07:39:54 +00:00
#[cfg(test)]
mod test {
use crate::cli_args::CliArgs;
#[test]
fn verify_cli() {
use clap::CommandFactory;
CliArgs::command().debug_assert()
}
}