2022-02-08 04:50:39 +00:00
|
|
|
use std::ops::RangeInclusive;
|
|
|
|
|
2022-03-14 14:31:40 +00:00
|
|
|
use clap::Parser;
|
2021-11-30 18:30:19 +00:00
|
|
|
|
|
|
|
#[derive(Parser)]
|
2022-09-02 20:37:23 +00:00
|
|
|
#[command(author, version, about, long_about = None)]
|
2021-11-30 18:30:19 +00:00
|
|
|
struct Cli {
|
|
|
|
/// Network port to use
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(value_parser = port_in_range)]
|
2022-05-21 00:52:04 +00:00
|
|
|
port: u16,
|
2021-11-30 18:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let cli = Cli::parse();
|
|
|
|
|
|
|
|
println!("PORT = {}", cli.port);
|
|
|
|
}
|
2022-02-08 04:50:39 +00:00
|
|
|
|
2022-03-14 14:31:40 +00:00
|
|
|
const PORT_RANGE: RangeInclusive<usize> = 1..=65535;
|
|
|
|
|
2022-05-21 00:52:04 +00:00
|
|
|
fn port_in_range(s: &str) -> Result<u16, String> {
|
2022-03-14 14:31:40 +00:00
|
|
|
let port: usize = s
|
|
|
|
.parse()
|
|
|
|
.map_err(|_| format!("`{}` isn't a port number", s))?;
|
|
|
|
if PORT_RANGE.contains(&port) {
|
2022-05-21 00:52:04 +00:00
|
|
|
Ok(port as u16)
|
2022-03-14 14:31:40 +00:00
|
|
|
} else {
|
|
|
|
Err(format!(
|
2023-01-03 16:22:40 +00:00
|
|
|
"port not in range {}-{}",
|
2022-03-14 14:31:40 +00:00
|
|
|
PORT_RANGE.start(),
|
|
|
|
PORT_RANGE.end()
|
|
|
|
))
|
|
|
|
}
|
2022-02-08 04:50:39 +00:00
|
|
|
}
|