@kjanat/dreamcli 2.5.0 → 3.0.0-rc.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. package/CHANGELOG.md +344 -1
  2. package/README.md +9 -2
  3. package/dist/core/cli/dispatch.d.mts +6 -5
  4. package/dist/core/cli/dispatch.mjs +205 -1
  5. package/dist/core/cli/help-links.d.mts +0 -1
  6. package/dist/core/cli/help-links.mjs +41 -1
  7. package/dist/core/cli/index.d.mts +185 -19
  8. package/dist/core/cli/index.mjs +945 -1
  9. package/dist/core/cli/planner.d.mts +32 -6
  10. package/dist/core/cli/planner.mjs +300 -1
  11. package/dist/core/cli/plugin.d.mts +1 -1
  12. package/dist/core/cli/plugin.mjs +41 -1
  13. package/dist/core/cli/propagate.d.mts +1 -1
  14. package/dist/core/cli/propagate.mjs +57 -1
  15. package/dist/core/cli/root-help.d.mts +18 -3
  16. package/dist/core/cli/root-help.mjs +226 -6
  17. package/dist/core/cli/root-surface.d.mts +30 -4
  18. package/dist/core/cli/root-surface.mjs +28 -1
  19. package/dist/core/cli/runtime-preflight.d.mts +30 -3
  20. package/dist/core/cli/runtime-preflight.mjs +169 -1
  21. package/dist/core/completion/index.d.mts +32 -2
  22. package/dist/core/completion/index.mjs +90 -1
  23. package/dist/core/completion/shells/bash.d.mts +0 -1
  24. package/dist/core/completion/shells/bash.mjs +308 -3
  25. package/dist/core/completion/shells/fish.d.mts +0 -1
  26. package/dist/core/completion/shells/fish.mjs +162 -2
  27. package/dist/core/completion/shells/powershell.d.mts +0 -1
  28. package/dist/core/completion/shells/powershell.mjs +358 -2
  29. package/dist/core/completion/shells/shared.d.mts +23 -2
  30. package/dist/core/completion/shells/shared.mjs +176 -1
  31. package/dist/core/completion/shells/zsh.d.mts +0 -1
  32. package/dist/core/completion/shells/zsh.mjs +251 -2
  33. package/dist/core/config/index.d.mts +2 -3
  34. package/dist/core/config/index.mjs +246 -1
  35. package/dist/core/config/package-json.d.mts +27 -4
  36. package/dist/core/config/package-json.mjs +386 -3
  37. package/dist/core/errors/index.d.mts +3 -3
  38. package/dist/core/errors/index.mjs +87 -1
  39. package/dist/core/execution/index.d.mts +0 -1
  40. package/dist/core/execution/index.mjs +201 -1
  41. package/dist/core/help/ansi.d.mts +11 -5
  42. package/dist/core/help/ansi.mjs +121 -2
  43. package/dist/core/help/index.d.mts +65 -2
  44. package/dist/core/help/index.mjs +341 -7
  45. package/dist/core/help/theme.d.mts +68 -0
  46. package/dist/core/help/theme.mjs +46 -0
  47. package/dist/core/json-schema/index.d.mts +15 -2
  48. package/dist/core/json-schema/index.mjs +536 -12
  49. package/dist/core/json-schema/meta-descriptions.generated.d.mts +22 -5
  50. package/dist/core/json-schema/meta-descriptions.generated.mjs +102 -28
  51. package/dist/core/output/activity.d.mts +0 -1
  52. package/dist/core/output/activity.mjs +452 -1
  53. package/dist/core/output/bind.mjs +56 -1
  54. package/dist/core/output/contracts.d.mts +4 -5
  55. package/dist/core/output/contracts.mjs +57 -1
  56. package/dist/core/output/display-value.mjs +32 -1
  57. package/dist/core/output/index.d.mts +21 -1
  58. package/dist/core/output/index.mjs +442 -2
  59. package/dist/core/output/renderers.d.mts +0 -1
  60. package/dist/core/output/renderers.mjs +46 -1
  61. package/dist/core/output/writer.mjs +12 -1
  62. package/dist/core/parse/index.d.mts +54 -7
  63. package/dist/core/parse/index.mjs +727 -1
  64. package/dist/core/prompt/index.d.mts +12 -4
  65. package/dist/core/prompt/index.mjs +348 -6
  66. package/dist/core/resolve/args.d.mts +1 -1
  67. package/dist/core/resolve/args.mjs +105 -1
  68. package/dist/core/resolve/coerce.d.mts +1 -1
  69. package/dist/core/resolve/coerce.mjs +336 -1
  70. package/dist/core/resolve/config.mjs +19 -1
  71. package/dist/core/resolve/contracts.d.mts +9 -4
  72. package/dist/core/resolve/errors.d.mts +0 -1
  73. package/dist/core/resolve/errors.mjs +140 -2
  74. package/dist/core/resolve/flags.d.mts +7 -2
  75. package/dist/core/resolve/flags.mjs +251 -1
  76. package/dist/core/resolve/index.d.mts +2 -2
  77. package/dist/core/resolve/index.mjs +85 -1
  78. package/dist/core/resolve/property.d.mts +6 -2
  79. package/dist/core/resolve/property.mjs +53 -1
  80. package/dist/core/schema/activity.d.mts +31 -11
  81. package/dist/core/schema/arg.d.mts +92 -13
  82. package/dist/core/schema/arg.mjs +463 -1
  83. package/dist/core/schema/command.d.mts +29 -7
  84. package/dist/core/schema/command.mjs +726 -1
  85. package/dist/core/schema/flag.d.mts +577 -21
  86. package/dist/core/schema/flag.mjs +705 -1
  87. package/dist/core/schema/index.d.mts +5 -2
  88. package/dist/core/schema/middleware.d.mts +4 -3
  89. package/dist/core/schema/middleware.mjs +41 -1
  90. package/dist/core/schema/number-constraints.d.mts +96 -0
  91. package/dist/core/schema/number-constraints.mjs +60 -0
  92. package/dist/core/schema/prompt.d.mts +21 -3
  93. package/dist/core/schema/run.d.mts +24 -5
  94. package/dist/core/schema/string-constraints.d.mts +97 -0
  95. package/dist/core/schema/string-constraints.mjs +69 -0
  96. package/dist/core/schema/value-parsers.d.mts +78 -0
  97. package/dist/core/schema/value-parsers.mjs +142 -0
  98. package/dist/core/schema-dsl/runtime.mjs +271 -2
  99. package/dist/core/schema-dsl/to-json-schema.d.mts +0 -1
  100. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  101. package/dist/core/testkit/index.d.mts +9 -36
  102. package/dist/core/testkit/index.mjs +83 -1
  103. package/dist/index.d.mts +12 -6
  104. package/dist/index.mjs +22 -1
  105. package/dist/runtime/adapter.d.mts +38 -2
  106. package/dist/runtime/adapter.mjs +87 -1
  107. package/dist/runtime/auto.d.mts +2 -3
  108. package/dist/runtime/auto.mjs +45 -1
  109. package/dist/runtime/deno.d.mts +24 -5
  110. package/dist/runtime/deno.mjs +262 -2
  111. package/dist/runtime/detect.mjs +45 -1
  112. package/dist/runtime/index.d.mts +3 -4
  113. package/dist/runtime/node.d.mts +8 -2
  114. package/dist/runtime/node.mjs +220 -2
  115. package/dist/runtime/paths.mjs +48 -1
  116. package/dist/runtime.d.mts +4 -4
  117. package/dist/runtime.mjs +6 -1
  118. package/dist/strings.mjs +22 -1
  119. package/dist/testkit.d.mts +1 -0
  120. package/dist/testkit.mjs +5 -1
  121. package/dist/version.d.mts +7 -3
  122. package/dist/version.mjs +7 -1
  123. package/dreamcli.schema.json +36 -7
  124. package/examples/basic.ts +5 -1
  125. package/examples/gh/package.json +2 -7
  126. package/examples/pwsh-demo/package.json +1 -7
  127. package/examples/pwsh-demo/src/main.test.ts +1 -4
  128. package/examples/pwsh-demo/src/main.ts +14 -4
  129. package/examples/transport-launcher.ts +85 -0
  130. package/package.json +43 -49
  131. package/dist/runtime/bun.d.mts +0 -28
  132. package/dist/runtime/bun.mjs +0 -1
  133. package/dist/runtime/support.d.mts +0 -42
  134. package/dist/runtime/support.mjs +0 -1
  135. package/src/core/cli/dispatch.ts +0 -349
  136. package/src/core/cli/help-links.ts +0 -82
  137. package/src/core/cli/index.ts +0 -1413
  138. package/src/core/cli/planner.ts +0 -487
  139. package/src/core/cli/plugin.ts +0 -117
  140. package/src/core/cli/propagate.ts +0 -97
  141. package/src/core/cli/root-help.ts +0 -252
  142. package/src/core/cli/root-surface.ts +0 -74
  143. package/src/core/cli/runtime-preflight.ts +0 -438
  144. package/src/core/completion/index.ts +0 -88
  145. package/src/core/completion/shells/bash.ts +0 -459
  146. package/src/core/completion/shells/fish.ts +0 -259
  147. package/src/core/completion/shells/powershell.ts +0 -439
  148. package/src/core/completion/shells/shared.ts +0 -283
  149. package/src/core/completion/shells/zsh.ts +0 -381
  150. package/src/core/config/index.ts +0 -417
  151. package/src/core/config/package-json.ts +0 -560
  152. package/src/core/errors/index.ts +0 -193
  153. package/src/core/execution/index.ts +0 -268
  154. package/src/core/help/ansi.ts +0 -144
  155. package/src/core/help/index.ts +0 -516
  156. package/src/core/json-schema/index.ts +0 -891
  157. package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
  158. package/src/core/output/activity.ts +0 -575
  159. package/src/core/output/bind.ts +0 -59
  160. package/src/core/output/contracts.ts +0 -194
  161. package/src/core/output/display-value.ts +0 -48
  162. package/src/core/output/index.ts +0 -704
  163. package/src/core/output/renderers.ts +0 -125
  164. package/src/core/output/writer.ts +0 -46
  165. package/src/core/parse/index.ts +0 -681
  166. package/src/core/prompt/index.ts +0 -583
  167. package/src/core/resolve/args.ts +0 -144
  168. package/src/core/resolve/coerce.ts +0 -432
  169. package/src/core/resolve/config.ts +0 -26
  170. package/src/core/resolve/contracts.ts +0 -135
  171. package/src/core/resolve/errors.ts +0 -227
  172. package/src/core/resolve/flags.ts +0 -255
  173. package/src/core/resolve/index.ts +0 -108
  174. package/src/core/resolve/property.ts +0 -99
  175. package/src/core/schema/activity.ts +0 -240
  176. package/src/core/schema/arg.ts +0 -719
  177. package/src/core/schema/command.ts +0 -1498
  178. package/src/core/schema/flag.ts +0 -801
  179. package/src/core/schema/index.ts +0 -91
  180. package/src/core/schema/middleware.ts +0 -171
  181. package/src/core/schema/prompt.ts +0 -156
  182. package/src/core/schema/run.ts +0 -198
  183. package/src/core/schema-dsl/index.ts +0 -109
  184. package/src/core/schema-dsl/parse.ts +0 -262
  185. package/src/core/schema-dsl/runtime.ts +0 -609
  186. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  187. package/src/core/testkit/index.ts +0 -123
  188. package/src/index.ts +0 -179
  189. package/src/runtime/adapter.ts +0 -303
  190. package/src/runtime/auto.ts +0 -110
  191. package/src/runtime/bun.ts +0 -55
  192. package/src/runtime/deno.ts +0 -369
  193. package/src/runtime/detect.ts +0 -105
  194. package/src/runtime/index.ts +0 -21
  195. package/src/runtime/node-builtins.d.ts +0 -27
  196. package/src/runtime/node.ts +0 -300
  197. package/src/runtime/paths.ts +0 -64
  198. package/src/runtime/support.ts +0 -135
  199. package/src/runtime.ts +0 -31
  200. package/src/strings.ts +0 -34
  201. package/src/testkit.ts +0 -25
  202. package/src/version.ts +0 -20
