Progmasoft Git

tazel

A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.

Clonegit clone https://git.progmasoft.com/tazel

Avoid forcing trailing space on value options in Fish and Bash completions

Commit
932240749e9ca0462601830f3ca8ea06345ad7f8
Author
Leitwolf11 ·
Parents
ff04e2358fe8

Files · Download .tar.gz

Changes

 src/Progmasoft.Tazel/Cli/ShellCompletion.cs        | 114 ++++++++++++++++++---
 .../ShellCompletionAndExplainTests.cs              |  10 ++
 2 files changed, 111 insertions(+), 13 deletions(-)

diff --git a/src/Progmasoft.Tazel/Cli/ShellCompletion.cs b/src/Progmasoft.Tazel/Cli/ShellCompletion.cs
index 38f9e22..db26013 100644
--- a/src/Progmasoft.Tazel/Cli/ShellCompletion.cs
+++ b/src/Progmasoft.Tazel/Cli/ShellCompletion.cs
@@ -132,6 +132,44 @@ internal static class ShellCompletion
                     prev="${COMP_WORDS[COMP_CWORD-1]}"
                 fi
 
+                case "$cur" in
+                    --color=*|--color:*|--progress=*|--progress:*)
+                        local prefix="${cur%%[:=]*}"
+                        local sep="${cur:${#prefix}:1}"
+                        COMPREPLY=( $(compgen -P "${prefix}${sep}" -W "always auto never" -- "${cur#*[:=]}") )
+                        return 0
+                        ;;
+                    --message-format=*|--message-format:*)
+                        local prefix="${cur%%[:=]*}"
+                        local sep="${cur:${#prefix}:1}"
+                        COMPREPLY=( $(compgen -P "${prefix}${sep}" -W "rich-stdio script-friendly-stdio jsonl xml" -- "${cur#*[:=]}") )
+                        return 0
+                        ;;
+                    --mode=*|--mode:*)
+                        local prefix="${cur%%[:=]*}"
+                        local sep="${cur:${#prefix}:1}"
+                        COMPREPLY=( $(compgen -P "${prefix}${sep}" -W "debug release" -- "${cur#*[:=]}") )
+                        return 0
+                        ;;
+                    --algorithm=*|--algorithm:*)
+                        local prefix="${cur%%[:=]*}"
+                        local sep="${cur:${#prefix}:1}"
+                        COMPREPLY=( $(compgen -P "${prefix}${sep}" -W "ed25519 rsa-4096 ml-dsa" -- "${cur#*[:=]}") )
+                        return 0
+                        ;;
+                    --output=*|--output:*)
+                        local prefix="${cur%%[:=]*}"
+                        local sep="${cur:${#prefix}:1}"
+                        if [[ "${COMP_WORDS[1]}" == "license" ]]; then
+                            COMPREPLY=( $(compgen -P "${prefix}${sep}" -W "text md markdown rst" -- "${cur#*[:=]}") )
+                            return 0
+                        elif [[ "${COMP_WORDS[1]}" == "query" || "${COMP_WORDS[1]}" == "cquery" || "${COMP_WORDS[1]}" == "aquery" ]]; then
+                            COMPREPLY=( $(compgen -P "${prefix}${sep}" -W "label label_kind" -- "${cur#*[:=]}") )
+                            return 0
+                        fi
+                        ;;
+                esac
+
                 case "$prev" in
                     --color|--progress)
                         COMPREPLY=( $(compgen -W "always auto never" -- "$cur") )
@@ -230,6 +268,13 @@ internal static class ShellCompletion
                 esac
 
                 COMPREPLY=( $(compgen -W "$words" -- "$cur") )
