| 1 |
// GStreamer |
| 2 |
// |
| 3 |
// Copyright (C) 2015-2023 Sebastian Dröge <[email protected]> |
| 4 |
// |
| 5 |
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0. |
| 6 |
// If a copy of the MPL was not distributed with this file, You can obtain one at |
| 7 |
// <https://mozilla.org/MPL/2.0/>. |
| 8 |
// |
| 9 |
// SPDX-License-Identifier: MPL-2.0 |
| 10 |
|
| 11 |
use std::env; |
| 12 |
|
| 13 |
use crate::{ |
| 14 |
bail, |
| 15 |
error::{Context, Error}, |
| 16 |
}; |
| 17 |
|
| 18 |
/// Parsed command-line arguments. |
| 19 |
#[derive(Debug)] |
| 20 |
pub struct Args { |
| 21 |
pub interfaces: Vec<String>, |
| 22 |
pub verbose: bool, |
| 23 |
pub clock_id: u64, |
| 24 |
pub ttl: u32, |
| 25 |
} |
| 26 |
|
| 27 |
/// Parse the command-line arguments. |
| 28 |
pub fn parse_args() -> Result<Args, Error> { |
| 29 |
let mut interfaces = Vec::new(); |
| 30 |
let mut verbose = false; |
| 31 |
let mut clock_id = 0; |
| 32 |
let mut ttl = 1; |
| 33 |
|
| 34 |
let mut args = env::args(); |
| 35 |
// Skip executable name |
| 36 |
let _ = args.next(); |
| 37 |
|
| 38 |
while let Some(arg) = args.next() { |
| 39 |
match arg.as_str() { |
| 40 |
"-v" | "--verbose" => { |
| 41 |
verbose = true; |
| 42 |
} |
| 43 |
"-i" | "--interface" => { |
| 44 |
let iface = args.next().context("No interface following -i")?; |
| 45 |
interfaces.push(iface); |
| 46 |
} |
| 47 |
"-c" | "--clock-id" => { |
| 48 |
let clock_id_arg = args.next().context("No clock-id following -c")?; |
| 49 |
if !clock_id_arg.starts_with("0x") && !clock_id_arg.starts_with("0X") { |
| 50 |
bail!("Clock ID not starting with 0x"); |
| 51 |
} |
| 52 |
clock_id = |
| 53 |
u64::from_str_radix(&clock_id_arg[2..], 16).context("Invalid clock ID")?; |
| 54 |
} |
| 55 |
"--ttl" => { |
| 56 |
let ttl_arg = args.next().context("No TTL following --ttl")?; |
| 57 |
ttl = ttl_arg.parse::<u32>().context("Invalid TTL value")?; |
| 58 |
} |
| 59 |
|
| 60 |
arg => { |
| 61 |
bail!("Unknown command-line argument {}", arg); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
let args = Args { |
| 67 |
interfaces, |
| 68 |
verbose, |
| 69 |
clock_id, |
| 70 |
ttl, |
| 71 |
}; |
| 72 |
|
| 73 |
info!("Running with arguments {:#?}", args); |
| 74 |
|
| 75 |
Ok(args) |
| 76 |
} |