Skip to content

Commit

Permalink
feat: auto alias subcmds with hyphens for underscores (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jun 8, 2024
1 parent aa196b1 commit 2892e55
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,19 @@ impl Command {
fn update_recursively(&mut self, paths: Vec<String>, mut require_tools: IndexSet<String>) {
self.paths.clone_from(&paths);

// auto alias if command name contains `_`
if let Some(name) = self.name.clone() {
let compatible_name = name.replace('_', "-");
if compatible_name != name {
match self.aliases.as_mut() {
Some((aliaes, _)) => aliaes.insert(0, compatible_name),
None => {
self.aliases = Some((vec![compatible_name], Position::default()));
}
}
}
}

// update command_fn
if paths.is_empty() {
if self.share.borrow().fns.contains_key(MAIN_NAME) {
Expand Down
10 changes: 10 additions & 0 deletions tests/compgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,16 @@ _choice_fn() {
snapshot_compgen!(script, [vec!["prog", ""], vec!["prog", "abc", ""]]);
}

#[test]
fn auto_alias_subcommand() {
let script = r###"
# @cmd
cmd_a() { :; }
"###;

snapshot_compgen!(script, [vec!["prog", ""], vec!["prog", "cmd"]]);
}

#[test]
fn multi_char() {
let script = r#"
Expand Down
12 changes: 12 additions & 0 deletions tests/snapshots/integration__compgen__auto_alias_subcommand.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: tests/compgen.rs
expression: data
---
************ COMPGEN `prog ` ************
cmd_a /color:magenta
cmd-a /color:magenta
help /color:magenta

************ COMPGEN `prog cmd` ************
cmd_a /color:magenta
cmd-a /color:magenta
52 changes: 52 additions & 0 deletions tests/snapshots/integration__spec__auto_alias_subcommand.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
source: tests/spec.rs
expression: data
---
************ RUN ************
prog -h

# OUTPUT
command cat >&2 <<-'EOF'
USAGE: prog <COMMAND>

COMMANDS:
cmd_a [aliases: cmd-a]

EOF
exit 0

# RUN_OUTPUT
USAGE: prog <COMMAND>

COMMANDS:
cmd_a [aliases: cmd-a]

************ RUN ************
prog cmd_a

# OUTPUT
argc__args=( prog cmd_a )
argc__fn=cmd_a
argc__positionals=( )
cmd_a

# RUN_OUTPUT
argc__args=([0]="prog" [1]="cmd_a")
argc__fn=cmd_a
argc__positionals=()
cmd_a

************ RUN ************
prog cmd-a

# OUTPUT
argc__args=( prog cmd-a )
argc__fn=cmd_a
argc__positionals=( )
cmd_a

# RUN_OUTPUT
argc__args=([0]="prog" [1]="cmd-a")
argc__fn=cmd_a
argc__positionals=()
cmd_a
16 changes: 16 additions & 0 deletions tests/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,19 @@ cmdb() { :; }
]
);
}

#[test]
fn auto_alias_subcommand() {
let script = r###"
# @cmd
cmd_a() { :; }
"###;
snapshot_multi!(
script,
[
vec!["prog", "-h"],
vec!["prog", "cmd_a"],
vec!["prog", "cmd-a"],
]
);
}

0 comments on commit 2892e55

Please sign in to comment.