~starkingdoms/starkingdoms

ref: e2c74ff0a4af609afb2869c59835838dd76f639c starkingdoms/crates/unified/src/cli.rs -rw-r--r-- 3.7 KiB
e2c74ff0 — core Revert "aaa ??? ?? ? ?? ???????????????????????????????????????????? i would like to explosion" 9 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
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] <subcommand> [<args>...]

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 <URL>      WebSocket URL to connect to
        ");
    }
    if cfg!(feature = "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 = "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}");
}