~starkingdoms/starkingdoms

ref: 302ee06fb6c522c2bf5d6908329d24a72dffa671 starkingdoms/crates/unified/src/cli.rs -rw-r--r-- 2.3 KiB
302ee06f — core netcode: fix up PlanetDto and try to figure out flickering 6 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
#[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 {
        bind_to: 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 {
                bind_to: pargs.value_from_str(["-b", "--bind-to"]).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>      WebSocket URL to connect to
        ");
    if cfg!(not(target_arch = "wasm32")) {
        print!("\
SERVER:
    -b, --bind-to <IP:PORT> Socket address to bind to
        --with-client       Start a client connected to this server
    \n");
    }
}