SeaBattle/rust/sea_battle_cli_player/src/cli_args.rs
Pierre Hubert e88d64ff63
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Bump to version 0.1.0 -> 0.2.0
2022-10-18 09:39:54 +02:00

75 lines
1.5 KiB
Rust

use clap::{Parser, ValueEnum};
#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum TestDevScreen {
Popup,
Input,
Confirm,
SelectBotType,
SelectPlayMode,
SetBoatsLayout,
ConfigureGameRules,
}
#[derive(Parser, Debug)]
pub struct CliArgs {
/// Upstream server to use
#[clap(
short,
long,
value_parser,
default_value = "https://seabattleapi.communiquons.org"
)]
pub remote_server: String,
/// Local server listen address
#[clap(short, long, default_value = "127.0.0.1:5679")]
pub listen_address: String,
#[clap(long, value_enum)]
pub dev_screen: Option<TestDevScreen>,
/// Run as server instead of as client
#[clap(long, short)]
pub serve: bool,
}
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())
}
}
lazy_static::lazy_static! {
static ref ARGS: CliArgs = {
CliArgs::parse()
};
}
/// Get parsed command line arguments
pub fn cli_args() -> &'static CliArgs {
&ARGS
}
#[cfg(test)]
mod test {
use crate::cli_args::CliArgs;
#[test]
fn verify_cli() {
use clap::CommandFactory;
CliArgs::command().debug_assert()
}
}