~starkingdoms/starkingdoms

ref: 0a56ebc381389eb63ab5cb3c3522d9ea2475e9d8 starkingdoms/crates/unified/src/cli.rs -rw-r--r-- 4.3 KiB
0a56ebc3 — core aaa ??? ?? ? ?? ???????????????????????????????????????????? i would like to explosion 13 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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] <subcommand> [<args>...]

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 <URL>      WebSocket URL to connect to
        ");
    }
    if cfg!(feature = "target_server") {
        print!("\
SERVER:
    -b, --bind-to <IP:PORT> Socket address to bind to
    -c, --max-clients <N>   Maximum number of clients to accept
    -r, --tick-rate   <N>   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}");
}