diff --git a/README.md b/README.md index 9ab2de8..f83976f 100644 --- a/README.md +++ b/README.md @@ -315,7 +315,9 @@ Options: MRT output file path -f, --fields - Comma-separated list of fields to output. Available fields: type, timestamp, peer_ip, peer_asn, prefix, as_path, origin, next_hop, local_pref, med, communities, atomic, aggr_asn, aggr_ip, collector + Comma-separated list of fields to output. Available fields: type, timestamp, peer_ip, peer_asn, prefix, path_id, as_path, origin_asns, origin, next_hop, local_pref, med, communities, atomic, aggr_asn, aggr_ip, only-to-customer, collector + + [default: fields as listed, except path_id, origin_asns, only-to-customer, collector; JSON: all fields but collector] --format Output format: table, markdown, json, json-pretty, json-line, psv (default varies by command) @@ -652,7 +654,9 @@ Options: Disable automatic database updates (use existing cached data only) -f, --fields - Comma-separated list of fields to output. Available fields: type, timestamp, peer_ip, peer_asn, prefix, as_path, origin, next_hop, local_pref, med, communities, atomic, aggr_asn, aggr_ip, collector + Comma-separated list of fields to output. Available fields: type, timestamp, peer_ip, peer_asn, prefix, path_id, as_path, origin_asns, origin, next_hop, local_pref, med, communities, atomic, aggr_asn, aggr_ip, only-to-customer, collector + + [default: fields as listed, except path_id, origin_asns, only-to-customer] --order-by Order output by field (enables buffering) diff --git a/src/bin/commands/elem_format.rs b/src/bin/commands/elem_format.rs index 1ed7ae3..bbb504b 100644 --- a/src/bin/commands/elem_format.rs +++ b/src/bin/commands/elem_format.rs @@ -94,14 +94,51 @@ pub fn format_elems_table( table.to_string() } -/// Generate help text for the fields argument -pub fn available_fields_help() -> String { +/// Generate help text for the fields argument. +/// +/// `defaults` is the field set used when the option is omitted. It is rendered +/// as a delta against `AVAILABLE_FIELDS` because clap does not wrap help text +/// (the `wrap_help` feature is off), so repeating the whole default list would +/// put a several-hundred-character line into `--help`. +/// +/// `native_json` marks commands whose JSON output emits the native element +/// fields instead of the projection when the default set is selected (see +/// `build_json_object`). +pub fn available_fields_help(defaults: &[&str], native_json: bool) -> String { + let json_note = if native_json { + "; JSON: all fields but collector" + } else { + "" + }; format!( - "Comma-separated list of fields to output. Available fields: {}", - AVAILABLE_FIELDS.join(", ") + "Comma-separated list of fields to output. Available fields: {}\n\n[default: {}{}]", + AVAILABLE_FIELDS.join(", "), + default_fields_summary(defaults), + json_note ) } +/// Summarize a default field set as "fields as listed, except ..." +/// when naming the omissions is shorter than listing the defaults themselves. +fn default_fields_summary(defaults: &[&str]) -> String { + if defaults.is_empty() { + return "none".to_string(); + } + let missing: Vec<&str> = AVAILABLE_FIELDS + .iter() + .copied() + .filter(|field| !defaults.contains(field)) + .collect(); + let defaults_are_available = defaults + .iter() + .all(|field| AVAILABLE_FIELDS.contains(field)); + if defaults_are_available && !missing.is_empty() && missing.len() < defaults.len() { + format!("fields as listed, except {}", missing.join(", ")) + } else { + defaults.join(", ") + } +} + /// Parse and validate the fields argument /// Returns the validated fields. If fields_arg is None, returns the appropriate defaults. /// `include_collector_default` should be true for search command, false for parse command. @@ -636,4 +673,58 @@ mod tests { vec!["only-to-customer"] ); } + + #[test] + fn test_fields_help_states_default() { + let parse_help = available_fields_help(DEFAULT_FIELDS_PARSE, true); + let search_help = available_fields_help(DEFAULT_FIELDS_SEARCH, false); + + // The default uses clap's `[default: ...]` style after an empty line. + // JSON output keeps the native element shape for the default field set, + // so parse and watch say so and search does not. + assert!(parse_help.ends_with( + "\n\n[default: fields as listed, except path_id, origin_asns, only-to-customer, collector; JSON: all fields but collector]" + )); + assert!(search_help.ends_with( + "\n\n[default: fields as listed, except path_id, origin_asns, only-to-customer]" + )); + assert_eq!(parse_help.lines().count(), 3); + assert_eq!(search_help.lines().count(), 3); + } + + #[test] + fn test_fields_default_line_stays_short() { + // clap does not wrap help text, so the default is summarized as a delta; + // listing the fields themselves would exceed 250 characters on one line. + for (defaults, native_json) in + [(DEFAULT_FIELDS_PARSE, true), (DEFAULT_FIELDS_SEARCH, false)] + { + let help = available_fields_help(defaults, native_json); + for line in help.lines().skip(1) { + assert!(line.len() < 120, "help line too long: {line}"); + } + } + } + + #[test] + fn test_default_fields_are_available() { + for field in DEFAULT_FIELDS_PARSE + .iter() + .chain(DEFAULT_FIELDS_SEARCH.iter()) + { + assert!( + AVAILABLE_FIELDS.contains(field), + "{field} is not an available field" + ); + } + assert!(DEFAULT_FIELDS_PARSE + .iter() + .all(|field| DEFAULT_FIELDS_SEARCH.contains(field))); + } + + #[test] + fn test_default_fields_summary_falls_back_to_explicit_list() { + assert_eq!(default_fields_summary(&["timestamp"]), "timestamp"); + assert_eq!(default_fields_summary(&[]), "none"); + } } diff --git a/src/bin/commands/parse.rs b/src/bin/commands/parse.rs index 9b23d02..78e6bf3 100644 --- a/src/bin/commands/parse.rs +++ b/src/bin/commands/parse.rs @@ -13,6 +13,7 @@ use monocle::utils::{OrderByField, OrderDirection, OutputFormat, TimestampFormat use super::elem_format::{ available_fields_help, format_elem, format_elems_table, get_header, parse_fields, sort_elems, + DEFAULT_FIELDS_PARSE, }; /// Arguments for the Parse command @@ -37,7 +38,7 @@ pub(crate) struct ParseArgs { pub mrt_type: Option, /// Comma-separated list of fields to output - #[clap(long, short = 'f', value_name = "FIELDS", help = available_fields_help())] + #[clap(long, short = 'f', value_name = "FIELDS", help = available_fields_help(DEFAULT_FIELDS_PARSE, true))] pub fields: Option, /// Order output by field (enables buffering) diff --git a/src/bin/commands/search.rs b/src/bin/commands/search.rs index 8b7d88f..5dc0a0f 100644 --- a/src/bin/commands/search.rs +++ b/src/bin/commands/search.rs @@ -22,6 +22,7 @@ use tracing::{info, warn}; use super::elem_format::{ available_fields_help, format_elem, format_elems_table, get_header, parse_fields, sort_elems, + DEFAULT_FIELDS_SEARCH, }; /// Arguments for the Search command @@ -48,7 +49,7 @@ pub struct SearchArgs { pub broker_files: bool, /// Comma-separated list of fields to output - #[clap(long, short = 'f', value_name = "FIELDS", help = available_fields_help())] + #[clap(long, short = 'f', value_name = "FIELDS", help = available_fields_help(DEFAULT_FIELDS_SEARCH, false))] pub fields: Option, /// Order output by field (enables buffering) diff --git a/src/bin/commands/watch.rs b/src/bin/commands/watch.rs index be81118..8cd7811 100644 --- a/src/bin/commands/watch.rs +++ b/src/bin/commands/watch.rs @@ -24,7 +24,9 @@ use monocle::lens::watch::{ }; use monocle::utils::{OutputFormat, TimestampFormat}; -use super::elem_format::{format_elem, get_header, parse_fields}; +use super::elem_format::{ + available_fields_help, format_elem, get_header, parse_fields, DEFAULT_FIELDS_PARSE, +}; /// Arguments for the Watch command #[derive(Args)] @@ -54,7 +56,7 @@ pub(crate) struct WatchArgs { pub pretty: bool, /// Comma-separated list of fields to output - #[clap(long, short = 'f', value_name = "FIELDS")] + #[clap(long, short = 'f', value_name = "FIELDS", help = available_fields_help(DEFAULT_FIELDS_PARSE, true))] pub fields: Option, /// Timestamp output format (unix or rfc3339)