@kjanat/dreamcli 2.5.0 → 3.0.0-rc.2

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