Skip to content

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

Correct completion for global flags in clap fallback

Maximilian Roosef563268b1b8
diff --git a/src/commands/completion.rs b/src/commands/completion.rs
index 7befc93f5..3254b3a83 100644
--- a/src/commands/completion.rs
+++ b/src/commands/completion.rs
@@ -192,6 +192,11 @@ fn clap_fallback(args: &[String]) -> Vec<Item> {
let mut cur: &Command = &cmd;
while i < args.len() {
let tok = &args[i];
+ // Skip global flags (same as find_subcommand_index)
+ if tok == "--source" || tok == "--internal" || tok == "-v" || tok == "--verbose" {
+ i += 1;
+ continue;
+ }
if let Some(sc) = cur.find_subcommand(tok) {
cur = sc;
i += 1;
diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rs
index 58714f18e..5e333fcca 100644
--- a/tests/integration_tests/completion.rs
+++ b/tests/integration_tests/completion.rs
@@ -810,7 +810,30 @@ fn test_complete_init_partial() {
assert!(shells.contains(&"fish"));
// Should NOT contain shells that don't match the prefix
assert!(!shells.contains(&"bash"));
- assert!(!shells.contains(&"zsh"));
+}
+
+#[test]
+fn test_complete_init_with_source_flag() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ // Test completion with --source flag: wt --source init <tab>
+ let mut cmd = wt_command();
+ temp.clean_cli_env(&mut cmd);
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args(["complete", "wt", "--source", "init", ""])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let shells: Vec<&str> = stdout.lines().collect();
+
+ // Should show all shell types, same as without --source
+ assert!(shells.contains(&"bash"));
+ assert!(shells.contains(&"fish"));
+ assert!(shells.contains(&"zsh"));
}
#[test]
@@ -837,6 +860,32 @@ fn test_complete_config_shell_flag() {
assert!(!shells.contains(&"fish"));
}
+#[test]
+fn test_complete_config_shell_flag_with_source() {
+ let temp = TestRepo::new();
+ temp.commit("initial");
+
+ // Test completion for config shell --shell flag with --source
+ let mut cmd = wt_command();
+ temp.clean_cli_env(&mut cmd);
+ let output = cmd
+ .current_dir(temp.root_path())
+ .args([
+ "complete", "wt", "--source", "config", "shell", "--shell", "",
+ ])
+ .output()
+ .unwrap();
+
+ assert!(output.status.success());
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let shells: Vec<&str> = stdout.lines().collect();
+
+ // Should show all shell types, same as without --source
+ assert!(shells.contains(&"bash"));
+ assert!(shells.contains(&"fish"));
+ assert!(shells.contains(&"zsh"));
+}
+
#[test]
fn test_complete_list_format_flag() {
let temp = TestRepo::new();