Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

refactor: Use Shell enum for init command instead of String

Maximilian Roosb5cc7dc3aef4
diff --git a/src/commands/init.rs b/src/commands/init.rs
index bc6eb4601..5bd60aa8b 100644
--- a/src/commands/init.rs
+++ b/src/commands/init.rs
@@ -3,9 +3,11 @@ use clap_complete::{Shell as CompletionShell, generate};
use worktrunk::shell;
use worktrunk::styling::{ERROR, ERROR_EMOJI, println};
-pub fn handle_init(shell_name: &str, cmd_name: &str, cli_cmd: &mut Command) -> Result<(), String> {
- let shell = shell_name.parse::<shell::Shell>()?;
-
+pub fn handle_init(
+ shell: shell::Shell,
+ cmd_name: &str,
+ cli_cmd: &mut Command,
+) -> Result<(), String> {
let init = shell::ShellInit::new(shell, cmd_name.to_string());
// Generate shell integration code
diff --git a/src/main.rs b/src/main.rs
index 0e4b729dd..a9d1db690 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -88,8 +88,8 @@ enum DevCommand {
enum Commands {
/// Generate shell integration code
Init {
- /// Shell to generate code for (bash, fish, zsh)
- shell: String,
+ /// Shell to generate code for
+ shell: Shell,
/// Command prefix (default: wt)
#[arg(long, default_value = "wt")]
@@ -333,7 +333,7 @@ fn main() {
let result = match cli.command {
Commands::Init { shell, cmd } => {
let mut cli_cmd = Cli::command();
- handle_init(&shell, &cmd, &mut cli_cmd).git_err()
+ handle_init(shell, &cmd, &mut cli_cmd).git_err()
}
Commands::ConfigureShell { shell, cmd, yes } => {
handle_configure_shell(shell, &cmd, yes)
diff --git a/tests/integration_tests/init.rs b/tests/integration_tests/init.rs
index b7b2e08c3..1ad104c14 100644
--- a/tests/integration_tests/init.rs
+++ b/tests/integration_tests/init.rs
@@ -60,11 +60,14 @@ fn test_init_invalid_shell() {
assert_cmd_snapshot!(cmd, @r"
success: false
- exit_code: 1
+ exit_code: 2
----- stdout -----
----- stderr -----
- Unsupported shell: invalid-shell
+ error: invalid value 'invalid-shell' for '<SHELL>'
+ [possible values: bash, elvish, fish, nushell, oil, powershell, xonsh, zsh]
+
+ For more information, try '--help'.
");
});
}
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index 6e5f02774..2bef8bfca 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpYBjef2/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpSZbFNE/test-config.toml
---
success: true
exit_code: 0
@@ -340,7 +340,7 @@ _wt() {
return 0
;;
wt__init)
- opts="-v -h --cmd --verbose --help <SHELL>"
+ opts="-v -h --cmd --verbose --help bash elvish fish nushell oil powershell xonsh zsh"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap
index 09510696e..3fd165387 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash_custom_prefix.snap
@@ -18,7 +18,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpCXsTxF/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpEXV3PM/test-config.toml
---
success: true
exit_code: 0
@@ -342,7 +342,7 @@ _wt() {
return 0
;;
wt__init)
- opts="-v -h --cmd --verbose --help <SHELL>"
+ opts="-v -h --cmd --verbose --help bash elvish fish nushell oil powershell xonsh zsh"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index dcea75cfb..fecf23f2b 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpNVNuYN/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp84YMkU/test-config.toml
---
success: true
exit_code: 0
@@ -167,7 +167,7 @@ if type -q wt
end
# Register dynamic completions
- complete -c wt -n '__fish_seen_subcommand_from init' -f -a '(__wt_complete)' -d 'Shell to generate code for (bash, fish, zsh)'
+ complete -c wt -n '__fish_seen_subcommand_from init' -f -a '(__wt_complete)' -d 'Shell to generate code for'
complete -c wt -n '__fish_seen_subcommand_from switch' -f -a '(__wt_complete)' -d 'Branch name or worktree path'
complete -c wt -n '__fish_seen_subcommand_from remove' -f -a '(__wt_complete)' -d 'Worktree names or branches to remove (defaults to current worktree if none specified)'
complete -c wt -n '__fish_seen_subcommand_from push' -f -a '(__wt_complete)' -d 'Target branch (defaults to default branch)'
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index f4eaf4aae..b4d3e835b 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpNfo5re/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpohHD45/test-config.toml
---
success: true
exit_code: 0
@@ -159,7 +159,7 @@ _arguments "${_arguments_options[@]}" : \
'--verbose[Enable verbose output (show git commands and debug info)]' \
'-h[Print help]' \
'--help[Print help]' \
-':shell -- Shell to generate code for (bash, fish, zsh):_default' \
+':shell -- Shell to generate code for:(bash elvish fish nushell oil powershell xonsh zsh)' \
&& ret=0
;;
(configure-shell)