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

Do not repeat subcommands after one is already given in Fish, Bash and PowerShell completions

Commit
eae9bab5d31acdf151a882d01a5f94e592166945
Author
Leitwolf11 ·
Parents
932240749e9c

Files · Download .tar.gz

Changes

 src/Progmasoft.Tazel/Cli/ShellCompletion.cs | 130 ++++++++++++++++++++--------
 1 file changed, 95 insertions(+), 35 deletions(-)

diff --git a/src/Progmasoft.Tazel/Cli/ShellCompletion.cs b/src/Progmasoft.Tazel/Cli/ShellCompletion.cs
index db26013..ca58356 100644
--- a/src/Progmasoft.Tazel/Cli/ShellCompletion.cs
+++ b/src/Progmasoft.Tazel/Cli/ShellCompletion.cs
@@ -199,13 +199,20 @@ internal static class ShellCompletion
                 esac
 
                 cmd=""
+                local subcmd=""
                 local i
                 for (( i = 1; i < COMP_CWORD; i++ )); do
                     case "${COMP_WORDS[i]}" 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|-o|--algorithm)
+                            (( i++ ))
+                            ;;
                         -*) ;;
                         *)
-                            cmd="${COMP_WORDS[i]}"
-                            break
+                            if [[ -z "$cmd" ]]; then
+                                cmd="${COMP_WORDS[i]}"
+                            elif [[ -z "$subcmd" ]]; then
+                                subcmd="${COMP_WORDS[i]}"
+                            fi
                             ;;
                     esac
                 done
@@ -230,7 +237,8 @@ internal static class ShellCompletion
                         words="$words --expunge"
                         ;;
                     help)
-                        words="$words --short --long {{commands}}"
+                        words="$words --short --long"
+                        if [[ -z "$subcmd" ]]; then words="$words {{commands}}"; fi
                         ;;
                     explain)
                         words="$words --short --long"
@@ -239,31 +247,36 @@ internal static class ShellCompletion
                         words="$words --output -o"
                         ;;
                     shell-completion)
-                        words="$words {{shells}}"
+                        if [[ -z "$subcmd" ]]; then words="$words {{shells}}"; fi
                         ;;
                     cache)
-                        words="$words keygen validate repair --algorithm --remote"
+                        words="$words --algorithm --remote"
+                        if [[ -z "$subcmd" ]]; then words="$words keygen validate repair"; fi
                         ;;
                     dotnet)
-                        words="$words get what-is start-daemon restart-daemon stop-daemon"
+                        if [[ -z "$subcmd" ]]; then words="$words get what-is start-daemon restart-daemon stop-daemon"; fi
                         ;;
                     options)
-                        words="$words validate get what-is update --user --project --local --create"
+                        words="$words --user --project --local --create"
+                        if [[ -z "$subcmd" ]]; then words="$words validate get what-is update"; fi
                         ;;
                     custom-cmds|envs)
-                        words="$words validate --user --project --local"
+                        words="$words --user --project --local"
+                        if [[ -z "$subcmd" ]]; then words="$words validate"; fi
                         ;;
                     tool)
-                        words="$words launch create-wrapper --output-dir -o"
+                        words="$words --output-dir -o"
+                        if [[ -z "$subcmd" ]]; then words="$words launch create-wrapper"; fi
                         ;;
                     ignore)
-                        words="$words create-from validate add delete del"
+                        if [[ -z "$subcmd" ]]; then words="$words create-from validate add delete del"; fi
                         ;;
                     portal-modules)
-                        words="$words search info --exact -e"
+                        words="$words --exact -e"
+                        if [[ -z "$subcmd" ]]; then words="$words search info"; fi
                         ;;
                     limited-time)
-                        words="$words C#"
+                        if [[ -z "$subcmd" ]]; then words="$words C#"; fi
                         ;;
                 esac
 
@@ -418,6 +431,37 @@ internal static class ShellCompletion
         sb.AppendLine("    end");
         sb.AppendLine("end");
         sb.AppendLine();
+        sb.AppendLine("function __tazel_needs_subcmd");
+        sb.AppendLine("    set -l cmd $argv[1]");
+        sb.AppendLine("    set -l tokens (commandline -opc)");
+        sb.AppendLine("    set -l seen 0");
+        sb.AppendLine("    set -l skip 0");
+        sb.AppendLine("    for i in (seq 2 (count $tokens))");
+        sb.AppendLine("        set -l t $tokens[$i]");
+        sb.AppendLine("        if test $skip -eq 1");
+        sb.AppendLine("            set skip 0");
+        sb.AppendLine("            continue");
+        sb.AppendLine("        end");
+        sb.AppendLine("        switch $t");
+        sb.AppendLine("            case --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 -o --algorithm");
+        sb.AppendLine("                set skip 1");
+        sb.AppendLine("                continue");
+        sb.AppendLine("            case '-*'");
+        sb.AppendLine("                continue");
+        sb.AppendLine("        end");
+        sb.AppendLine("        if test $seen -eq 0");
+        sb.AppendLine("            if test \"$t\" = \"$cmd\"");
+        sb.AppendLine("                set seen 1");
+        sb.AppendLine("            else");
+        sb.AppendLine("                return 1");
+        sb.AppendLine("            end");
+        sb.AppendLine("        else");
+        sb.AppendLine("            return 1");
+        sb.AppendLine("        end");
+        sb.AppendLine("    end");
+        sb.AppendLine("    test $seen -eq 1");
+        sb.AppendLine("end");
+        sb.AppendLine();
         sb.AppendLine("complete -c tazel -f");
         sb.AppendLine();
         sb.AppendLine("# Global options (space, = and : separators supported without forcing a trailing space)");
