Skip to content

Commit a49fadb

Browse files
committed
refactor(complete): Pull out subcommand separator
1 parent ddc008b commit a49fadb

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

clap_complete/src/aot/shells/bash.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ fn all_subcommands(cmd: &Command, parent_fn_name: &str) -> String {
9696
subcmds: &mut Vec<(String, String, String)>,
9797
) {
9898
let fn_name = format!(
99-
"{parent_fn_name}__{cmd_name}",
99+
"{parent_fn_name}{CMD_SEP}{cmd_name}",
100100
parent_fn_name = parent_fn_name,
101-
cmd_name = cmd.get_name().to_string().replace('-', "__")
101+
cmd_name = cmd.get_name().to_string().replace('-', CMD_SEP)
102102
);
103103
subcmds.push((
104104
parent_fn_name.to_string(),
@@ -140,7 +140,7 @@ fn subcommand_details(cmd: &Command) -> String {
140140
let mut subcmd_dets = vec![String::new()];
141141
let mut scs = utils::all_subcommands(cmd)
142142
.iter()
143-
.map(|x| x.1.replace(' ', "__"))
143+
.map(|x| x.1.replace(' ', CMD_SEP))
144144
.collect::<Vec<_>>();
145145

146146
scs.sort();
@@ -162,9 +162,9 @@ fn subcommand_details(cmd: &Command) -> String {
162162
COMPREPLY=( $(compgen -W \"${{opts}}\" -- \"${{cur}}\") )
163163
return 0
164164
;;",
165-
subcmd = sc.replace('-', "__"),
165+
subcmd = sc.replace('-', CMD_SEP),
166166
sc_opts = all_options_for_path(cmd, sc),
167-
level = sc.split("__").map(|_| 1).sum::<u64>(),
167+
level = sc.split(CMD_SEP).map(|_| 1).sum::<u64>(),
168168
opts_details = option_details_for_path(cmd, sc)
169169
)
170170
}));
@@ -175,7 +175,7 @@ fn subcommand_details(cmd: &Command) -> String {
175175
fn option_details_for_path(cmd: &Command, path: &str) -> String {
176176
debug!("option_details_for_path: path={path}");
177177

178-
let p = utils::find_subcommand_with_path(cmd, path.split("__").skip(1).collect());
178+
let p = utils::find_subcommand_with_path(cmd, path.split(CMD_SEP).skip(1).collect());
179179
let mut opts = vec![String::new()];
180180

181181
for o in p.get_opts() {
@@ -280,7 +280,7 @@ fn vals_for(o: &Arg) -> String {
280280
fn all_options_for_path(cmd: &Command, path: &str) -> String {
281281
debug!("all_options_for_path: path={path}");
282282

283-
let p = utils::find_subcommand_with_path(cmd, path.split("__").skip(1).collect());
283+
let p = utils::find_subcommand_with_path(cmd, path.split(CMD_SEP).skip(1).collect());
284284

285285
let mut opts = String::new();
286286
for short in utils::shorts_and_visible_aliases(p) {
@@ -306,3 +306,5 @@ fn all_options_for_path(cmd: &Command, path: &str) -> String {
306306

307307
opts
308308
}
309+
310+
const CMD_SEP: &str = "__";

clap_complete/src/aot/shells/zsh.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ _{bin_name_underscore}_commands() {{
105105
local commands; commands=({subcommands_and_args})
106106
_describe -t commands '{bin_name} commands' commands \"$@\"
107107
}}",
108-
bin_name_underscore = bin_name.replace(' ', "__"),
108+
bin_name_underscore = bin_name.replace(' ', CMD_SEP),
109109
bin_name = bin_name,
110110
subcommands_and_args = subcommands_of(p)
111111
);
@@ -130,7 +130,7 @@ _{bin_name_underscore}_commands() {{
130130
local commands; commands=({subcommands_and_args})
131131
_describe -t commands '{bin_name} commands' commands \"$@\"
132132
}}",
133-
bin_name_underscore = bin_name.replace(' ', "__"),
133+
bin_name_underscore = bin_name.replace(' ', CMD_SEP),
134134
bin_name = bin_name,
135135
subcommands_and_args =
136136
subcommands_of(parser_of(p, bin_name).expect(INTERNAL_ERROR_MSG))
@@ -347,7 +347,7 @@ fn get_args_of(parent: &Command, p_global: Option<&Command>) -> String {
347347
.expect("crate::generate should have set the bin_name");
348348
let subcommand_bin_name = format!(
349349
"\":: :_{name}_commands\" \\",
350-
name = parent_bin_name.replace(' ', "__")
350+
name = parent_bin_name.replace(' ', CMD_SEP)
351351
);
352352
segments.push(subcommand_bin_name);
353353

@@ -678,6 +678,8 @@ fn write_positionals_of(p: &Command) -> String {
678678
ret.join("\n")
679679
}
680680

681+
const CMD_SEP: &str = "__";
682+
681683
#[cfg(test)]
682684
mod tests {
683685
use super::{escape_help, escape_value};

0 commit comments

Comments
 (0)