Skip to content

Commit

Permalink
feat: improve command line bool flag handling (#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Oct 6, 2023
1 parent 72b848d commit 359cc83
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl TryFrom<(Args, u16)> for TrippyConfig {
cfg_file_tui.tui_max_samples,
constants::DEFAULT_TUI_MAX_SAMPLES,
);
let tui_preserve_screen = cfg_layer(
let tui_preserve_screen = cfg_layer_bool_flag(
args.tui_preserve_screen,
cfg_file_tui.tui_preserve_screen,
constants::DEFAULT_TUI_PRESERVE_SCREEN,
Expand Down Expand Up @@ -381,7 +381,7 @@ impl TryFrom<(Args, u16)> for TrippyConfig {
cfg_file_dns.dns_resolve_method,
constants::DEFAULT_DNS_RESOLVE_METHOD,
);
let dns_lookup_as_info = cfg_layer(
let dns_lookup_as_info = cfg_layer_bool_flag(
args.dns_lookup_as_info,
cfg_file_dns.dns_lookup_as_info,
constants::DEFAULT_DNS_LOOKUP_AS_INFO,
Expand Down Expand Up @@ -555,6 +555,14 @@ fn cfg_layer_opt<T>(fst: Option<T>, snd: Option<T>) -> Option<T> {
}
}

fn cfg_layer_bool_flag(fst: bool, snd: Option<bool>, default: bool) -> bool {
match (fst, snd) {
(true, _) => true,
(false, Some(val)) => val,
(false, None) => default,
}
}

fn validate_logging(mode: Mode, verbose: bool) -> anyhow::Result<()> {
if matches!(mode, Mode::Tui) && verbose {
Err(anyhow!("cannot enable verbose logging in tui mode"))
Expand Down
4 changes: 2 additions & 2 deletions src/config/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub struct Args {

/// Lookup autonomous system (AS) information during DNS queries [default: false]
#[arg(long, short = 'z', display_order = 25)]
pub dns_lookup_as_info: Option<bool>,
pub dns_lookup_as_info: bool,

/// How to render addresses [default: host]
#[arg(value_enum, short = 'a', long, display_order = 26)]
Expand All @@ -153,7 +153,7 @@ pub struct Args {

/// Preserve the screen on exit [default: false]
#[arg(long, display_order = 31)]
pub tui_preserve_screen: Option<bool>,
pub tui_preserve_screen: bool,

/// The Tui refresh rate [default: 100ms]
#[arg(long, display_order = 32)]
Expand Down

0 comments on commit 359cc83

Please sign in to comment.