|
4 | 4 | *
|
5 | 5 | */
|
6 | 6 |
|
7 |
| -use clap::{App, Arg}; |
| 7 | +use clap::Parser; |
8 | 8 | use std::num::ParseIntError;
|
9 | 9 | use tls_parser::TlsCipherSuite;
|
10 | 10 |
|
| 11 | +#[derive(Parser)] |
| 12 | +struct CmdOptions { |
| 13 | + /// List all known ciphersuites |
| 14 | + #[arg(short = 'L', long)] |
| 15 | + list: bool, |
| 16 | + /// Display details (algorithms, mode, ...) |
| 17 | + #[arg(short, long)] |
| 18 | + long: bool, |
| 19 | + /// Use JSON for output |
| 20 | + #[arg(short = 'j', long)] |
| 21 | + to_json: bool, |
| 22 | + |
| 23 | + /// Ciphersuite IANA identifier (decimal or hexadecimal prefix by 0x) |
| 24 | + #[arg(short, long, value_name = "id")] |
| 25 | + id: Option<String>, |
| 26 | + /// Ciphersuite IANA name |
| 27 | + #[arg(short, long, value_name = "name")] |
| 28 | + name: Option<String>, |
| 29 | +} |
| 30 | + |
11 | 31 | fn parse_u16(s: &str) -> Result<u16, ParseIntError> {
|
12 | 32 | if s.starts_with("0x") {
|
13 | 33 | let s = s.trim_start_matches("0x");
|
@@ -67,37 +87,25 @@ fn find_by_name(name: &str, show_details: bool, to_json: bool) {
|
67 | 87 | }
|
68 | 88 |
|
69 | 89 | fn main() {
|
70 |
| - let matches = App::new("get-ciphersuite-info") |
71 |
| - .arg(Arg::with_name("id").short('i').long("id").takes_value(true)) |
72 |
| - .arg( |
73 |
| - Arg::with_name("name") |
74 |
| - .short('n') |
75 |
| - .long("name") |
76 |
| - .takes_value(true), |
77 |
| - ) |
78 |
| - .arg(Arg::with_name("list").short('L').long("list")) |
79 |
| - .arg(Arg::with_name("json").short('j').long("json")) |
80 |
| - .arg(Arg::with_name("long").short('l').long("long")) |
81 |
| - .get_matches(); |
| 90 | + let options = CmdOptions::parse(); |
82 | 91 |
|
83 |
| - let show_details = matches.is_present("long"); |
84 |
| - let to_json = matches.is_present("json"); |
| 92 | + let show_details = options.long; |
85 | 93 |
|
86 |
| - if matches.is_present("list") { |
| 94 | + if options.list { |
87 | 95 | let mut id_list = tls_parser::CIPHERS.keys().collect::<Vec<_>>();
|
88 | 96 | id_list.sort();
|
89 | 97 | for &id in &id_list {
|
90 | 98 | let cipher = TlsCipherSuite::from_id(*id).expect("could not get cipher");
|
91 |
| - print_ciphersuite(cipher, show_details, to_json); |
| 99 | + print_ciphersuite(cipher, show_details, options.to_json); |
92 | 100 | }
|
93 | 101 | return;
|
94 | 102 | }
|
95 | 103 |
|
96 |
| - if let Some(str_id) = matches.value_of("id") { |
97 |
| - let id = parse_u16(str_id).expect("Could not parse cipher ID"); |
98 |
| - find_by_id(id, show_details, to_json); |
99 |
| - } else if let Some(name) = matches.value_of("name") { |
100 |
| - find_by_name(name, show_details, to_json); |
| 104 | + if let Some(str_id) = options.id { |
| 105 | + let id = parse_u16(&str_id).expect("Could not parse cipher ID"); |
| 106 | + find_by_id(id, show_details, options.to_json); |
| 107 | + } else if let Some(name) = options.name { |
| 108 | + find_by_name(&name, show_details, options.to_json); |
101 | 109 | } else {
|
102 | 110 | eprintln!("Missing command");
|
103 | 111 | }
|
|
0 commit comments