+                if (( ${#COMPREPLY[@]} == 1 )); then
+                    case "${COMPREPLY[0]}" in
+                        --color|--progress|--dotnet-settings|--message-format|--mode|--platform|--jobs|--feature|--no-feature|--compiler-options|--c-compiler-options|--cpp-compiler-options|--linker-options|--zig-compiler-options|--output|--output-dir|--algorithm)
+                            compopt -o nospace 2>/dev/null || true
+                            ;;
+                    esac
+                fi
                 return 0
             }
 
@@ -351,14 +396,36 @@ internal static class ShellCompletion
         sb.AppendLine("# Tazel shell completion for Fish.");
         sb.AppendLine("# Load in the current shell: tazel shell-completion fish | source");
         sb.AppendLine();
+        sb.AppendLine("function __tazel_complete_val_opt");
+        sb.AppendLine("    set -l tok (commandline -ct)");
+        sb.AppendLine("    set -l opt $argv[1]");
+        sb.AppendLine("    set -l vals $argv[2..-1]");
+        sb.AppendLine("    switch $tok");
+        sb.AppendLine("        case \"$opt:*\"");
+        sb.AppendLine("            for v in $vals");
+        sb.AppendLine("                echo \"$opt:$v\"");
+        sb.AppendLine("            end");
+        sb.AppendLine("        case \"$opt=*\"");
+        sb.AppendLine("            for v in $vals");
+        sb.AppendLine("                echo \"$opt=$v\"");
+        sb.AppendLine("            end");
+        sb.AppendLine("        case \"-*\"");
+        sb.AppendLine("            if string match -q -- \"$tok*\" \"$opt\"");
+        sb.AppendLine("                echo \"$opt\"");
+        sb.AppendLine("                echo \"$opt=\"");
+        sb.AppendLine("                echo \"$opt:\"");
+        sb.AppendLine("            end");
+        sb.AppendLine("    end");
+        sb.AppendLine("end");
+        sb.AppendLine();
         sb.AppendLine("complete -c tazel -f");
         sb.AppendLine();
-        sb.AppendLine("# Global options");
-        sb.AppendLine("complete -c tazel -l color -x -a 'always auto never' -d 'Color output'");
-        sb.AppendLine("complete -c tazel -l progress -x -a 'always auto never' -d 'Progress output'");
+        sb.AppendLine("# Global options (space, = and : separators supported without forcing a trailing space)");
+        AppendFishValueOption(sb, null, "color", "always auto never", "Color output");
+        AppendFishValueOption(sb, null, "progress", "always auto never", "Progress output");
         sb.AppendLine("complete -c tazel -l quiet -d 'Suppress status output'");
-        sb.AppendLine("complete -c tazel -l dotnet-settings -x -d 'Set .NET runtime settings'");
-        sb.AppendLine("complete -c tazel -l message-format -x -a 'rich-stdio script-friendly-stdio jsonl xml' -d 'Message output format'");
+        AppendFishValueOption(sb, null, "dotnet-settings", null, "Set .NET runtime settings");
+        AppendFishValueOption(sb, null, "message-format", "rich-stdio script-friendly-stdio jsonl xml", "Message output format");
         sb.AppendLine("complete -c tazel -s h -l help -d 'Show help'");
         sb.AppendLine();
         sb.AppendLine("# Commands");
@@ -370,23 +437,31 @@ internal static class ShellCompletion
 
         sb.AppendLine();
         sb.AppendLine("# Command-specific completions");
-        sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from build run test coverage cquery aquery' -l mode -x -a 'debug release' -d 'Build mode'");
-        sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from build run test coverage cquery aquery' -l platform -x -d 'Target platform'");
-        sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from build run test coverage cquery aquery' -l jobs -x -d 'Parallel jobs'");
-        sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from build run test coverage cquery aquery' -l keep-going -d 'Continue after failures'");
+        const string buildSubcmds = "__fish_seen_subcommand_from build run test coverage cquery aquery";
+        AppendFishValueOption(sb, buildSubcmds, "mode", "debug release", "Build mode");
+        AppendFishValueOption(sb, buildSubcmds, "platform", null, "Target platform");
+        AppendFishValueOption(sb, buildSubcmds, "jobs", null, "Parallel jobs");
+        AppendFishValueOption(sb, buildSubcmds, "feature", null, "Enable toolchain feature");
+        AppendFishValueOption(sb, buildSubcmds, "no-feature", null, "Disable toolchain feature");
+        AppendFishValueOption(sb, buildSubcmds, "compiler-options", null, "C-family compiler options");
+        AppendFishValueOption(sb, buildSubcmds, "c-compiler-options", null, "C/ObjC compiler options");
+        AppendFishValueOption(sb, buildSubcmds, "cpp-compiler-options", null, "C++/ObjC++ compiler options");
+        AppendFishValueOption(sb, buildSubcmds, "linker-options", null, "Linker options");
+        AppendFishValueOption(sb, buildSubcmds, "zig-compiler-options", null, "Zig compiler options");
+        sb.AppendLine($"complete -c tazel -n '{buildSubcmds}' -l keep-going -d 'Continue after failures'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from build run test coverage' -l recompile -d 'Recompile all sources'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from build run test coverage' -l no-cache -d 'Bypass cache'");
-        sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from query cquery aquery' -l output -x -a 'label label_kind' -d 'Output format'");
+        AppendFishValueOption(sb, "__fish_seen_subcommand_from query cquery aquery", "output", "label label_kind", "Output format");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from clean' -l expunge -d 'Remove all of .tazel'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from help' -l short -d 'Short help'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from help' -l long -d 'Detailed help'");
         sb.AppendLine($"complete -c tazel -n '__fish_seen_subcommand_from help' -a '{string.Join(' ', CommandLine.Commands)}'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from explain' -l short -d 'Short explanation'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from explain' -l long -d 'Detailed explanation'");
-        sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from license' -s o -l output -x -a 'text md markdown rst' -d 'License format'");
+        AppendFishValueOption(sb, "__fish_seen_subcommand_from license", "output", "text md markdown rst", "License format", shortOpt: "o");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from shell-completion' -a 'bash zsh fish powershell'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from cache' -a 'keygen validate repair'");
-        sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from cache' -l algorithm -x -a 'ed25519 rsa-4096 ml-dsa' -d 'Signing algorithm'");
+        AppendFishValueOption(sb, "__fish_seen_subcommand_from cache", "algorithm", "ed25519 rsa-4096 ml-dsa", "Signing algorithm");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from cache' -l remote -d 'Include remote cache'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from dotnet' -a 'get what-is start-daemon restart-daemon stop-daemon'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options' -a 'validate get what-is update'");
@@ -396,7 +471,7 @@ internal static class ShellCompletion
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options' -l create -d 'Create file if missing'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from custom-cmds envs' -a 'validate'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from tool' -a 'launch create-wrapper'");
-        sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from tool' -s o -l output-dir -r -d 'Output directory'");
+        AppendFishValueOption(sb, "__fish_seen_subcommand_from tool", "output-dir", null, "Output directory", shortOpt: "o", requireFiles: true);
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from ignore' -a 'create-from validate add delete del'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from portal-modules' -a 'search info'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from portal-modules' -s e -l exact -d 'Exact match'");
@@ -404,6 +479,19 @@ internal static class ShellCompletion
         return sb.ToString().TrimEnd();
     }
 
