From 96e6e75812beebdd9c31b20d6e5ac3f30b5f58e2 Mon Sep 17 00:00:00 2001 From: Tom Forbes Date: Tue, 17 Jan 2023 22:21:28 +0000 Subject: [PATCH] Use clap mangen? --- Cargo.lock | 17 +++++++++ gping/Cargo.toml | 2 ++ gping/build.rs | 18 ++++++++++ gping/src/args.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++ gping/src/main.rs | 87 +++-------------------------------------------- 5 files changed, 124 insertions(+), 83 deletions(-) create mode 100644 gping/src/args.rs diff --git a/Cargo.lock b/Cargo.lock index 5516d977e..8bfe1e37b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -117,6 +117,16 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "clap_mangen" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb258c6232b4d728d13d6072656627924c16707aae6267cd5a1ea05abff9a25c" +dependencies = [ + "clap", + "roff", +] + [[package]] name = "codespan-reporting" version = "0.11.1" @@ -296,6 +306,7 @@ dependencies = [ "anyhow", "chrono", "clap", + "clap_mangen", "const_format", "crossterm", "dns-lookup", @@ -677,6 +688,12 @@ version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +[[package]] +name = "roff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316" + [[package]] name = "rustix" version = "0.36.6" diff --git a/gping/Cargo.toml b/gping/Cargo.toml index d93307247..9e0dad1e5 100644 --- a/gping/Cargo.toml +++ b/gping/Cargo.toml @@ -24,3 +24,5 @@ clap = { version = "4.1.1", features = ["derive"] } [build-dependencies] shadow-rs = "0.20.0" +clap = { version = "4.1.1", features = ["derive"] } +clap_mangen = "0.2.7" \ No newline at end of file diff --git a/gping/build.rs b/gping/build.rs index 4a0dfc459..99d907b25 100644 --- a/gping/build.rs +++ b/gping/build.rs @@ -1,3 +1,21 @@ +use clap::{Command, Args, FromArgMatches as _, Parser, Subcommand as _}; + +#[path = "src/args.rs"] +mod args; + + fn main() -> shadow_rs::SdResult<()> { + let out_dir = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); + + let cli = clap::Command::new("Built CLI"); + // Augment with derived subcommands + let cli = crate::args::Args::augment_args(cli); + + let man = clap_mangen::Man::new(cli); + let mut buffer: Vec = Default::default(); + man.render(&mut buffer)?; + + std::fs::write(out_dir.join("mybin.1"), buffer)?; + shadow_rs::new() } diff --git a/gping/src/args.rs b/gping/src/args.rs new file mode 100644 index 000000000..c4540d980 --- /dev/null +++ b/gping/src/args.rs @@ -0,0 +1,83 @@ +use clap::Parser; +// use shadow_rs::{formatcp, shadow}; +// +// shadow!(build); +// +// pub const VERSION_INFO: &str = formatcp!( +// r#"{} +// commit_hash: {} +// build_time: {} +// build_env: {},{}"#, +// build::PKG_VERSION, +// build::SHORT_COMMIT, +// build::BUILD_TIME, +// build::RUST_VERSION, +// build::RUST_CHANNEL +// ); + +#[derive(Parser, Debug)] +#[command(author, name = "gping", about = "Ping, but with a graph.")] +pub struct Args { + #[arg( + long, + help = "Graph the execution time for a list of commands rather than pinging hosts" + )] + pub cmd: bool, + #[arg( + short = 'n', + long, + help = "Watch interval seconds (provide partial seconds like '0.5'). Default for ping is 0.2, default for cmd is 0.5." + )] + pub watch_interval: Option, + #[arg( + help = "Hosts or IPs to ping, or commands to run if --cmd is provided. Can use cloud shorthands like aws:eu-west-1." + )] + pub hosts_or_commands: Vec, + #[arg( + short, + long, + default_value = "30", + help = "Determines the number of seconds to display in the graph." + )] + pub buffer: u64, + /// Resolve ping targets to IPv4 address + #[arg(short = '4', conflicts_with = "ipv6")] + pub ipv4: bool, + /// Resolve ping targets to IPv6 address + #[arg(short = '6', conflicts_with = "ipv4")] + pub ipv6: bool, + /// Interface to use when pinging. + #[arg(short = 'i', long)] + pub interface: Option, + #[arg(short = 's', long, help = "Uses dot characters instead of braille")] + pub simple_graphics: bool, + #[arg( + long, + help = "Vertical margin around the graph (top and bottom)", + default_value = "1" + )] + pub vertical_margin: u16, + #[arg( + long, + help = "Horizontal margin around the graph (left and right)", + default_value = "0" + )] + pub horizontal_margin: u16, + #[arg( + name = "color", + short = 'c', + long = "color", + use_value_delimiter = true, + value_delimiter = ',', + help = "\ + Assign color to a graph entry. This option can be defined more than \ + once as a comma separated string, and the order which the colors are \ + provided will be matched against the hosts or commands passed to gping. \ + Hexadecimal RGB color codes are accepted in the form of '#RRGGBB' or the \ + following color names: 'black', 'red', 'green', 'yellow', 'blue', 'magenta',\ + 'cyan', 'gray', 'dark-gray', 'light-red', 'light-green', 'light-yellow', \ + 'light-blue', 'light-magenta', 'light-cyan', and 'white'\ + " + )] + pub color_codes_or_names: Vec, +} \ No newline at end of file diff --git a/gping/src/main.rs b/gping/src/main.rs index f3ad1538a..943ac263b 100644 --- a/gping/src/main.rs +++ b/gping/src/main.rs @@ -1,7 +1,6 @@ use crate::plot_data::PlotData; use anyhow::{anyhow, Result}; use chrono::prelude::*; -use clap::Parser; use crossterm::event::{KeyEvent, KeyModifiers}; use crossterm::{ event::{self, Event as CEvent, KeyCode}, @@ -21,6 +20,7 @@ use std::sync::{mpsc, Arc}; use std::thread; use std::thread::JoinHandle; use std::time::{Duration, Instant}; +use clap::Parser; use tui::backend::{Backend, CrosstermBackend}; use tui::layout::{Constraint, Direction, Layout}; use tui::style::{Color, Style}; @@ -31,90 +31,11 @@ use tui::Terminal; mod colors; mod plot_data; mod region_map; +mod args; use colors::Colors; -use shadow_rs::{formatcp, shadow}; - -shadow!(build); - -const VERSION_INFO: &str = formatcp!( - r#"{} -commit_hash: {} -build_time: {} -build_env: {},{}"#, - build::PKG_VERSION, - build::SHORT_COMMIT, - build::BUILD_TIME, - build::RUST_VERSION, - build::RUST_CHANNEL -); - -#[derive(Parser, Debug)] -#[command(author, name = "gping", about = "Ping, but with a graph.", version = VERSION_INFO)] -struct Args { - #[arg( - long, - help = "Graph the execution time for a list of commands rather than pinging hosts" - )] - cmd: bool, - #[arg( - short = 'n', - long, - help = "Watch interval seconds (provide partial seconds like '0.5'). Default for ping is 0.2, default for cmd is 0.5." - )] - watch_interval: Option, - #[arg( - help = "Hosts or IPs to ping, or commands to run if --cmd is provided. Can use cloud shorthands like aws:eu-west-1." - )] - hosts_or_commands: Vec, - #[arg( - short, - long, - default_value = "30", - help = "Determines the number of seconds to display in the graph." - )] - buffer: u64, - /// Resolve ping targets to IPv4 address - #[arg(short = '4', conflicts_with = "ipv6")] - ipv4: bool, - /// Resolve ping targets to IPv6 address - #[arg(short = '6', conflicts_with = "ipv4")] - ipv6: bool, - /// Interface to use when pinging. - #[arg(short = 'i', long)] - interface: Option, - #[arg(short = 's', long, help = "Uses dot characters instead of braille")] - simple_graphics: bool, - #[arg( - long, - help = "Vertical margin around the graph (top and bottom)", - default_value = "1" - )] - vertical_margin: u16, - #[arg( - long, - help = "Horizontal margin around the graph (left and right)", - default_value = "0" - )] - horizontal_margin: u16, - #[arg( - name = "color", - short = 'c', - long = "color", - use_value_delimiter = true, - value_delimiter = ',', - help = "\ - Assign color to a graph entry. This option can be defined more than \ - once as a comma separated string, and the order which the colors are \ - provided will be matched against the hosts or commands passed to gping. \ - Hexadecimal RGB color codes are accepted in the form of '#RRGGBB' or the \ - following color names: 'black', 'red', 'green', 'yellow', 'blue', 'magenta',\ - 'cyan', 'gray', 'dark-gray', 'light-red', 'light-green', 'light-yellow', \ - 'light-blue', 'light-magenta', 'light-cyan', and 'white'\ - " - )] - color_codes_or_names: Vec, -} +use crate::args::Args; + struct App { data: Vec,