From a65d520e28856503a410df1f328307a4d9498a30 Mon Sep 17 00:00:00 2001 From: Mingwei Zhang Date: Tue, 3 Nov 2026 06:00:20 -0800 Subject: [PATCH 1/3] feat(cli): show the default field set in --fields help `parse`, `search`, and `watch` accept `--fields` but the help never said which fields are printed when the option is omitted. The default is rendered as a delta against the available fields ("all fields except ...") because clap does not wrap help text, so repeating the default list would add a several-hundred-character line to `--help`. `watch --fields` now lists the available fields as well, since it shares the parse defaults. --- README.md | 6 ++- src/bin/commands/elem_format.rs | 83 +++++++++++++++++++++++++++++++-- src/bin/commands/parse.rs | 3 +- src/bin/commands/search.rs | 3 +- src/bin/commands/watch.rs | 6 ++- 5 files changed, 91 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9ab2de8..c85122a 100644 --- a/README.md +++ b/README.md @@ -315,7 +315,8 @@ 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: all fields except path_id, origin_asns, only-to-customer, collector --format Output format: table, markdown, json, json-pretty, json-line, psv (default varies by command) @@ -652,7 +653,8 @@ 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: all fields 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..970bc07 100644 --- a/src/bin/commands/elem_format.rs +++ b/src/bin/commands/elem_format.rs @@ -94,14 +94,41 @@ 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`. +pub fn available_fields_help(defaults: &[&str]) -> String { format!( - "Comma-separated list of fields to output. Available fields: {}", - AVAILABLE_FIELDS.join(", ") + "Comma-separated list of fields to output. Available fields: {}\nDefault: {}", + AVAILABLE_FIELDS.join(", "), + default_fields_summary(defaults) ) } +/// Summarize a default field set as "all fields 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!("all fields 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 +663,52 @@ mod tests { vec!["only-to-customer"] ); } + + #[test] + fn test_fields_help_states_default() { + let parse_help = available_fields_help(DEFAULT_FIELDS_PARSE); + let search_help = available_fields_help(DEFAULT_FIELDS_SEARCH); + + assert!(parse_help.ends_with( + "Default: all fields except path_id, origin_asns, only-to-customer, collector" + )); + assert!(search_help + .ends_with("Default: all fields except path_id, origin_asns, only-to-customer")); + } + + #[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 in [DEFAULT_FIELDS_PARSE, DEFAULT_FIELDS_SEARCH] { + let help = available_fields_help(defaults); + let default_line = help.lines().nth(1).expect("help has a default line"); + assert!( + default_line.len() < 120, + "default line too long: {default_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..9389f12 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))] 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..241ccc3 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))] 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..9828968 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))] pub fields: Option, /// Timestamp output format (unix or rfc3339) From 2df2d3301fc576649a7aa185c94a581b5254da24 Mon Sep 17 00:00:00 2001 From: Mingwei Zhang Date: Tue, 3 Nov 2026 06:06:30 -0800 Subject: [PATCH 2/3] fix(cli): note the JSON native element output in --fields help `parse` and `watch` emit the native element fields for JSON output when the default field set is selected, so the default line alone was inaccurate for those two commands. `search` projects the field list, so its help is unchanged. --- README.md | 1 + src/bin/commands/elem_format.rs | 56 ++++++++++++++++++++++----------- src/bin/commands/parse.rs | 2 +- src/bin/commands/search.rs | 2 +- src/bin/commands/watch.rs | 2 +- 5 files changed, 42 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index c85122a..d90a4ef 100644 --- a/README.md +++ b/README.md @@ -317,6 +317,7 @@ Options: -f, --fields 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: all fields except path_id, origin_asns, only-to-customer, collector + JSON output emits all native element fields when the default set is selected. --format Output format: table, markdown, json, json-pretty, json-line, psv (default varies by command) diff --git a/src/bin/commands/elem_format.rs b/src/bin/commands/elem_format.rs index 970bc07..9028b6e 100644 --- a/src/bin/commands/elem_format.rs +++ b/src/bin/commands/elem_format.rs @@ -100,12 +100,22 @@ pub fn format_elems_table( /// 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`. -pub fn available_fields_help(defaults: &[&str]) -> String { - format!( +/// +/// `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 mut help = format!( "Comma-separated list of fields to output. Available fields: {}\nDefault: {}", AVAILABLE_FIELDS.join(", "), default_fields_summary(defaults) - ) + ); + if native_json { + help.push_str( + "\nJSON output emits all native element fields when the default set is selected.", + ); + } + help } /// Summarize a default field set as "all fields except ..." when naming the @@ -666,27 +676,37 @@ mod tests { #[test] fn test_fields_help_states_default() { - let parse_help = available_fields_help(DEFAULT_FIELDS_PARSE); - let search_help = available_fields_help(DEFAULT_FIELDS_SEARCH); - - assert!(parse_help.ends_with( - "Default: all fields except path_id, origin_asns, only-to-customer, collector" - )); - assert!(search_help - .ends_with("Default: all fields except path_id, origin_asns, only-to-customer")); + let parse_help = available_fields_help(DEFAULT_FIELDS_PARSE, true); + let search_help = available_fields_help(DEFAULT_FIELDS_SEARCH, false); + + assert_eq!( + parse_help.lines().nth(1), + Some("Default: all fields except path_id, origin_asns, only-to-customer, collector") + ); + assert_eq!( + search_help.lines().nth(1), + Some("Default: all fields except path_id, origin_asns, only-to-customer") + ); + // JSON output keeps the native element shape for the default field set, + // so parse and watch say so and search does not. + assert_eq!( + parse_help.lines().nth(2), + Some("JSON output emits all native element fields when the default set is selected.") + ); + assert_eq!(search_help.lines().count(), 2); } #[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 in [DEFAULT_FIELDS_PARSE, DEFAULT_FIELDS_SEARCH] { - let help = available_fields_help(defaults); - let default_line = help.lines().nth(1).expect("help has a default line"); - assert!( - default_line.len() < 120, - "default line too long: {default_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}"); + } } } diff --git a/src/bin/commands/parse.rs b/src/bin/commands/parse.rs index 9389f12..78e6bf3 100644 --- a/src/bin/commands/parse.rs +++ b/src/bin/commands/parse.rs @@ -38,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(DEFAULT_FIELDS_PARSE))] + #[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 241ccc3..5dc0a0f 100644 --- a/src/bin/commands/search.rs +++ b/src/bin/commands/search.rs @@ -49,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(DEFAULT_FIELDS_SEARCH))] + #[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 9828968..8cd7811 100644 --- a/src/bin/commands/watch.rs +++ b/src/bin/commands/watch.rs @@ -56,7 +56,7 @@ pub(crate) struct WatchArgs { pub pretty: bool, /// Comma-separated list of fields to output - #[clap(long, short = 'f', value_name = "FIELDS", help = available_fields_help(DEFAULT_FIELDS_PARSE))] + #[clap(long, short = 'f', value_name = "FIELDS", help = available_fields_help(DEFAULT_FIELDS_PARSE, true))] pub fields: Option, /// Timestamp output format (unix or rfc3339) From 05ef8bae31b439bcf64dcd58534bbf809955d389 Mon Sep 17 00:00:00 2001 From: jmuecke <48099698+jmuecke@users.noreply.github.com> Date: Tue, 3 Nov 2026 17:18:52 +0100 Subject: [PATCH 3/3] Improve presentation of default value for fields We use `fields as listed` to include that the above order is considered while including the different behavior for JSON output and maintaining 120 line length. --- README.md | 7 ++--- src/bin/commands/elem_format.rs | 48 +++++++++++++++------------------ 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index d90a4ef..f83976f 100644 --- a/README.md +++ b/README.md @@ -316,8 +316,8 @@ Options: -f, --fields 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: all fields except path_id, origin_asns, only-to-customer, collector - JSON output emits all native element fields when the default set is selected. + + [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) @@ -655,7 +655,8 @@ Options: -f, --fields 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: all fields except path_id, origin_asns, only-to-customer + + [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 9028b6e..bbb504b 100644 --- a/src/bin/commands/elem_format.rs +++ b/src/bin/commands/elem_format.rs @@ -105,21 +105,21 @@ pub fn format_elems_table( /// 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 mut help = format!( - "Comma-separated list of fields to output. Available fields: {}\nDefault: {}", + let json_note = if native_json { + "; JSON: all fields but collector" + } else { + "" + }; + format!( + "Comma-separated list of fields to output. Available fields: {}\n\n[default: {}{}]", AVAILABLE_FIELDS.join(", "), - default_fields_summary(defaults) - ); - if native_json { - help.push_str( - "\nJSON output emits all native element fields when the default set is selected.", - ); - } - help + default_fields_summary(defaults), + json_note + ) } -/// Summarize a default field set as "all fields except ..." when naming the -/// omissions is shorter than listing the defaults themselves. +/// 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(); @@ -133,7 +133,7 @@ fn default_fields_summary(defaults: &[&str]) -> String { .iter() .all(|field| AVAILABLE_FIELDS.contains(field)); if defaults_are_available && !missing.is_empty() && missing.len() < defaults.len() { - format!("all fields except {}", missing.join(", ")) + format!("fields as listed, except {}", missing.join(", ")) } else { defaults.join(", ") } @@ -679,21 +679,17 @@ mod tests { let parse_help = available_fields_help(DEFAULT_FIELDS_PARSE, true); let search_help = available_fields_help(DEFAULT_FIELDS_SEARCH, false); - assert_eq!( - parse_help.lines().nth(1), - Some("Default: all fields except path_id, origin_asns, only-to-customer, collector") - ); - assert_eq!( - search_help.lines().nth(1), - Some("Default: all fields except path_id, origin_asns, only-to-customer") - ); + // 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_eq!( - parse_help.lines().nth(2), - Some("JSON output emits all native element fields when the default set is selected.") - ); - assert_eq!(search_help.lines().count(), 2); + 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]