@@ -1 +1,90 @@
1
- import{generateBashCompletion as e}from"./shells/bash.mjs";import{generateFishCompletion as t}from"./shells/fish.mjs";import{generatePowerShellCompletion as n}from"./shells/powershell.mjs";import{generateZshCompletion as r}from"./shells/zsh.mjs";const i=Object.freeze([`bash`,`zsh`,`fish`,`powershell`]);function a(i,a,o){switch(a){case`bash`:return e(i,o);case`zsh`:return r(i,o);case`fish`:return t(i,o);case`powershell`:return n(i,o)}}export{i as SHELLS,a as generateCompletion};
1
+ import { generateBashCompletion } from "./shells/bash.mjs";
2
+ import { generateFishCompletion } from "./shells/fish.mjs";
3
+ import { generatePowerShellCompletion } from "./shells/powershell.mjs";
4
+ import { generateZshCompletion } from "./shells/zsh.mjs";
5
+ //#region src/core/completion/index.ts
6
+ /**
7
+ * Implemented shell values as a frozen readonly non-empty tuple.
8
+ *
9
+ * Use this tuple for user-facing validation and shell selection UIs.
10
+ * It intentionally matches the shipped {@link Shell} union exactly so docs,
11
+ * help output, and completion generation advertise the same support surface.
12
+ *
13
+ * @see {@link Shell} for the union type matching these entries.
14
+ */
15
+ const SHELLS = Object.freeze([
16
+ "bash",
17
+ "zsh",
18
+ "fish",
19
+ "powershell"
20
+ ]);
21
+ /**
22
+ * Normalize a raw shell token — a bare name (`zsh`), a `$SHELL` path
23
+ * (`/bin/zsh`), or a Windows executable (`…\pwsh.exe`) — to a supported
24
+ * {@link Shell}, or `undefined` when it does not map to one.
25
+ *
26
+ * Mirrors the matching used by the `completions` subcommand so the subcommand
27
+ * and the eager `--completions <shell>` flag accept exactly the same inputs.
28
+ *
29
+ * @param raw - User-supplied shell token or interpreter path.
30
+ * @returns The resolved {@link Shell}, or `undefined` if unrecognized.
31
+ */
32
+ function normalizeShell(raw) {
33
+ const segments = raw.split(/[\\/]/);
34
+ const name = (segments[segments.length - 1] ?? raw).replace(/\.(?:exe|cmd|bat)$/i, "");
35
+ if (name === "pwsh") return "powershell";
36
+ return SHELLS.includes(name) ? name : void 0;
37
+ }
38
+ /**
39
+ * Auto-detect the current shell from environment variables.
40
+ *
41
+ * Resolution order:
42
+ * 1. `$SHELL` parsed as an interpreter path (`/bin/zsh` → `zsh`). It wins when
43
+ * it names a shell we support — a pwsh session launched from bash inherits
44
+ * `SHELL=/bin/bash`, and the login shell the user opted into should be
45
+ * honored over the fallback.
46
+ * 2. Otherwise, the presence of `$PSModulePath` signals PowerShell. PowerShell
47
+ * never sets `$SHELL` on any platform but always exports `PSModulePath` for
48
+ * module discovery, so it is the reliable pwsh signal.
49
+ *
50
+ * Returns `undefined` when no signal resolves, leaving the caller to ask the
51
+ * user for an explicit shell.
52
+ *
53
+ * @param env - Environment record (e.g. `process.env` or an adapter's env).
54
+ * @returns The detected {@link Shell}, or `undefined`.
55
+ */
56
+ function detectShell(env) {
57
+ const shellVar = env.SHELL;
58
+ const fromShell = shellVar !== void 0 && shellVar !== "" ? normalizeShell(shellVar) : void 0;
59
+ if (fromShell !== void 0) return fromShell;
60
+ if (env.PSModulePath !== void 0) return "powershell";
61
+ }
62
+ /**
63
+ * Generate a completion script for the given shell.
64
+ *
65
+ * This is the primary completion entrypoint for most consumers. Pass a CLI
66
+ * schema and target shell, then write the returned script to a file or source
67
+ * it directly from the command line.
68
+ *
69
+ * @param schema - The CLI schema describing commands, flags, and args.
70
+ * @param shell - Target shell.
71
+ * @param options - Optional generator configuration such as function naming
72
+ * and root default-command completion behavior.
73
+ * @returns A complete shell completion script as a string.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * const script = generateCompletion(app.schema, 'bash');
78
+ * // e.g. source <(mycli completions bash)
79
+ * ```
80
+ */
81
+ function generateCompletion(schema, shell, options) {
82
+ switch (shell) {
83
+ case "bash": return generateBashCompletion(schema, options);
84
+ case "zsh": return generateZshCompletion(schema, options);
85
+ case "fish": return generateFishCompletion(schema, options);
86
+ case "powershell": return generatePowerShellCompletion(schema, options);
87
+ }
88
+ }
89
+ //#endregion
90
+ export { SHELLS, detectShell, generateBashCompletion, generateCompletion, generateFishCompletion, generatePowerShellCompletion, generateZshCompletion, normalizeShell };
@@ -1,6 +1,5 @@
1
1
  import { CompletionOptions } from "./shared.mjs";
