fix(commands): Require an argument name before the list of tracing filters when used without a subcommand (#7056)

* require flag for tracing filters when subcmd is omitted.

* Fixes test, updates Changelog

* Adds code comment

* Apply suggestions from code review

Co-authored-by: teor <teor@riseup.net>

---------

Co-authored-by: teor <teor@riseup.net>
This commit is contained in:
Arya 2023-07-05 05:28:05 -04:00 committed by GitHub
parent 147b8fa3a8
commit 7e7ce2ba71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 20 deletions

View File

@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org).
## [Zebra 1.0.1](https://github.com/ZcashFoundation/zebra/releases/tag/v1.0.1) - 2023-XX-XX
### Breaking Changes
- Zebra now detects subcommand name typos on the command-line. If you want to give Zebra a list of tracing filters, use `zebrad start --filters debug,...` ([#7056](https://github.com/ZcashFoundation/zebra/pull/7056))
## [Zebra 1.0.1](https://github.com/ZcashFoundation/zebra/releases/tag/v1.0.1) - 2023-07-03
Zebra's first patch release fixes multiple peer connection security issues and panics. It also significantly reduces Zebra's CPU usage. We recommend that all users upgrade to Zebra 1.0.1 or later.

View File

@ -1,7 +1,5 @@
//! Zebrad EntryPoint
use std::cmp::min;
use abscissa_core::{Command, Configurable, FrameworkError, Runnable};
use clap::Parser;
use std::{ffi::OsString, path::PathBuf};
@ -42,7 +40,7 @@ pub struct EntryPoint {
/// Filter strings which override the config file and defaults
// This can be applied to the default start command if no subcommand is provided.
#[clap(help = "tracing filters which override the zebrad.toml config")]
#[clap(long, help = "tracing filters which override the zebrad.toml config")]
filters: Vec<String>,
}
@ -63,28 +61,23 @@ impl EntryPoint {
"start"
}
/// Checks if the provided arguments include a subcommand
fn should_add_default_subcommand(&self) -> bool {
self.cmd.is_none()
}
/// Process command arguments and insert the default subcommand
/// if no subcommand is provided.
pub fn process_cli_args(mut args: Vec<OsString>) -> clap::error::Result<Vec<OsString>> {
// Check if the provided arguments include a subcommand
let should_add_default_subcommand = EntryPoint::try_parse_from(&args)?.cmd.is_none();
let entry_point = EntryPoint::try_parse_from(&args)?;
// Add the default subcommand to args after the top-level args if cmd is None
if should_add_default_subcommand {
// try_parse_from currently produces an error if the first argument is not the binary name,
let mut num_top_level_args = 1;
// update last_top_level_arg_idx to the number of top-level args
for (idx, arg) in args.iter().enumerate() {
num_top_level_args = match arg.to_str() {
Some("--verbose" | "-v" | "--version" | "-V" | "--help") => idx + 1,
Some("--config" | "-c") => idx + 2,
_ => num_top_level_args,
}
if entry_point.should_add_default_subcommand() {
args.push(EntryPoint::default_cmd_as_str().into());
// This duplicates the top-level filters args, but the tracing component only checks `StartCmd.filters`.
for filter in entry_point.filters {
args.push(filter.into())
}
num_top_level_args = min(num_top_level_args, args.len());
args.insert(num_top_level_args, EntryPoint::default_cmd_as_str().into());
}
Ok(args)

View File

@ -16,7 +16,8 @@ fn args_with_subcommand_pass_through() {
(true, false, false, vec!["zebrad", "--help"]),
(false, true, false, vec!["zebrad", "start"]),
(false, true, true, vec!["zebrad", "-v", "start"]),
(false, true, false, vec!["zebrad", "warn"]),
(false, true, false, vec!["zebrad", "--filters", "warn"]),
(true, false, false, vec!["zebrad", "warn"]),
(false, true, false, vec!["zebrad", "start", "warn"]),
(true, false, false, vec!["zebrad", "help", "warn"]),
];