From 37c2fb41201160e85c8dc3ad40f462cd4e17a304 Mon Sep 17 00:00:00 2001 From: floriskn <48930050+floriskn@users.noreply.github.com> Date: Wed, 18 Jun 2025 14:04:03 +0200 Subject: [PATCH] feat(cli): add support for global CLI arguments (#2772) --- .changes/add-global-flag-cli.md | 8 ++++++++ plugins/cli/src/config.rs | 6 ++++++ plugins/cli/src/parser.rs | 2 ++ 3 files changed, 16 insertions(+) create mode 100644 .changes/add-global-flag-cli.md diff --git a/.changes/add-global-flag-cli.md b/.changes/add-global-flag-cli.md new file mode 100644 index 00000000..9fa5a39b --- /dev/null +++ b/.changes/add-global-flag-cli.md @@ -0,0 +1,8 @@ +--- +"cli": minor +"cli-js": minor +--- + +Added a new `global` boolean flag to the `CliArg` struct to support global CLI arguments. This flag allows arguments like `--verbose` to be marked as global and automatically propagated to all subcommands, enabling consistent availability throughout the CLI. + +The new field is optional and defaults to false. diff --git a/plugins/cli/src/config.rs b/plugins/cli/src/config.rs index f7f36a0e..bd4a3015 100644 --- a/plugins/cli/src/config.rs +++ b/plugins/cli/src/config.rs @@ -114,6 +114,12 @@ pub struct Arg { /// It does not define position in the argument list as a whole. When utilized with multiple=true, /// only the last positional argument may be defined as multiple (i.e. the one with the highest index). pub index: Option, + /// Specifies whether the argument should be global. + /// + /// Global arguments are propagated to all subcommands automatically, + /// making them available throughout the CLI regardless of where they are defined. + #[serde(default)] + pub global: bool, } /// describes a CLI configuration diff --git a/plugins/cli/src/parser.rs b/plugins/cli/src/parser.rs index 69a926c1..64ec2fb7 100644 --- a/plugins/cli/src/parser.rs +++ b/plugins/cli/src/parser.rs @@ -278,5 +278,7 @@ fn get_arg(arg_name: String, arg: &Arg) -> ClapArg { clap_arg = bind_value_arg!(arg, clap_arg, require_equals); clap_arg = bind_value_arg!(arg, clap_arg, index); + clap_arg = clap_arg.global(arg.global); + clap_arg }