~starkingdoms/starkingdoms

ref: 8d2bec1c927f2adeffd7fdf0b14c7d29db41df23 starkingdoms/crates/unified/src/cli.rs -rw-r--r-- 2.8 KiB
8d2bec1cghostly_zsh netcode: rotation interpolation 4 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
#[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 {
    Client {
        server: String,
    },
    #[cfg(not(target_arch = "wasm32"))]
    Server {
        management_bind: std::net::SocketAddr,
        native_bind: std::net::SocketAddr,
        wt_bind: std::net::SocketAddr,
        with_client: bool
    },
}

#[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"]) {
        println!("{}", env!("CARGO_PKG_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() {
        "client" => {
            StkArgs::Client {
                server: pargs.value_from_str(["-s", "--server"]).unwrap(),
            }
        },
        #[cfg(not(target_arch = "wasm32"))]
        "server" => {
            StkArgs::Server {
                management_bind: pargs.value_from_str("--management-bind").unwrap(),
                native_bind: pargs.value_from_str("--native-bind").unwrap(),
                wt_bind: pargs.value_from_str("--wt-bind").unwrap(),
                with_client: pargs.contains("--with-client"),
            }
        },
        unknown => {
            eprintln!("unknown subcommand: {unknown} (is that feature supported on this platform?)");
            eprintln!("-h, --help for help");
            std::process::exit(1);
        }
    }
}

fn print_help() {
    println!("\
USAGE:
    starkingdoms [FLAGS] <subcommand> [<args>...]

FLAGS:
    -h, --help        Prints help information
    -v, --version     Prints version information

SUBCOMMANDS:
    ");

        println!("    client            Run the client (see CLIENT for options)");

    if cfg!(not(target_arch = "wasm32")) {
        println!("    server            Run the server (see SERVER for options)");
    }

    println!("\n");

        println!("\
CLIENT:
    -s, --server <URL>           Connection string of the server's management port (e.g. http://127.0.0.1:5150/)
        ");
    if cfg!(not(target_arch = "wasm32")) {
        print!("\
SERVER:
    --management-bind <IP:PORT>  Socket address to bind the management port to
    --native-bind <IP:PORT>      Socket address to bind the native (UDP) port to
    --wt-bind <IP:PORT>          Socket address to bind the WebTransport port to
    --with-client                Start a client connected to this server
    \n");
    }
}