@@ -455,27 +499,28 @@ internal static class ShellCompletion
         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 '__tazel_needs_subcmd 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'");
         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 '__tazel_needs_subcmd shell-completion' -a 'bash zsh fish powershell'");
+        sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd cache' -a 'keygen validate repair'");
         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'");
+        sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd dotnet' -a 'get what-is start-daemon restart-daemon stop-daemon'");
+        sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd options' -a 'validate get what-is update'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options custom-cmds envs' -l user -d 'User layer'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options custom-cmds envs' -l project -d 'Project layer'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from options custom-cmds envs' -l local -d 'Local layer'");
         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 '__tazel_needs_subcmd custom-cmds' -a 'validate'");
+        sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd envs' -a 'validate'");
+        sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd tool' -a 'launch create-wrapper'");
         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 '__tazel_needs_subcmd ignore' -a 'create-from validate add delete del'");
+        sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd portal-modules' -a 'search info'");
         sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from portal-modules' -s e -l exact -d 'Exact match'");
-        sb.AppendLine("complete -c tazel -n '__fish_seen_subcommand_from limited-time' -a 'C#'");
+        sb.AppendLine("complete -c tazel -n '__tazel_needs_subcmd limited-time' -a 'C#'");
         return sb.ToString().TrimEnd();
     }
 
@@ -514,15 +559,30 @@ internal static class ShellCompletion
                     $elements += $el.Extent.Text
                 }
 
+                $valueOpts = @('--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', '-o', '--algorithm')
                 $cmd = $null
+                $subcmd = $null
+                $skipNext = $false
                 for ($i = 1; $i -lt $elements.Count; $i++) {
                     $token = $elements[$i]
                     if ($token -eq $wordToComplete -and $i -eq ($elements.Count - 1)) {
                         continue
                     }
-                    if (-not $token.StartsWith('-')) {
+                    if ($skipNext) {
+                        $skipNext = $false
+                        continue
+                    }
+                    if ($valueOpts -contains $token) {
+                        $skipNext = $true
+                        continue
+                    }
+                    if ($token.StartsWith('-')) {
+                        continue
+                    }
+                    if ($null -eq $cmd) {
                         $cmd = $token
-                        break
+                    } elseif ($null -eq $subcmd) {
+                        $subcmd = $token
                     }
                 }
 
@@ -561,19 +621,19 @@ internal static class ShellCompletion
                         'cquery'           { $candidates += @('--output') + $buildOptions }
                         'aquery'           { $candidates += @('--output') + $buildOptions }
                         'clean'            { $candidates += @('--expunge') }
-                        'help'             { $candidates += @('--short', '--long') + $commands }
+                        'help'             { $candidates += @('--short', '--long'); if ($null -eq $subcmd) { $candidates += $commands } }
                         'explain'          { $candidates += @('--short', '--long') }
                         'license'          { $candidates += @('--output', '-o') }
-                        'shell-completion' { $candidates += @('bash', 'zsh', 'fish', 'powershell') }
-                        'cache'            { $candidates += @('keygen', 'validate', 'repair', '--algorithm', '--remote') }
-                        'dotnet'           { $candidates += @('get', 'what-is', 'start-daemon', 'restart-daemon', 'stop-daemon') }
-                        'options'          { $candidates += @('validate', 'get', 'what-is', 'update', '--user', '--project', '--local', '--create') }
-                        'custom-cmds'      { $candidates += @('validate', '--user', '--project', '--local') }
-                        'envs'             { $candidates += @('validate', '--user', '--project', '--local') }
-                        'tool'             { $candidates += @('launch', 'create-wrapper', '--output-dir', '-o') }
-                        'ignore'           { $candidates += @('create-from', 'validate', 'add', 'delete', 'del') }
-                        'portal-modules'   { $candidates += @('search', 'info', '--exact', '-e') }
-                        'limited-time'     { $candidates += @('C#') }
+                        'shell-completion' { if ($null -eq $subcmd) { $candidates += @('bash', 'zsh', 'fish', 'powershell') } }
+                        'cache'            { $candidates += @('--algorithm', '--remote'); if ($null -eq $subcmd) { $candidates += @('keygen', 'validate', 'repair') } }
+                        'dotnet'           { if ($null -eq $subcmd) { $candidates += @('get', 'what-is', 'start-daemon', 'restart-daemon', 'stop-daemon') } }
+                        'options'          { $candidates += @('--user', '--project', '--local', '--create'); if ($null -eq $subcmd) { $candidates += @('validate', 'get', 'what-is', 'update') } }
+                        'custom-cmds'      { $candidates += @('--user', '--project', '--local'); if ($null -eq $subcmd) { $candidates += @('validate') } }
+                        'envs'             { $candidates += @('--user', '--project', '--local'); if ($null -eq $subcmd) { $candidates += @('validate') } }
+                        'tool'             { $candidates += @('--output-dir', '-o'); if ($null -eq $subcmd) { $candidates += @('launch', 'create-wrapper') } }
+                        'ignore'           { if ($null -eq $subcmd) { $candidates += @('create-from', 'validate', 'add', 'delete', 'del') } }
+                        'portal-modules'   { $candidates += @('--exact', '-e'); if ($null -eq $subcmd) { $candidates += @('search', 'info') } }
+                        'limited-time'     { if ($null -eq $subcmd) { $candidates += @('C#') } }
                     }
                 }