Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Filter out options when completing positionals
Maximilian Roosa285f40ee91d
diff --git a/src/completion.rs b/src/completion.rsindex 1a60581e0..bc27cecbc 100644--- a/src/completion.rs+++ b/src/completion.rs@@ -108,7 +108,38 @@ thread_local! {}fn completion_command() -> Command {- adjust_completion_command(cli::build_command())+ let cmd = cli::build_command();+ let cmd = adjust_completion_command(cmd);+ hide_non_positional_options_for_completion(cmd)+}++/// Hide non-positional options so they're filtered out when positional/subcommand+/// completions exist, but still shown when completing `--<TAB>`.+///+/// This exploits clap_complete's behavior: if any non-hidden candidates exist,+/// hidden ones are dropped. When all candidates are hidden, they're kept.+fn hide_non_positional_options_for_completion(cmd: Command) -> Command {+ // Disable built-in help/version flags for completion only+ let cmd = cmd+ .disable_help_flag(true)+ .disable_help_subcommand(true)+ .disable_version_flag(true);++ fn recurse(cmd: Command) -> Command {+ // Hide every non-positional arg on this Command+ let cmd = cmd.mut_args(|arg| {+ if arg.is_positional() {+ arg+ } else {+ arg.hide(true)+ }+ });++ // Recurse into subcommands+ cmd.mut_subcommands(recurse)+ }++ recurse(cmd)}// Mark positional args as `.last(true)` to allow them after all flags.diff --git a/tests/integration_tests/completion.rs b/tests/integration_tests/completion.rsindex dca737159..35979d409 100644--- a/tests/integration_tests/completion.rs+++ b/tests/integration_tests/completion.rs@@ -9,6 +9,10 @@ fn only_option_suggestions(stdout: &str) -> bool {.all(|line| line.starts_with('-'))}+fn has_any_options(stdout: &str) -> bool {+ stdout.lines().any(|line| line.trim().starts_with('-'))+}+fn value_suggestions(stdout: &str) -> Vec<&str> {stdout.lines()@@ -1036,3 +1040,66 @@ fn test_complete_remove_flexible_argument_ordering() {"Should suggest additional worktrees after positional and flag, got:\n{stdout}");}++#[test]+fn test_complete_filters_options_when_positionals_exist() {+ let temp = TestRepo::new();+ temp.commit("initial");++ Command::new("git")+ .args(["branch", "feature"])+ .current_dir(temp.root_path())+ .output()+ .unwrap();++ // Test: wt switch <cursor>+ // Should show branches but NOT options like --config, --verbose, -C+ let output = temp.completion_cmd(&["wt", "switch", ""]).output().unwrap();+ assert!(output.status.success());+ let stdout = String::from_utf8_lossy(&output.stdout);++ // Should have branch completions+ assert!(stdout.contains("feature"), "Should contain feature branch");+ assert!(stdout.contains("main"), "Should contain main branch");++ // Should NOT have options (they're filtered out when positionals exist)+ assert!(+ !has_any_options(&stdout),+ "Options should be filtered out when positional completions exist, got:\n{stdout}"+ );+}++#[test]+fn test_complete_subcommands_filter_options() {+ let temp = TestRepo::new();+ temp.commit("initial");++ // Test: wt <cursor>+ // Should show subcommands but NOT global options+ let output = temp.completion_cmd(&["wt", ""]).output().unwrap();+ assert!(output.status.success());+ let stdout = String::from_utf8_lossy(&output.stdout);+ let suggestions = value_suggestions(&stdout);++ // Should have subcommands+ assert!(suggestions.contains(&"switch"), "Should contain switch");+ assert!(suggestions.contains(&"list"), "Should contain list");+ assert!(suggestions.contains(&"merge"), "Should contain merge");++ // Should NOT have global options+ assert!(+ !has_any_options(&stdout),+ "Global options should be filtered out at subcommand position, got:\n{stdout}"+ );++ // Test: wt --<cursor>+ // Now options SHOULD appear+ let output = temp.completion_cmd(&["wt", "--"]).output().unwrap();+ assert!(output.status.success());+ let stdout = String::from_utf8_lossy(&output.stdout);++ assert!(+ has_any_options(&stdout),+ "Options should appear when explicitly completing with --, got:\n{stdout}"+ );+}