netspeed/
cli.rs

1use clap::{App, AppSettings, Arg, ArgMatches};
2use std::env;
3
4pub struct ArgParser {}
5
6const VERSION: &str = env!("CARGO_PKG_VERSION");
7
8impl ArgParser {
9    pub fn parse(args: env::ArgsOs) -> ArgMatches<'static> {
10        App::new("netspeed")
11            .version(VERSION)
12            .about("Measure tcp throughput")
13            .global_setting(AppSettings::ColorAuto)
14            .global_setting(AppSettings::ColoredHelp)
15            .global_setting(AppSettings::VersionlessSubcommands)
16            .arg(
17                Arg::with_name("verbose")
18                    .long("verbose")
19                    .short("v")
20                    .multiple(true)
21                    .global(true)
22                    .help("Logging verbose"),
23            )
24            .arg(
25                Arg::with_name("address")
26                    .long("addr")
27                    .alias("address")
28                    .short("a")
29                    .help("Remote server address")
30                    .takes_value(true)
31                    .default_value("netspeed.ymgyt.io:5555"),
32            )
33            .arg(
34                Arg::with_name("duration")
35                    .long("duration")
36                    .alias("duration-seconds")
37                    .help("Speed test duration seconds(max: 10)")
38                    .takes_value(true)
39                    .default_value("3")
40                    .validator(|s| {
41                        let n = s.parse::<u8>().map_err(|err| format!("{}", err))?;
42                        if n > 10 {
43                            Err("Max duration exceeded (max: 10)".to_owned())
44                        } else {
45                            Ok(())
46                        }
47                    })
48                    .value_name("SECONDS"),
49            )
50            .subcommand(
51                App::new("server")
52                    .about("Server mode")
53                    .arg(
54                        Arg::with_name("run")
55                            .index(1)
56                            .required(true)
57                            .help("Running server"),
58                    )
59                    .arg(
60                        Arg::with_name("address")
61                            .long("addr")
62                            .alias("address")
63                            .short("a")
64                            .help("Listening address")
65                            .takes_value(true)
66                            .default_value("0.0.0.0:5555"),
67                    )
68                    .arg(
69                        Arg::with_name("max-threads")
70                            .long("max-threads")
71                            .alias("max-workers")
72                            .help("Max concurrent threads/workers")
73                            .takes_value(true)
74                            .default_value("100")
75                            .value_name("NUMBER"),
76                    ),
77            )
78            .get_matches_from(args)
79    }
80}