From 36a0022029f104e0b3cc6e9dd18ad3da85b106e0 Mon Sep 17 00:00:00 2001 From: Pierre Chifflier Date: Tue, 14 Mar 2023 11:36:26 +0100 Subject: [PATCH] Upgrade to clap 4.1 and convert to clap-derive --- Cargo.toml | 2 +- examples/get-ciphersuite-info.rs | 52 ++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c8fee52..a055057 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,7 @@ phf = "0.11" rusticata-macros = "4.0" [dev-dependencies] -clap = { version="3.2" } +clap = { version="4.1", features = ["derive"]} hex-literal = "0.3" pretty_assertions = "1.0" diff --git a/examples/get-ciphersuite-info.rs b/examples/get-ciphersuite-info.rs index 66428f6..d70bad7 100644 --- a/examples/get-ciphersuite-info.rs +++ b/examples/get-ciphersuite-info.rs @@ -4,10 +4,30 @@ * */ -use clap::{App, Arg}; +use clap::Parser; use std::num::ParseIntError; use tls_parser::TlsCipherSuite; +#[derive(Parser)] +struct CmdOptions { + /// List all known ciphersuites + #[arg(short = 'L', long)] + list: bool, + /// Display details (algorithms, mode, ...) + #[arg(short, long)] + long: bool, + /// Use JSON for output + #[arg(short = 'j', long)] + to_json: bool, + + /// Ciphersuite IANA identifier (decimal or hexadecimal prefix by 0x) + #[arg(short, long, value_name = "id")] + id: Option, + /// Ciphersuite IANA name + #[arg(short, long, value_name = "name")] + name: Option, +} + fn parse_u16(s: &str) -> Result { if s.starts_with("0x") { let s = s.trim_start_matches("0x"); @@ -67,37 +87,25 @@ fn find_by_name(name: &str, show_details: bool, to_json: bool) { } fn main() { - let matches = App::new("get-ciphersuite-info") - .arg(Arg::with_name("id").short('i').long("id").takes_value(true)) - .arg( - Arg::with_name("name") - .short('n') - .long("name") - .takes_value(true), - ) - .arg(Arg::with_name("list").short('L').long("list")) - .arg(Arg::with_name("json").short('j').long("json")) - .arg(Arg::with_name("long").short('l').long("long")) - .get_matches(); + let options = CmdOptions::parse(); - let show_details = matches.is_present("long"); - let to_json = matches.is_present("json"); + let show_details = options.long; - if matches.is_present("list") { + if options.list { let mut id_list = tls_parser::CIPHERS.keys().collect::>(); id_list.sort(); for &id in &id_list { let cipher = TlsCipherSuite::from_id(*id).expect("could not get cipher"); - print_ciphersuite(cipher, show_details, to_json); + print_ciphersuite(cipher, show_details, options.to_json); } return; } - if let Some(str_id) = matches.value_of("id") { - let id = parse_u16(str_id).expect("Could not parse cipher ID"); - find_by_id(id, show_details, to_json); - } else if let Some(name) = matches.value_of("name") { - find_by_name(name, show_details, to_json); + if let Some(str_id) = options.id { + let id = parse_u16(&str_id).expect("Could not parse cipher ID"); + find_by_id(id, show_details, options.to_json); + } else if let Some(name) = options.name { + find_by_name(&name, show_details, options.to_json); } else { eprintln!("Missing command"); }