Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ Options:
MRT output file path

-f, --fields <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 <FORMAT>
Output format: table, markdown, json, json-pretty, json-line, psv (default varies by command)
Expand Down Expand Up @@ -652,7 +654,9 @@ Options:
Disable automatic database updates (use existing cached data only)

-f, --fields <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_BY>
Order output by field (enables buffering)
Expand Down
99 changes: 95 additions & 4 deletions src/bin/commands/elem_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
}
}
3 changes: 2 additions & 1 deletion src/bin/commands/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,7 +38,7 @@ pub(crate) struct ParseArgs {
pub mrt_type: Option<MrtType>,

/// 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<String>,

/// Order output by field (enables buffering)
Expand Down
3 changes: 2 additions & 1 deletion src/bin/commands/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String>,

/// Order output by field (enables buffering)
Expand Down
6 changes: 4 additions & 2 deletions src/bin/commands/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<String>,

/// Timestamp output format (unix or rfc3339)
Expand Down
Loading