+    private static void AppendFishValueOption(
+        StringBuilder sb, string? condition, string longOpt, string? values, string description,
+        string? shortOpt = null, bool requireFiles = false)
+    {
+        var cond = condition is null ? "" : $" -n '{condition}'";
+        var shortPart = shortOpt is null ? "" : $" -s {shortOpt}";
+        var mode = requireFiles ? "-r" : "-x";
+        var valuesArg = values is null ? "" : $" -a '{values}'";
+        var helperArgs = values is null ? $"--{longOpt}" : $"--{longOpt} {values}";
+        sb.AppendLine($"complete -c tazel{cond}{shortPart} -l {longOpt} {mode}{valuesArg} -d '{description}'");
+        sb.AppendLine($"complete -c tazel{cond} -a '(__tazel_complete_val_opt {helperArgs})' -d '{description}'");
+    }
+
     private static string PowerShell()
     {
         // Strictly compatible with Windows PowerShell 5.1 and later (no PowerShell 7-only syntax).
diff --git a/tests/Progmasoft.Tazel.Tests/ShellCompletionAndExplainTests.cs b/tests/Progmasoft.Tazel.Tests/ShellCompletionAndExplainTests.cs
index e9e92d3..9fdda6d 100644
--- a/tests/Progmasoft.Tazel.Tests/ShellCompletionAndExplainTests.cs
+++ b/tests/Progmasoft.Tazel.Tests/ShellCompletionAndExplainTests.cs
@@ -112,6 +112,16 @@ public sealed class ShellCompletionAndExplainTests
         }
     }
 
+    [Fact]
+    public void FishScriptDoesNotForceTrailingSpaceOnValueOptions()
+    {
+        var script = ShellCompletion.Generate(ShellKind.Fish);
+        Assert.Contains("function __tazel_complete_val_opt", script, StringComparison.Ordinal);
+        Assert.Contains("__tazel_complete_val_opt --message-format rich-stdio script-friendly-stdio jsonl xml", script, StringComparison.Ordinal);
+        Assert.Contains("echo \"$opt=\"", script, StringComparison.Ordinal);
+        Assert.Contains("echo \"$opt:\"", script, StringComparison.Ordinal);
+    }
+
     [Fact]
     public void ZshScriptSupportsDirectSourceAndCompdef()
     {