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 = "target_client", feature = "target_server", feature = "target_particle_editor")))] compile_error!("You need to enable one or more of client, server, particle_editor features"); #[cfg(not(any(feature = "platform_native", feature = "platform_wasm")))] compile_error!("You need to enable one of platform_native, platform_wasm features"); #[cfg(all(feature = "platform_native", feature = "platform_wasm"))] compile_error!("You cannot enable both platform_native and platform_wasm features"); #[cfg(all(feature = "target_server", not(feature = "target_net_server"), not(feature = "target_client")))] compile_error!("You have disabled server networking but are not including client code. This binary will do nothing"); pub enum StkArgs { #[cfg(feature = "target_client")] Client { #[cfg(feature = "target_net_client")] server: String, }, #[cfg(feature = "target_server")] Server { #[cfg(feature = "target_net_server")] bind_to: std::net::SocketAddr, max_clients: std::num::NonZeroUsize, tick_rate: f32, #[cfg(feature = "target_client")] with_client: bool }, #[cfg(feature = "target_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 = "target_client")] "client" => { StkArgs::Client { #[cfg(feature = "target_net_client")] server: pargs.value_from_str(["-s", "--server"]).unwrap(), } }, #[cfg(feature = "target_server")] "server" => { StkArgs::Server { #[cfg(feature = "target_net_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 = "target_client")] with_client: pargs.contains("--with-client"), } }, #[cfg(feature = "target_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 = "target_client") { println!(" client Run the client (see CLIENT for options)"); } if cfg!(feature = "target_server") { println!(" server Run the server (see SERVER for options)"); } if cfg!(feature = "target_particle_editor") { println!("particle_editor Run the particle editor"); } println!("\n"); if cfg!(feature = "target_client") { println!("\ CLIENT: -s, --server WebSocket URL to connect to "); } if cfg!(feature = "target_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 = "target_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}"); }