use crate::build_meta::built_info::{BUILT_TIME_UTC, HOST, RUSTC_VERSION, TARGET}; use crate::build_meta::version_and_features_line; #[cfg(not(any(feature = "client", feature = "server", feature = "particle_editor")))] compile_error!("You need to enable one or more of client, server, particle_editor features"); #[cfg(not(any(feature = "native", feature = "wasm")))] compile_error!("You need to enable one of native, wasm features"); #[cfg(all(feature = "native", feature = "wasm"))] compile_error!("You cannot enable both native and wasm features"); pub enum StkArgs { #[cfg(feature = "client")] Client { server: String, }, #[cfg(feature = "server")] Server { bind_to: std::net::SocketAddr, max_clients: std::num::NonZeroUsize, tick_rate: f32, #[cfg(feature = "client")] with_client: bool }, #[cfg(feature = "particle_editor")] ParticleEditor } #[cfg(not(target_arch = "wasm32"))] pub fn parse_args() -> StkArgs { let mut pargs = pico_args::Arguments::from_env(); if pargs.contains(["-h", "--help"]) { print_help(); std::process::exit(0); } if pargs.contains(["-v", "--version"]) { print_version(); std::process::exit(0); } let Some(subcommand) = pargs.subcommand().unwrap() else { eprintln!("a subcommand is required"); print_help(); std::process::exit(1); }; match subcommand.as_str() { #[cfg(feature = "client")] "client" => { StkArgs::Client { server: pargs.value_from_str(["-s", "--server"]).unwrap(), } }, #[cfg(feature = "server")] "server" => { StkArgs::Server { bind_to: pargs.value_from_str(["-b", "--bind-to"]).unwrap(), max_clients: pargs.value_from_str(["-c", "--max-clients"]).unwrap(), tick_rate: pargs.value_from_str(["-r", "--tick-rate"]).unwrap(), #[cfg(feature = "client")] with_client: pargs.contains("--with-client"), } }, #[cfg(feature = "particle_editor")] "particle_editor" => { StkArgs::ParticleEditor }, unknown => { eprintln!("unknown subcommand: {unknown} (is that feature enabled?)"); eprintln!("-h, --help for help"); std::process::exit(1); } } } fn print_help() { println!("{}\n", version_and_features_line()); println!("\ USAGE: starkingdoms [FLAGS] [...] FLAGS: -h, --help Prints help information -v, --version Prints version information SUBCOMMANDS: "); if cfg!(feature = "client") { println!(" client Run the client (see CLIENT for options)"); } if cfg!(feature = "server") { println!(" server Run the server (see SERVER for options)"); } if cfg!(feature = "particle_editor") { println!("particle_editor Run the particle editor"); } println!("\n"); if cfg!(feature = "client") { println!("\ CLIENT: -s, --server WebSocket URL to connect to "); } if cfg!(feature = "server") { print!("\ SERVER: -b, --bind-to Socket address to bind to -c, --max-clients Maximum number of clients to accept -r, --tick-rate Tick rate\n"); if cfg!(feature = "client") { println!(" --with-client Start a client connected to this server"); } else { println!(); } } } fn print_version() { println!("{}", version_and_features_line()); println!("built on {BUILT_TIME_UTC} by {RUSTC_VERSION} on {HOST} for {TARGET}"); }