Skip to content

Commit

Permalink
Upgrade to clap 4.1 and convert to clap-derive
Browse files Browse the repository at this point in the history
  • Loading branch information
chifflier committed Mar 14, 2023
1 parent ef27f5e commit 36a0022
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
52 changes: 30 additions & 22 deletions examples/get-ciphersuite-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// Ciphersuite IANA name
#[arg(short, long, value_name = "name")]
name: Option<String>,
}

fn parse_u16(s: &str) -> Result<u16, ParseIntError> {
if s.starts_with("0x") {
let s = s.trim_start_matches("0x");
Expand Down Expand Up @@ -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::<Vec<_>>();
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");
}
Expand Down

0 comments on commit 36a0022

Please sign in to comment.