Skip to content

Commit

Permalink
feat: add complete flag for generating shell completions (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Apr 15, 2023
1 parent f75b6d7 commit 6c3f114
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::config::TuiCommandItem::{
ToggleHelp,
};
use anyhow::anyhow;
use clap::{Parser, ValueEnum};
use clap::{Command, CommandFactory, Parser, ValueEnum};
use clap_complete::{generate, Generator, Shell};
use crossterm::event::{KeyCode, KeyModifiers};
use itertools::Itertools;
use serde::Deserialize;
Expand Down Expand Up @@ -215,7 +216,7 @@ pub enum DnsResolveMethod {

/// Trace a route to a host and record statistics
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
#[command(name = "trip", author, version, about, long_about = None)]
pub struct Args {
/// A space delimited list of hostnames and IPs to trace
#[arg(required = true)]
Expand Down Expand Up @@ -378,6 +379,10 @@ pub struct Args {
/// The number of report cycles to run [default: 10]
#[arg(short = 'C', long, display_order = 36)]
pub report_cycles: Option<usize>,

/// Generate shell completion
#[arg(long, display_order = 37)]
pub generate: Option<Shell>,
}

fn parse_tui_theme_color_value(value: &str) -> anyhow::Result<(TuiThemeItem, TuiColor)> {
Expand Down Expand Up @@ -1303,6 +1308,11 @@ impl TryFrom<(Args, u16)> for TrippyConfig {
);
process::exit(0);
}
if let Some(generator) = args.generate {
let mut cmd = Args::command();
print_completions(generator, &mut cmd);
process::exit(0);
}
let cfg_file = if let Some(cfg) = args.config_file {
config_file::read_config_file(cfg)?
} else if let Some(cfg) = config_file::read_default_config_file()? {
Expand Down Expand Up @@ -1551,6 +1561,10 @@ impl TryFrom<(Args, u16)> for TrippyConfig {
}
}

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
}

fn cfg_layer<T>(fst: Option<T>, snd: Option<T>, def: T) -> T {
match (fst, snd) {
(Some(val), _) | (None, Some(val)) => val,
Expand Down

0 comments on commit 6c3f114

Please # to comment.