mirror of
https://github.com/rustfs/rustfs.git
synced 2026-07-28 00:58:59 +00:00
fix(cli): remove autogenerated help Clap subcommand (#3289)
* Fix(CLI): remove autogenerated `help` Clap subcommand * fix(cli): preserve rustfs help behavior --------- Co-authored-by: cxymds <Cxymds@qq.com> Co-authored-by: houseme <housemecn@gmail.com> Co-authored-by: overtrue <anzhengchao@gmail.com>
This commit is contained in:
@@ -72,6 +72,14 @@ pub fn preprocess_args_for_legacy(args: Vec<String>) -> Vec<String> {
|
||||
if KNOWN_SUBCOMMANDS.contains(&first.as_str()) {
|
||||
return args;
|
||||
}
|
||||
// Preserve the traditional `rustfs help` entry point without exposing
|
||||
// Clap's generated `help` subcommand in the top-level command list.
|
||||
if first == "help" {
|
||||
let mut out = vec![args[0].clone()];
|
||||
out.extend(args[2..].iter().cloned());
|
||||
out.push("--help".to_string());
|
||||
return out;
|
||||
}
|
||||
// If first arg is --info, treat it as info subcommand
|
||||
if first == "--info" {
|
||||
let mut out = vec![args[0].clone(), "info".to_string()];
|
||||
@@ -91,6 +99,7 @@ pub fn preprocess_args_for_legacy(args: Vec<String>) -> Vec<String> {
|
||||
/// Main CLI parser
|
||||
#[derive(Parser, Clone)]
|
||||
#[command(name = "rustfs", version = SHORT_VERSION, long_version = LONG_VERSION)]
|
||||
#[command(disable_help_subcommand = true)]
|
||||
pub struct Cli {
|
||||
#[command(subcommand)]
|
||||
pub command: Option<Commands>,
|
||||
@@ -334,3 +343,23 @@ pub fn default_server_opts() -> ServerOpts {
|
||||
buffer_profile: "GeneralPurpose".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Cli, preprocess_args_for_legacy};
|
||||
use clap::Parser;
|
||||
use clap::error::ErrorKind;
|
||||
|
||||
#[test]
|
||||
fn preprocess_help_command_displays_top_level_help() {
|
||||
let args = preprocess_args_for_legacy(vec!["rustfs".to_string(), "help".to_string()]);
|
||||
|
||||
assert_eq!(args, vec!["rustfs".to_string(), "--help".to_string()]);
|
||||
|
||||
let err = match Cli::try_parse_from(args) {
|
||||
Ok(_) => panic!("rustfs help should display help"),
|
||||
Err(err) => err,
|
||||
};
|
||||
assert_eq!(err.kind(), ErrorKind::DisplayHelp);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user