Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use clap mangen to generate manpage #257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
18 changes: 18 additions & 0 deletions gping/build.rs
Original file line number Diff line number Diff line change
@@ -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<u8> = Default::default();
man.render(&mut buffer)?;

std::fs::write(out_dir.join("mybin.1"), buffer)?;

shadow_rs::new()
}
83 changes: 83 additions & 0 deletions gping/src/args.rs
Original file line number Diff line number Diff line change
@@ -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<f32>,
#[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<String>,
#[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<String>,
#[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<String>,
}
87 changes: 4 additions & 83 deletions gping/src/main.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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};
Expand All @@ -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<f32>,
#[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<String>,
#[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<String>,
#[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<String>,
}
use crate::args::Args;


struct App {
data: Vec<PlotData>,
Expand Down