1use std::ops::RangeInclusive;
2
3use clap::Parser;
4
5#[derive(Parser)]
6#[command(author, version, about, long_about = None)]
7struct Cli {
8 /// Network port to use
9 #[arg(value_parser = port_in_range)]
10 port: u16,
11}
12
13fn main() {
14 let cli = Cli::parse();
15
16 println!("PORT = {}", cli.port);
17}
18
19const PORT_RANGE: RangeInclusive<usize> = 1..=65535;
20
21fn port_in_range(s: &str) -> Result<u16, String> {
22 let port: usize = s
23 .parse()
24 .map_err(|_| format!("`{s}` isn't a port number"))?;
25 if PORT_RANGE.contains(&port) {
26 Ok(port as u16)
27 } else {
28 Err(format!(
29 "port not in range {}-{}",
30 PORT_RANGE.start(),
31 PORT_RANGE.end()
32 ))
33 }
34}
35