2
2
  import { CLISchema } from "../../cli/index.mjs";
3
-
4
3
  //#region src/core/completion/shells/bash.d.ts
5
4
  /**
6
5
  * Generate a bash completion script for the CLI.
@@ -1,3 +1,308 @@
1
- import{getFlagAliasNames as e}from"../../schema/flag.mjs";import{quoteShellArg as t,resolveRootCompletionSurface as n,sanitizeShellIdentifier as r,versionTag as i,walkCommandTree as a}from"./shared.mjs";function o(e,o){let l=`_${r(o?.functionPrefix??e.name)}_completions`,u=n(e,o?.rootMode),p=u.visibleCommands,v=a(p),y=v.reduce((e,t)=>Math.max(e,t.path.length),0),b=[];if(b.push(`#!/usr/bin/env bash`),b.push(`# Bash completion for ${e.name}`),b.push(`# Generated by ${i()}`),b.push(`#`),b.push(`# Usage: source <(${e.name} completions bash)`),b.push(`#`),b.push(`# Install permanently:`),b.push(`# ${e.name} completions bash > /etc/bash_completion.d/${e.name} # Linux`),b.push(`# ${e.name} completions bash > $(brew --prefix)/etc/bash_completion.d/${e.name} # macOS`),b.push(`#`),b.push(`# Requires: bash-completion (provides _init_completion)`),b.push(``),b.push(`${l}() {`),b.push(` local cur prev words cword`),b.push(` _init_completion || return`),b.push(``),p.length>0){let e=v.filter(e=>e.children.length>0),t=h(v,u);b.push(` # Build subcommand path by scanning COMP_WORDS`),b.push(` local subcmd_path=""`),b.push(` local i`),b.push(` for ((i = 1; i < cword; i++)); do`),b.push(' case "${words[i]}" in'),b.push(` --) break ;;`),t.length>0&&b.push(`\t\t\t${t}) ((i++)); continue ;;`),b.push(` -*) continue ;;`),b.push(` esac`),s(b,p,e,y),b.push(` done`),b.push(``),b.push(` # Complete flags and subcommands for the active path`),b.push(` case "$subcmd_path" in`);for(let e of v){let t=e.path.join(` `),n=e.mergedFlags,r=_(n),i=m(n),a=g(e.children.flatMap(e=>[e.name,...e.aliases])).map(d).join(` `),o=i.split(` `).filter(Boolean).map(d).join(` `),s=a.length>0?`${a} ${o}`:o;if(b.push(`\t\t${c(t)})`),r.length>0){b.push(` local enum_flag=""`),b.push(` local enum_cur="$cur"`),b.push(` local enum_prefix=""`),b.push(` if [[ "$cur" == --*=* ]]; then`),b.push(' enum_flag="${cur%%=*}"'),b.push(' enum_cur="${cur#*=}"'),b.push(' enum_prefix="${cur%%=*}="'),b.push(` fi`),b.push(' case "${enum_flag:-$prev}" in');for(let e of r)b.push(`\t\t\t\t${e.flags})`),b.push(f(e.values,` `,`enum_cur`,`enum_prefix`)),b.push(` return`),b.push(` ;;`);b.push(` *)`),b.push(` ;;`),b.push(` esac`)}b.push(`\t\t\tCOMPREPLY=($(compgen -W '${s.trim()}' -- "$cur"))`),b.push(` return`),b.push(` ;;`)}b.push(` *)`),b.push(` ;;`),b.push(` esac`),b.push(``);let n=m(u.rootFlags).split(` `).filter(Boolean),r=u.includeDefaultFlags?m(u.defaultFlags).split(` `).filter(Boolean):[],i=g([...p.flatMap(e=>[e.name,...e.aliases]),...n,...r]).map(d).join(` `);b.push(` # Root-level completions: visible root surface`),b.push(`\tCOMPREPLY=($(compgen -W '${i}' -- "$cur"))`)}else{let e=m(u.rootFlags);b.push(`\tCOMPREPLY=($(compgen -W '${e}' -- "$cur"))`)}return b.push(`}`),b.push(``),b.push(`complete -F ${l} ${t(e.name)}`),b.push(``),b.join(`
2
- `)}function s(e,t,n,r){e.push(` case "$subcmd_path" in`),e.push(` '')`),e.push(' case "${words[i]}" in');for(let n of t){let t=[n.name,...n.aliases].map(l);e.push(`\t\t\t\t\t${t.join(`|`)})`),e.push(`\t\t\t\t\t\tsubcmd_path="${u(n.name)}"`),e.push(` ;;`)}if(e.push(` esac`),e.push(` ;;`),r>1)for(let t of n){let n=t.path.join(` `);e.push(`\t\t\t${c(n)})`),e.push(' case "${words[i]}" in');for(let n of t.children){let t=[n.name,...n.aliases].map(l);e.push(`\t\t\t\t\t${t.join(`|`)})`),e.push(`\t\t\t\t\t\tsubcmd_path="$subcmd_path ${u(n.name)}"`),e.push(` ;;`)}e.push(` esac`),e.push(` ;;`)}e.push(` esac`)}function c(e){return e.includes(` `)?`"${u(e)}"`:l(e)}function l(e){return/^[a-zA-Z0-9_\-.]+$/.test(e)?e:e.replace(/\\/g,`\\\\`).replace(/'/g,`\\'`).replace(/"/g,`\\"`).replace(/\|/g,`\\|`).replace(/\*/g,`\\*`).replace(/\?/g,`\\?`).replace(/\[/g,`\\[`).replace(/\]/g,`\\]`).replace(/\(/g,`\\(`).replace(/\)/g,`\\)`).replace(/\{/g,`\\{`).replace(/\}/g,`\\}`).replace(/\+/g,`\\+`).replace(/!/g,`\\!`).replace(/@/g,`\\@`).replace(/#/g,`\\#`).replace(/~/g,`\\~`).replace(/\s/g,e=>e===` `?`\\ `:`\\${e}`)}function u(e){return e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`).replace(/\$/g,`\\$`).replace(/`/g,"\\`")}function d(e){return/^[a-zA-Z0-9_\-.]+$/.test(e)?e:e.replace(/'/g,`'\\''`)}function f(e,t,n=`cur`,r){let i=[];if(e.some(e=>!/^[a-zA-Z0-9_\-.]+$/.test(e))){let r=e.map(p).join(`\\n`);i.push(`${t}local IFS=$'\\n'`),i.push(`${t}COMPREPLY=($(compgen -W $'${r}' -- "$${n}"))`)}else i.push(`${t}COMPREPLY=($(compgen -W '${e.join(` `)}' -- "$${n}"))`);return r!==void 0&&(i.push(`${t}if [[ -n "$${r}" && "\${#COMPREPLY[@]}" -gt 0 ]]; then`),i.push(`${t}\tlocal i`),i.push(`${t}\tfor ((i = 0; i < \${#COMPREPLY[@]}; i++)); do`),i.push(`${t}\t\tCOMPREPLY[i]="$${r}\${COMPREPLY[i]}"`),i.push(`${t}\tdone`),i.push(`${t}fi`)),i.join(`
3
- `)}function p(e){return e.replace(/\\/g,`\\\\`).replace(/'/g,`\\'`).replace(/\n/g,`\\n`)}function m(t){let n=[];for(let[r,i]of Object.entries(t)){n.push(`--${r}`);for(let t of e(i))n.push(t.length===1?`-${t}`:`--${t}`)}return n.join(` `)}function h(t,n){let r=new Set,i=t=>{for(let[n,i]of Object.entries(t))if(i.kind!==`boolean`){r.add(`--${n}`);for(let t of e(i,{includeHidden:!0}))r.add(t.length===1?`-${t}`:`--${t}`)}};for(let e of t)i(e.mergedFlags);return i(n.rootFlags),n.includeDefaultFlags&&i(n.defaultFlags),[...r].map(l).join(`|`)}function g(e){return[...new Set(e)]}function _(t){let n=[];for(let[r,i]of Object.entries(t)){if(i.kind!==`enum`||i.enumValues===void 0)continue;let t=[`--${r}`,...e(i,{includeHidden:!0}).map(e=>e.length===1?`-${e}`:`--${e}`)].map(l);n.push({flags:t.join(`|`),values:i.enumValues})}return n}export{o as generateBashCompletion};
1
+ import { getFlagAliasNames } from "../../schema/flag.mjs";
2
+ import { flagExpectsValue } from "../../parse/index.mjs";
3
+ import { getVisibleNegatedName, quoteShellArg, resolveRootCompletionSurface, sanitizeShellIdentifier, versionTag, walkCommandTree } from "./shared.mjs";
4
+ //#region src/core/completion/shells/bash.ts
5
+ /**
6
+ * Generate a bash completion script for the CLI.
7
+ *
8
+ * Use this when you want a bash-specific script directly rather than routing
9
+ * through {@link generateCompletion}. The generated script expects the
10
+ * `bash-completion` package because it relies on `_init_completion`.
11
+ *
12
+ * @param schema - The CLI schema.
13
+ * @param options - Optional generator configuration.
14
+ * @returns A complete bash completion script.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const script = generateBashCompletion(app.schema);
19
+ * // install with: mycli completions bash > /etc/bash_completion.d/mycli
20
+ * ```
21
+ */
22
+ function generateBashCompletion(schema, options) {
23
+ const funcName = `_${sanitizeShellIdentifier(options?.functionPrefix ?? schema.name)}_completions`;
24
+ const rootSurface = resolveRootCompletionSurface(schema, options?.rootMode);
25
+ const visibleCommands = rootSurface.visibleCommands;
26
+ const nodes = walkCommandTree(visibleCommands);
27
+ const maxDepth = nodes.reduce((max, n) => Math.max(max, n.path.length), 0);
28
+ const lines = [];
29
+ lines.push("#!/usr/bin/env bash");
30
+ lines.push(`# Bash completion for ${schema.name}`);
31
+ lines.push(`# Generated by ${versionTag()}`);
32
+ lines.push("#");
33
+ lines.push(`# Usage: source <(${schema.name} completions bash)`);
34
+ lines.push("#");
35
+ lines.push("# Install permanently:");
36
+ lines.push(`# ${schema.name} completions bash > /etc/bash_completion.d/${schema.name} # Linux`);
37
+ lines.push(`# ${schema.name} completions bash > $(brew --prefix)/etc/bash_completion.d/${schema.name} # macOS`);
38
+ lines.push("#");
39
+ lines.push("# Requires: bash-completion (provides _init_completion)");
40
+ lines.push("");
41
+ lines.push(`${funcName}() {`);
42
+ lines.push(`\tlocal cur prev words cword`);
43
+ lines.push(`\t_init_completion || return`);
44
+ lines.push("");
45
+ if (visibleCommands.length > 0) {
46
+ const groupNodes = nodes.filter((n) => n.children.length > 0);
47
+ const valueFlagPattern = collectValueFlagPattern(nodes, rootSurface);
48
+ lines.push(" # Build subcommand path by scanning COMP_WORDS");
49
+ lines.push(" local subcmd_path=\"\"");
50
+ lines.push(" local i");
51
+ lines.push(" for ((i = 1; i < cword; i++)); do");
52
+ lines.push(" case \"${words[i]}\" in");
53
+ lines.push(" --) break ;;");
54
+ if (valueFlagPattern.length > 0) lines.push(`\t\t\t${valueFlagPattern}) ((i++)); continue ;;`);
55
+ lines.push(" -*) continue ;;");
56
+ lines.push(" esac");
57
+ emitBashPathDetection(lines, visibleCommands, groupNodes, maxDepth);
58
+ lines.push(" done");
59
+ lines.push("");
60
+ lines.push(" # Complete flags and subcommands for the active path");
61
+ lines.push(" case \"$subcmd_path\" in");
62
+ for (const node of nodes) {
63
+ const pathKey = node.path.join(" ");
64
+ const mergedFlags = node.mergedFlags;
65
+ const enumCases = collectEnumCasesFromFlags(mergedFlags);
66
+ const flagWords = collectFlagWords(mergedFlags);
67
+ const childNames = dedupeWords(node.children.flatMap((child) => [child.name, ...child.aliases])).map(escapeForSingleQuote).join(" ");
68
+ const escapedFlagWords = flagWords.split(" ").filter(Boolean).map(escapeForSingleQuote).join(" ");
69
+ const completionWords = childNames.length > 0 ? `${childNames} ${escapedFlagWords}` : escapedFlagWords;
70
+ lines.push(`\t\t${quoteShellCasePattern(pathKey)})`);
71
+ if (enumCases.length > 0) {
72
+ lines.push(" local enum_flag=\"\"");
73
+ lines.push(" local enum_cur=\"$cur\"");
74
+ lines.push(" local enum_prefix=\"\"");
75
+ lines.push(" if [[ \"$cur\" == --*=* ]]; then");
76
+ lines.push(`\t\t\t\tenum_flag="\${cur%%=*}"`);
77
+ lines.push(`\t\t\t\tenum_cur="\${cur#*=}"`);
78
+ lines.push(`\t\t\t\tenum_prefix="\${cur%%=*}="`);
79
+ lines.push(" fi");
80
+ lines.push(`\t\t\tcase "\${enum_flag:-$prev}" in`);
81
+ for (const ec of enumCases) {
82
+ lines.push(`\t\t\t\t${ec.flags})`);
83
+ lines.push(formatBashEnumCompletion(ec.values, " ", "enum_cur", "enum_prefix"));
84
+ lines.push(" return");
85
+ lines.push(" ;;");
86
+ }
87
+ lines.push(" *)");
88
+ lines.push(" ;;");
89
+ lines.push(" esac");
90
+ }
91
+ lines.push(`\t\t\tCOMPREPLY=($(compgen -W '${completionWords.trim()}' -- "$cur"))`);
92
+ lines.push(" return");
93
+ lines.push(" ;;");
94
+ }
95
+ lines.push(" *)");
96
+ lines.push(" ;;");
97
+ lines.push(" esac");
98
+ lines.push("");
99
+ const rootFlagWords = collectFlagWords(rootSurface.rootFlags).split(" ").filter(Boolean);
100
+ const defaultFlagWords = rootSurface.includeDefaultFlags ? collectFlagWords(rootSurface.defaultFlags).split(" ").filter(Boolean) : [];
101
+ const completionWords = dedupeWords([
102
+ ...visibleCommands.flatMap((command) => [command.name, ...command.aliases]),
103
+ ...rootFlagWords,
104
+ ...defaultFlagWords
105
+ ]).map(escapeForSingleQuote).join(" ");
106
+ lines.push(`\t# Root-level completions: visible root surface`);
107
+ lines.push(`\tCOMPREPLY=($(compgen -W '${completionWords}' -- "$cur"))`);
108
+ } else {
109
+ const globalFlags = collectFlagWords(rootSurface.rootFlags);
110
+ lines.push(`\tCOMPREPLY=($(compgen -W '${globalFlags}' -- "$cur"))`);
111
+ }
112
+ lines.push("}");
113
+ lines.push("");
114
+ lines.push(`complete -F ${funcName} ${quoteShellArg(schema.name)}`);
115
+ lines.push("");
116
+ return lines.join("\n");
117
+ }
118
+ /**
119
+ * Emit bash case branches for progressive subcommand path detection.
120
+ *
121
+ * For each nesting depth, generates a `case "$subcmd_path"` that matches
122
+ * the current accumulated path and extends it when a matching child command
123
+ * name is found in COMP_WORDS.
124
+ *
125
+ * @internal
126
+ */
127
+ function emitBashPathDetection(lines, visibleCommands, groupNodes, maxDepth) {
128
+ lines.push(`\t\tcase "$subcmd_path" in`);
129
+ lines.push(`\t\t\t'')`);
130
+ lines.push(`\t\t\t\tcase "\${words[i]}" in`);
131
+ for (const command of visibleCommands) {
132
+ const patterns = [command.name, ...command.aliases].map(escapeBashCasePatternValue);
133
+ lines.push(`\t\t\t\t\t${patterns.join("|")})`);
134
+ lines.push(`\t\t\t\t\t\tsubcmd_path="${escapeBashDoubleQuotedValue(command.name)}"`);
135
+ lines.push(`\t\t\t\t\t\t;;`);
136
+ }
137
+ lines.push(`\t\t\t\tesac`);
138
+ lines.push(`\t\t\t\t;;`);
139
+ if (maxDepth > 1) for (const node of groupNodes) {
140
+ const pathKey = node.path.join(" ");
141
+ lines.push(`\t\t\t${quoteShellCasePattern(pathKey)})`);
142
+ lines.push(`\t\t\t\tcase "\${words[i]}" in`);
143
+ for (const child of node.children) {
144
+ const childPatterns = [child.name, ...child.aliases].map(escapeBashCasePatternValue);
145
+ lines.push(`\t\t\t\t\t${childPatterns.join("|")})`);
146
+ lines.push(`\t\t\t\t\t\tsubcmd_path="$subcmd_path ${escapeBashDoubleQuotedValue(child.name)}"`);
147
+ lines.push(`\t\t\t\t\t\t;;`);
148
+ }
149
+ lines.push(`\t\t\t\tesac`);
150
+ lines.push(`\t\t\t\t;;`);
151
+ }
152
+ lines.push(`\t\tesac`);
153
+ }
154
+ /**
155
+ * Escape a string for use as a bash `case` pattern.
156
+ *
157
+ * Escapes glob metacharacters so arbitrary command, alias, and flag names are
158
+ * matched literally rather than being interpreted as shell patterns.
159
+ *
160
+ * @internal
161
+ */
162
+ function quoteShellCasePattern(pattern) {
163
+ if (pattern.includes(" ")) return `"${escapeBashDoubleQuotedValue(pattern)}"`;
164
+ return escapeBashCasePatternValue(pattern);
165
+ }
166
+ /**
167
+ * Escape a value for use in a bash `case` pattern.
168
+ *
169
+ * Escapes glob metacharacters so arbitrary command and flag names are matched
170
+ * literally rather than being interpreted as shell patterns.
171
+ *
172
+ * @internal
173
+ */
174
+ function escapeBashCasePatternValue(value) {
175
+ if (/^[a-zA-Z0-9_\-.]+$/.test(value)) return value;
176
+ return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/"/g, "\\\"").replace(/\|/g, "\\|").replace(/\*/g, "\\*").replace(/\?/g, "\\?").replace(/\[/g, "\\[").replace(/\]/g, "\\]").replace(/\(/g, "\\(").replace(/\)/g, "\\)").replace(/\{/g, "\\{").replace(/\}/g, "\\}").replace(/\+/g, "\\+").replace(/!/g, "\\!").replace(/@/g, "\\@").replace(/#/g, "\\#").replace(/~/g, "\\~").replace(/\s/g, (match) => match === " " ? "\\ " : `\\${match}`);
177
+ }
178
+ /**
179
+ * Escape a value for interpolation inside a bash double-quoted string.
180
+ *
181
+ * @internal
182
+ */
183
+ function escapeBashDoubleQuotedValue(value) {
184
+ return value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\$/g, "\\$").replace(/`/g, "\\`");
185
+ }
186
+ /**
187
+ * Escape a string for safe embedding inside an existing single-quoted
188
+ * shell literal (e.g. inside `compgen -W '...'`).
189
+ *
190
+ * Uses the `'\''` idiom: close quote, escaped literal quote, reopen
191
+ * quote. Returns the input unchanged when it consists only of shell-safe
192
+ * characters, which covers the vast majority of flag/command names.
193
+ *
194
+ * @internal
195
+ */
196
+ function escapeForSingleQuote(value) {
197
+ if (/^[a-zA-Z0-9_\-.]+$/.test(value)) return value;
198
+ return value.replace(/'/g, "'\\''");
199
+ }
200
+ /**
201
+ * Format a COMPREPLY line for bash enum value completion.
202
+ *
203
+ * When all values are simple (no whitespace or special chars), emits a
204
+ * standard `compgen -W 'v1 v2' -- "$cur"` line.
205
+ *
206
+ * When any value contains whitespace, single quotes, or backslashes,
207
+ * uses `IFS=$'\n'` with `$'...'` quoting to split on newlines
208
+ * instead of spaces, handling all special characters correctly.
209
+ *
210
+ * @internal
211
+ */
212
+ function formatBashEnumCompletion(values, indent, currentVar = "cur", prefixVar) {
213
+ const lines = [];
214
+ if (values.some((v) => !/^[a-zA-Z0-9_\-.]+$/.test(v))) {
215
+ const joined = values.map(escapeBashDollarQuote).join("\\n");
216
+ lines.push(`${indent}local IFS=$'\\n'`);
217
+ lines.push(`${indent}COMPREPLY=($(compgen -W $'${joined}' -- "$${currentVar}"))`);
218
+ } else lines.push(`${indent}COMPREPLY=($(compgen -W '${values.join(" ")}' -- "$${currentVar}"))`);
219
+ if (prefixVar !== void 0) {
220
+ lines.push(`${indent}if [[ -n "$${prefixVar}" && "\${#COMPREPLY[@]}" -gt 0 ]]; then`);
221
+ lines.push(`${indent}\tlocal i`);
222
+ lines.push(`${indent}\tfor ((i = 0; i < \${#COMPREPLY[@]}; i++)); do`);
223
+ lines.push(`${indent}\t\tCOMPREPLY[i]="$${prefixVar}\${COMPREPLY[i]}"`);
224
+ lines.push(`${indent}\tdone`);
225
+ lines.push(`${indent}fi`);
226
+ }
227
+ return lines.join("\n");
228
+ }
229
+ /**
230
+ * Escape a single enum value for use inside a bash `$'...'` string.
231
+ *
232
+ * In `$'...'` context:
233
+ * - `\\` → literal backslash
234
+ * - `\'` → literal single quote
235
+ * - `\n` → newline (used as IFS delimiter, so must be escaped in values)
236
+ *
237
+ * Values that are simple identifiers pass through unescaped.
238
+ *
239
+ * @internal
240
+ */
241
+ function escapeBashDollarQuote(value) {
242
+ return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n");
243
+ }
244
+ /**
245
+ * Collect all `--flag` words (long + short aliases + visible negated
246
+ * spellings) for a flag record.
247
+ *
248
+ * @internal
249
+ */
250
+ function collectFlagWords(flags) {
251
+ const words = [];
252
+ for (const [name, schema] of Object.entries(flags)) {
253
+ words.push(`--${name}`);
254
+ for (const alias of getFlagAliasNames(schema)) words.push(alias.length === 1 ? `-${alias}` : `--${alias}`);
255
+ const negated = getVisibleNegatedName(name, schema);
256
+ if (negated !== void 0) words.push(`--${negated}`);
257
+ }
258
+ return words.join(" ");
259
+ }
260
+ /**
261
+ * Collect all value-taking flag forms across every node and root surface,
262
+ * deduplicated and escaped for use in a bash `case` pattern.
263
+ *
264
+ * Returns a `|`-joined pattern like `--schema|-s|--output|-o` that the
265
+ * subcommand-path walker uses to skip the operand following a value-taking
266
+ * flag. Returns an empty string when no value-taking flags exist.
267
+ *
268
+ * @internal
269
+ */
270
+ function collectValueFlagPattern(nodes, rootSurface) {
271
+ const forms = /* @__PURE__ */ new Set();
272
+ const addFlags = (flags) => {
273
+ for (const [name, schema] of Object.entries(flags)) {
274
+ if (!flagExpectsValue(schema)) continue;
275
+ forms.add(`--${name}`);
276
+ for (const alias of getFlagAliasNames(schema, { includeHidden: true })) forms.add(alias.length === 1 ? `-${alias}` : `--${alias}`);
277
+ }
278
+ };
279
+ for (const node of nodes) addFlags(node.mergedFlags);
280
+ addFlags(rootSurface.rootFlags);
281
+ if (rootSurface.includeDefaultFlags) addFlags(rootSurface.defaultFlags);
282
+ return [...forms].map(escapeBashCasePatternValue).join("|");
283
+ }
284
+ function dedupeWords(words) {
285
+ return [...new Set(words)];
286
+ }
287
+ /**
288
+ * Collect enum flag cases from a merged flag record.
289
+ *
290
+ * Each enum flag produces one case entry with its long/short flag forms
291
+ * as the pattern and enum values as the completions.
292
+ *
293
+ * @internal
294
+ */
295
+ function collectEnumCasesFromFlags(flags) {
296
+ const cases = [];
297
+ for (const [name, schema] of Object.entries(flags)) {
298
+ if (schema.kind !== "enum" || schema.enumValues === void 0) continue;
299
+ const flagForms = [`--${name}`, ...getFlagAliasNames(schema, { includeHidden: true }).map((alias) => alias.length === 1 ? `-${alias}` : `--${alias}`)].map(escapeBashCasePatternValue);
300
+ cases.push({
301
+ flags: flagForms.join("|"),
302
+ values: schema.enumValues
303
+ });
304
+ }
305
+ return cases;
306
+ }
307
+ //#endregion
308
+ export { generateBashCompletion };
@@ -1,6 +1,5 @@
1
1
  import { CompletionOptions } from "./shared.mjs";
2
2
  import { CLISchema } from "../../cli/index.mjs";
3
-
4
3
  //#region src/core/completion/shells/fish.d.ts
5
4
  /**
6
5
  * Generate a fish completion script for the CLI.
@@ -1,2 +1,162 @@
1
- import{getFlagAliasNames as e}from"../../schema/flag.mjs";import{quoteShellArg as t,resolveRootCompletionSurface as n,sanitizeShellIdentifier as r,versionTag as i,walkCommandTree as a}from"./shared.mjs";function o(e,o){let u=`__${r(o?.functionPrefix??e.name)}_completions`,d=`${u}_path`,f=`${u}_path_is`,p=n(e,o?.rootMode),m=p.visibleCommands,h=a(m),g=t(e.name),_=[];_.push(`# Fish completion for ${e.name}`),_.push(`# Generated by ${i()}`),_.push(`#`),_.push(`# Usage: source (${e.name} completions fish | psub)`),_.push(`#`),_.push(`# Install permanently:`),_.push(`# ${e.name} completions fish > ~/.config/fish/completions/${e.name}.fish`),_.push(``),_.push(`complete -c ${g} -f`),_.push(``),s(_,d,f,m,h,p),c(_,g,f,``,m),l(_,g,f,``,p.rootFlags),p.includeDefaultFlags&&l(_,g,f,``,p.defaultFlags);for(let e of h){let t=e.path.join(` `);c(_,g,f,t,e.children),l(_,g,f,t,e.mergedFlags)}return _.join(`
2
- `)}function s(e,n,r,i,a,o){let s=u(a,o);e.push(`function ${n}`),e.push(` set -l words (commandline -opc)`),e.push(` if test (count $words) -gt 0`),e.push(` set -e words[1]`),e.push(` end`),e.push(` set -l subcmd_path ''`),e.push(` set -l expect_value 0`),e.push(` for word in $words`),e.push(` if test $expect_value -eq 1`),e.push(` set expect_value 0`),e.push(` continue`),e.push(` end`),e.push(` switch $word`),e.push(` case '--'`),e.push(` break`),s.exact.length>0&&(e.push(`\t\t\tcase ${s.exact.map(t).join(` `)}`),e.push(` set expect_value 1`),e.push(` continue`)),s.prefix.length>0&&(e.push(`\t\t\tcase ${s.prefix.map(t).join(` `)}`),e.push(` continue`)),e.push(` case '-*'`),e.push(` continue`),e.push(` end`),e.push(` switch "$subcmd_path|$word"`);for(let n of i)for(let r of[n.name,...n.aliases])e.push(`\t\t\tcase ${t(`|${r}`)}`),e.push(`\t\t\t\tset subcmd_path ${t(n.name)}`);for(let n of a)for(let r of n.children)for(let i of[r.name,...r.aliases])e.push(`\t\t\tcase ${t(`${n.path.join(` `)}|${i}`)}`),e.push(`\t\t\t\tset subcmd_path ${t(`${n.path.join(` `)} ${r.name}`)}`);e.push(` case '*'`),e.push(` break`),e.push(` end`),e.push(` end`),e.push(` printf '%s\\n' "$subcmd_path"`),e.push(`end`),e.push(``),e.push(`function ${r}`),e.push(` set -l expected "$argv[1]"`),e.push(`\ttest (${n}) = "$expected"`),e.push(`end`),e.push(``)}function c(e,n,r,i,a){let o=d(r,i);for(let r of a){let i=t(r.description??r.name);for(let a of[r.name,...r.aliases])e.push(`complete -c ${n} -n "${o}" -a ${t(a)} -d ${i}`)}a.length>0&&e.push(``)}function l(n,r,i,a,o){let s=d(i,a);for(let[i,a]of Object.entries(o)){let o=[`complete -c ${r}`,`-n "${s}"`];for(let n of e(a,{kind:`short`}))o.push(`-s ${t(n)}`);o.push(`-l ${t(i)}`);for(let n of e(a,{kind:`long`}))o.push(`-l ${t(n)}`);if(o.push(`-d ${t(a.description??i)}`),a.kind!==`boolean`&&o.push(`-r`,`-f`),a.kind===`enum`&&a.enumValues!==void 0){for(let e of a.enumValues)n.push([...o,`-a ${t(e)}`].join(` `));continue}n.push(o.join(` `))}Object.keys(o).length>0&&n.push(``)}function u(t,n){let r=new Set,i=new Set,a=[n.rootFlags,...n.includeDefaultFlags?[n.defaultFlags]:[],...t.map(e=>e.mergedFlags)];for(let t of a)for(let[n,a]of Object.entries(t))if(a.kind!==`boolean`){r.add(`--${n}`),i.add(`--${n}=*`);for(let t of e(a,{kind:`short`,includeHidden:!0}))r.add(`-${t}`),i.add(`-${t}*`);for(let t of e(a,{kind:`long`,includeHidden:!0}))r.add(`--${t}`),i.add(`--${t}=*`)}return{exact:[...r],prefix:[...i]}}function d(e,n){return`${e} ${t(n)}`}export{o as generateFishCompletion};
1
+ import { getFlagAliasNames } from "../../schema/flag.mjs";
2
+ import { flagExpectsValue } from "../../parse/index.mjs";
3
+ import { getVisibleNegatedName, quoteShellArg, resolveRootCompletionSurface, sanitizeShellIdentifier, versionTag, walkCommandTree } from "./shared.mjs";
4
+ //#region src/core/completion/shells/fish.ts
5
+ /**
6
+ * Generate a fish completion script for the CLI.
7
+ *
8
+ * @param schema - The CLI schema.
9
+ * @param options - Optional generator configuration.
10
+ * @returns A complete fish completion script.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const script = generateFishCompletion(app.schema);
15
+ * // install with: mycli completions fish > ~/.config/fish/completions/mycli.fish
16
+ * ```
17
+ */
18
+ function generateFishCompletion(schema, options) {
19
+ const helperPrefix = `__${sanitizeShellIdentifier(options?.functionPrefix ?? schema.name)}_completions`;
20
+ const pathFuncName = `${helperPrefix}_path`;
21
+ const pathIsFuncName = `${helperPrefix}_path_is`;
22
+ const rootSurface = resolveRootCompletionSurface(schema, options?.rootMode);
23
+ const visibleCommands = rootSurface.visibleCommands;
24
+ const nodes = walkCommandTree(visibleCommands);
25
+ const commandName = quoteShellArg(schema.name);
26
+ const lines = [];
27
+ lines.push(`# Fish completion for ${schema.name}`);
28
+ lines.push(`# Generated by ${versionTag()}`);
29
+ lines.push("#");
30
+ lines.push(`# Usage: source (${schema.name} completions fish | psub)`);
31
+ lines.push("#");
32
+ lines.push("# Install permanently:");
33
+ lines.push(`# ${schema.name} completions fish > ~/.config/fish/completions/${schema.name}.fish`);
34
+ lines.push("");
35
+ lines.push(`complete -c ${commandName} -f`);
36
+ lines.push("");
37
+ emitFishPathHelpers(lines, pathFuncName, pathIsFuncName, visibleCommands, nodes, rootSurface);
38
+ appendFishCommandCompletions(lines, commandName, pathIsFuncName, "", visibleCommands);
39
+ appendFishFlagCompletions(lines, commandName, pathIsFuncName, "", rootSurface.rootFlags);
40
+ if (rootSurface.includeDefaultFlags) appendFishFlagCompletions(lines, commandName, pathIsFuncName, "", rootSurface.defaultFlags);
41
+ for (const node of nodes) {
42
+ const path = node.path.join(" ");
43
+ appendFishCommandCompletions(lines, commandName, pathIsFuncName, path, node.children);
44
+ appendFishFlagCompletions(lines, commandName, pathIsFuncName, path, node.mergedFlags);
45
+ }
46
+ return lines.join("\n");
47
+ }
48
+ function emitFishPathHelpers(lines, pathFuncName, pathIsFuncName, visibleCommands, nodes, rootSurface) {
49
+ const valueFlags = collectFishValueFlagPatterns(nodes, rootSurface);
50
+ lines.push(`function ${pathFuncName}`);
51
+ lines.push(" set -l words (commandline -opc)");
52
+ lines.push(" if test (count $words) -gt 0");
53
+ lines.push(" set -e words[1]");
54
+ lines.push(" end");
55
+ lines.push(" set -l subcmd_path ''");
56
+ lines.push(" set -l expect_value 0");
57
+ lines.push(" for word in $words");
58
+ lines.push(" if test $expect_value -eq 1");
59
+ lines.push(" set expect_value 0");
60
+ lines.push(" continue");
61
+ lines.push(" end");
62
+ lines.push(" switch $word");
63
+ lines.push(" case '--'");
64
+ lines.push(" break");
65
+ if (valueFlags.exact.length > 0) {
66
+ lines.push(`\t\t\tcase ${valueFlags.exact.map(quoteShellArg).join(" ")}`);
67
+ lines.push(" set expect_value 1");
68
+ lines.push(" continue");
69
+ }
70
+ if (valueFlags.prefix.length > 0) {
71
+ lines.push(`\t\t\tcase ${valueFlags.prefix.map(quoteShellArg).join(" ")}`);
72
+ lines.push(" continue");
73
+ }
74
+ lines.push(" case '-*'");
75
+ lines.push(" continue");
76
+ lines.push(" end");
77
+ lines.push(" switch \"$subcmd_path|$word\"");
78
+ for (const command of visibleCommands) for (const name of [command.name, ...command.aliases]) {
79
+ lines.push(`\t\t\tcase ${quoteShellArg(`|${name}`)}`);
80
+ lines.push(`\t\t\t\tset subcmd_path ${quoteShellArg(command.name)}`);
81
+ }
82
+ for (const node of nodes) for (const child of node.children) for (const name of [child.name, ...child.aliases]) {
83
+ lines.push(`\t\t\tcase ${quoteShellArg(`${node.path.join(" ")}|${name}`)}`);
84
+ lines.push(`\t\t\t\tset subcmd_path ${quoteShellArg(`${node.path.join(" ")} ${child.name}`)}`);
85
+ }
86
+ lines.push(" case '*'");
87
+ lines.push(" break");
88
+ lines.push(" end");
89
+ lines.push(" end");
90
+ lines.push(" printf '%s\\n' \"$subcmd_path\"");
91
+ lines.push("end");
92
+ lines.push("");
93
+ lines.push(`function ${pathIsFuncName}`);
94
+ lines.push(" set -l expected \"$argv[1]\"");
95
+ lines.push(`\ttest (${pathFuncName}) = "$expected"`);
96
+ lines.push("end");
97
+ lines.push("");
98
+ }
99
+ function appendFishCommandCompletions(lines, commandName, pathIsFuncName, path, commands) {
100
+ const condition = buildFishCondition(pathIsFuncName, path);
101
+ for (const command of commands) {
102
+ const description = quoteShellArg(command.description ?? command.name);
103
+ for (const name of [command.name, ...command.aliases]) lines.push(`complete -c ${commandName} -n "${condition}" -a ${quoteShellArg(name)} -d ${description}`);
104
+ }
105
+ if (commands.length > 0) lines.push("");
106
+ }
107
+ function appendFishFlagCompletions(lines, commandName, pathIsFuncName, path, flags) {
108
+ const condition = buildFishCondition(pathIsFuncName, path);
109
+ for (const [name, schema] of Object.entries(flags)) {
110
+ const parts = [`complete -c ${commandName}`, `-n "${condition}"`];
111
+ for (const alias of getFlagAliasNames(schema, { kind: "short" })) parts.push(`-s ${quoteShellArg(alias)}`);
112
+ parts.push(`-l ${quoteShellArg(name)}`);
113
+ for (const alias of getFlagAliasNames(schema, { kind: "long" })) parts.push(`-l ${quoteShellArg(alias)}`);
114
+ const negated = getVisibleNegatedName(name, schema);
115
+ if (negated !== void 0) parts.push(`-l ${quoteShellArg(negated)}`);
116
+ parts.push(`-d ${quoteShellArg(schema.description ?? name)}`);
117
+ if (flagExpectsValue(schema)) parts.push("-r", "-f");
118
+ if (schema.kind === "enum" && schema.enumValues !== void 0) {
119
+ for (const enumValue of schema.enumValues) lines.push([...parts, `-a ${quoteShellArg(enumValue)}`].join(" "));
120
+ continue;
121
+ }
122
+ lines.push(parts.join(" "));
123
+ }
124
+ if (Object.keys(flags).length > 0) lines.push("");
125
+ }
126
+ function collectFishValueFlagPatterns(nodes, rootSurface) {
127
+ const exact = /* @__PURE__ */ new Set();
128
+ const prefix = /* @__PURE__ */ new Set();
129
+ const flagSets = [
130
+ rootSurface.rootFlags,
131
+ ...rootSurface.includeDefaultFlags ? [rootSurface.defaultFlags] : [],
132
+ ...nodes.map((node) => node.mergedFlags)
133
+ ];
134
+ for (const flags of flagSets) for (const [name, schema] of Object.entries(flags)) {
135
+ if (!flagExpectsValue(schema)) continue;
136
+ exact.add(`--${name}`);
137
+ prefix.add(`--${name}=*`);
138
+ for (const alias of getFlagAliasNames(schema, {
139
+ kind: "short",
140
+ includeHidden: true
141
+ })) {
142
+ exact.add(`-${alias}`);
143
+ prefix.add(`-${alias}*`);
144
+ }
145
+ for (const alias of getFlagAliasNames(schema, {
146
+ kind: "long",
147
+ includeHidden: true
148
+ })) {
149
+ exact.add(`--${alias}`);
150
+ prefix.add(`--${alias}=*`);
151
+ }
152
+ }
153
+ return {
154
+ exact: [...exact],
155
+ prefix: [...prefix]
156
+ };
157
+ }
158
+ function buildFishCondition(pathIsFuncName, path) {
159
+ return `${pathIsFuncName} ${quoteShellArg(path)}`;
160
+ }
161
+ //#endregion
162
+ export { generateFishCompletion };
@@ -1,6 +1,5 @@
1
1
  import { CompletionOptions } from "./shared.mjs";
2
2
  import { CLISchema } from "../../cli/index.mjs";
3
-
4
3
  //#region src/core/completion/shells/powershell.d.ts
5
4
  /**
6
5
  * Generate a PowerShell completion script for the CLI.