@kjanat/dreamcli 2.0.1 → 2.1.0

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.
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "pwsh-demo",
3
+ "version": "0.0.0-example",
4
+ "private": true,
5
+ "description": "PowerShell completion playground for dreamcli",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/kjanat/dreamcli.git",
9
+ "directory": "examples/pwsh-demo"
10
+ },
11
+ "type": "module",
12
+ "module": "src/main.ts",
13
+ "bin": {
14
+ "pwsh-demo": "src/main.ts"
15
+ },
16
+ "files": [
17
+ "src",
18
+ "README.md",
19
+ "smoke-powershell.ps1",
20
+ "pwsh-demo.cmd",
21
+ "!src/**/*.test.ts"
22
+ ],
23
+ "scripts": {
24
+ "build": "bun build src/main.ts --install --outdir=dist --format=esm --target=node --external @kjanat/dreamcli",
25
+ "smoke:powershell": "pwsh -NoLogo -NoProfile -ExecutionPolicy Bypass -File ./smoke-powershell.ps1",
26
+ "test": "bun test",
27
+ "typecheck": "tsc --noEmit"
28
+ },
29
+ "dependencies": {
30
+ "@kjanat/dreamcli": "link:@kjanat/dreamcli"
31
+ },
32
+ "devDependencies": {
33
+ "@types/bun": "catalog:",
34
+ "typescript": "catalog:"
35
+ },
36
+ "packageManager": "bun@1.3.12",
37
+ "engines": {
38
+ "bun": "^1.3"
39
+ },
40
+ "devEngines": {
41
+ "runtime": {
42
+ "name": "bun",
43
+ "version": "^1.3",
44
+ "onFail": "error"
45
+ }
46
+ }
47
+ }
@@ -0,0 +1,3 @@
1
+ @echo off
2
+ bun --cwd "%~dp0." src\main.ts %*
3
+ exit /b %errorlevel%
@@ -0,0 +1,218 @@
1
+ #!/usr/bin/env pwsh
2
+
3
+ <#
4
+ .SYNOPSIS
5
+ Runs an end-to-end smoke test for DreamCLI PowerShell completions.
6
+
7
+ .DESCRIPTION
8
+ Generates the `pwsh-demo` PowerShell completion script, registers it in the
9
+ current session, executes a set of completion scenarios with `TabExpansion2`,
10
+ and fails if expected completion values are missing or hidden values leak into
11
+ the result set.
12
+ #>
13
+
14
+ $ErrorActionPreference = 'Stop'
15
+ Set-StrictMode -Version Latest
16
+
17
+ $root = Split-Path -Parent $MyInvocation.MyCommand.Path
18
+ $env:PATH = "$root;$env:PATH"
19
+
20
+ $completionScript = & bun "$root/src/main.ts" completions powershell | Out-String
21
+ $completionExitCode = $LASTEXITCODE
22
+ if ($completionExitCode -ne 0) {
23
+ throw "Direct Bun completion generation failed with exit code $completionExitCode."
24
+ }
25
+ $completionScript = $completionScript.TrimEnd()
26
+ if ([string]::IsNullOrWhiteSpace($completionScript)) {
27
+ throw 'No completion script was generated.'
28
+ }
29
+
30
+ $nativeCompletionScript = & pwsh-demo completions powershell | Out-String
31
+ $nativeCompletionExitCode = $LASTEXITCODE
32
+ if ($nativeCompletionExitCode -ne 0) {
33
+ throw "Native pwsh-demo completion generation failed with exit code $nativeCompletionExitCode."
34
+ }
35
+ $nativeCompletionScript = $nativeCompletionScript.TrimEnd()
36
+ if ([string]::IsNullOrWhiteSpace($nativeCompletionScript)) {
37
+ throw 'The native pwsh-demo launcher did not generate a completion script.'
38
+ }
39
+
40
+ if ($nativeCompletionScript -ne $completionScript) {
41
+ throw 'The native pwsh-demo launcher generated a different completion script than the direct Bun invocation.'
42
+ }
43
+
44
+ $nativeCompletionScript | Out-String | Invoke-Expression
45
+
46
+ <#
47
+ .SYNOPSIS
48
+ Returns completion texts for a PowerShell input string.
49
+
50
+ .DESCRIPTION
51
+ Invokes `TabExpansion2` for the provided input and returns only the
52
+ `CompletionText` values so smoke assertions can compare the visible completion
53
+ surface.
54
+
55
+ .PARAMETER InputScript
56
+ The exact PowerShell command line to expand.
57
+
58
+ .OUTPUTS
59
+ System.String[]
60
+ #>
61
+ function Get-CompletionTexts {
62
+ param([string]$InputScript)
63
+
64
+ $expanded = TabExpansion2 -inputScript $InputScript -cursorColumn $InputScript.Length
65
+ return @($expanded.CompletionMatches | ForEach-Object CompletionText)
66
+ }
67
+
68
+ <#
69
+ .SYNOPSIS
70
+ Fails when expected completion values are missing.
71
+
72
+ .DESCRIPTION
73
+ Compares the expected completion values to the actual completion results and
74
+ throws when any expected value is absent.
75
+
76
+ .PARAMETER Label
77
+ The scenario label used in the failure message.
78
+
79
+ .PARAMETER Actual
80
+ The completion texts returned by the expansion.
81
+
82
+ .PARAMETER Expected
83
+ The completion texts that must be present.
84
+ #>
85
+ function Assert-Contains {
86
+ param(
87
+ [string]$Label,
88
+ [string[]]$Actual,
89
+ [string[]]$Expected
90
+ )
91
+
92
+ $missing = @($Expected | Where-Object { $_ -notin $Actual })
93
+ if ($missing.Count -gt 0) {
94
+ throw "$Label is missing expected completions: $($missing -join ', ')"
95
+ }
96
+ }
97
+
98
+ <#
99
+ .SYNOPSIS
100
+ Fails when excluded completion values appear in the results.
101
+
102
+ .DESCRIPTION
103
+ Compares the actual completion results to a list of values that must remain
104
+ hidden and throws when any excluded value is present.
105
+
106
+ .PARAMETER Label
107
+ The scenario label used in the failure message.
108
+
109
+ .PARAMETER Actual
110
+ The completion texts returned by the expansion.
111
+
112
+ .PARAMETER Excluded
113
+ The completion texts that must not appear.
114
+ #>
115
+ function Assert-Excludes {
116
+ param(
117
+ [string]$Label,
118
+ [string[]]$Actual,
119
+ [string[]]$Excluded
120
+ )
121
+
122
+ $unexpected = @($Excluded | Where-Object { $_ -in $Actual })
123
+ if ($unexpected.Count -gt 0) {
124
+ throw "$Label included completions that should stay hidden: $($unexpected -join ', ')"
125
+ }
126
+ }
127
+
128
+ $cases = @(
129
+ @{
130
+ Label = 'Command name completion'
131
+ Input = 'pwsh-demo de'
132
+ Expected = @('deploy')
133
+ Excluded = @('debug-dump')
134
+ },
135
+ @{
136
+ Label = 'Command alias completion'
137
+ Input = 'pwsh-demo sh'
138
+ Expected = @('ship')
139
+ Excluded = @()
140
+ },
141
+ @{
142
+ Label = 'Root-surface flag completion'
143
+ Input = 'pwsh-demo --pro'
144
+ Expected = @('--profile')
145
+ Excluded = @('--account')
146
+ },
147
+ @{
148
+ Label = 'Deploy flag value completion'
149
+ Input = 'pwsh-demo deploy --region '
150
+ Expected = @('us', 'eu', 'ap')
151
+ Excluded = @()
152
+ },
153
+ @{
154
+ Label = 'Deploy flag value prefix completion'
155
+ Input = 'pwsh-demo deploy --region e'
156
+ Expected = @('eu')
157
+ Excluded = @('us', 'ap')
158
+ },
159
+ @{
160
+ Label = 'Deploy inline flag value completion'
161
+ Input = 'pwsh-demo deploy --region=e'
162
+ Expected = @('--region=eu')
163
+ Excluded = @('--region=us', '--region=ap')
164
+ },
165
+ @{
166
+ Label = 'Deploy strategy value completion'
167
+ Input = 'pwsh-demo ship --strategy '
168
+ Expected = @('rolling', 'blue-green', 'canary')
169
+ Excluded = @()
170
+ },
171
+ @{
172
+ Label = 'Status view value completion'
173
+ Input = 'pwsh-demo st --view '
174
+ Expected = @('summary', 'full', 'json')
175
+ Excluded = @()
176
+ },
177
+ @{
178
+ Label = 'Root default-command value completion'
179
+ Input = 'pwsh-demo --profile o'
180
+ Expected = @('ops')
181
+ Excluded = @('o', 'open')
182
+ },
183
+ @{
184
+ Label = 'Quoted root default-command value completion'
185
+ Input = 'pwsh-demo --profile q'
186
+ Expected = @("'qa ops'", "'qa''s'")
187
+ Excluded = @('qa ops', "qa's")
188
+ },
189
+ @{
190
+ Label = 'Quoted inline root default-command value completion'
191
+ Input = 'pwsh-demo --profile=q'
192
+ Expected = @("--profile='qa ops'", "--profile='qa''s'")
193
+ Excluded = @('--profile=qa ops', "--profile=qa's")
194
+ },
195
+ @{
196
+ Label = 'Nested subcommand completion'
197
+ Input = 'pwsh-demo config se'
198
+ Expected = @('set')
199
+ Excluded = @()
200
+ },
201
+ @{
202
+ Label = 'Option separator stops flag completion'
203
+ Input = 'pwsh-demo deploy -- --re'
204
+ Expected = @()
205
+ Excluded = @('--region', '--approval', '-r')
206
+ }
207
+ )
208
+
209
+ foreach ($case in $cases) {
210
+ $actual = Get-CompletionTexts -InputScript $case.Input
211
+ Assert-Contains -Label $case.Label -Actual $actual -Expected $case.Expected
212
+ Assert-Excludes -Label $case.Label -Actual $actual -Excluded $case.Excluded
213
+
214
+ Write-Host "`n$($case.Label):"
215
+ $actual | Sort-Object | ForEach-Object { Write-Host " $_" }
216
+ }
217
+
218
+ Write-Host "`nPowerShell completion smoke test passed."
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Smoke tests for the PowerShell completion playground.
3
+ *
4
+ * @module
5
+ */
6
+ /// <reference types="bun" />
7
+
8
+ import { describe, expect, it } from 'bun:test';
9
+
10
+ import { pwshDemo } from './main.ts';
11
+
12
+ describe('pwsh-demo example', () => {
13
+ it('generates a PowerShell script with native registration', async () => {
14
+ const result = await pwshDemo.execute(['completions', 'powershell']);
15
+
16
+ expect(result.exitCode).toBe(0);
17
+
18
+ const script = result.stdout.join('');
19
+ expect(script).toContain('# PowerShell completion for pwsh-demo');
20
+ expect(script).toContain('Register-ArgumentCompleter -Native -CommandName');
21
+ expect(script).toContain('ship');
22
+ expect(script).toContain('blue-green');
23
+ expect(script).not.toContain('debug-dump');
24
+ expect(script).toContain("Forms = @('--profile', '-p')");
25
+ });
26
+
27
+ it('keeps hidden commands executable', async () => {
28
+ const result = await pwshDemo.execute(['debug-dump', '--section', 'completions']);
29
+
30
+ expect(result.exitCode).toBe(0);
31
+ expect(result.stdout.join('')).toContain('"section": "completions"');
32
+ });
33
+
34
+ it('surfaces default-command flags at the root', async () => {
35
+ const help = await pwshDemo.execute(['--help']);
36
+
37
+ expect(help.exitCode).toBe(0);
38
+ expect(help.stdout.join('')).toContain('open');
39
+
40
+ const result = await pwshDemo.execute(['bravo', '--profile', 'ops', '--shell', 'cmd']);
41
+
42
+ expect(result.exitCode).toBe(0);
43
+ expect(result.stdout).toEqual([
44
+ 'Opening bravo with ops profile in cmd\n',
45
+ 'Format: table\n',
46
+ ]);
47
+ });
48
+ });
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env bun
2
+ /**
3
+ * PowerShell completion playground for DreamCLI.
4
+ *
5
+ * Usage:
6
+ * bun --cwd=examples/pwsh-demo src/main.ts --help
7
+ * bun --cwd=examples/pwsh-demo src/main.ts completions powershell
8
+ * bun --cwd=examples/pwsh-demo src/main.ts deploy --region eu --strategy canary
9
+ * bun --cwd=examples/pwsh-demo src/main.ts config set theme midnight --scope workspace
10
+ *
11
+ * @module
12
+ */
13
+
14
+ import { arg, cli, command, flag, group } from '@kjanat/dreamcli';
15
+
16
+ const open = command('open')
17
+ .description('Open a workspace session')
18
+ .alias('o')
19
+ .arg(
20
+ 'workspace',
21
+ arg.enum(['alpha', 'bravo', 'charlie']).default('alpha').describe('Workspace to open'),
22
+ )
23
+ .flag(
24
+ 'profile',
25
+ flag
26
+ .enum(['default', 'ops', 'readonly', 'qa ops', "qa's"])
27
+ .alias('p')
28
+ .alias('account', { hidden: true })
29
+ .describe('Credential profile'),
30
+ )
31
+ .flag(
32
+ 'shell',
33
+ flag.enum(['powershell', 'cmd', 'bash']).default('powershell').describe('Interactive shell'),
34
+ )
35
+ .flag('format', flag.enum(['table', 'json', 'yaml']).alias('f').describe('Output format'))
36
+ .flag('verbose', flag.boolean().alias('v').describe('Verbose logging'))
37
+ .action(({ args, flags, out }) => {
38
+ out.log(`Opening ${args.workspace} with ${flags.profile ?? 'default'} profile in ${flags.shell}`);
39
+ out.log(`Format: ${flags.format ?? 'table'}`);
40
+ if (flags.verbose) out.log('Verbose output enabled');
41
+ });
42
+
43
+ const deploy = command('deploy')
44
+ .description('Deploy the selected workspace')
45
+ .alias('ship')
46
+ .arg(
47
+ 'target',
48
+ arg.enum(['dev', 'staging', 'prod']).default('staging').describe('Deployment target'),
49
+ )
50
+ .flag('region', flag.enum(['us', 'eu', 'ap']).alias('r').describe('Target region'))
51
+ .flag(
52
+ 'strategy',
53
+ flag.enum(['rolling', 'blue-green', 'canary']).alias('s').describe('Deployment strategy'),
54
+ )
55
+ .flag('approval', flag.enum(['manual', 'auto']).describe('Approval mode'))
56
+ .flag('dryRun', flag.boolean().alias('d').describe('Preview the rollout without applying it'))
57
+ .action(({ args, flags, out }) => {
58
+ out.log(`Deploying ${args.target} to ${flags.region ?? 'us'} with ${flags.strategy ?? 'rolling'}`);
59
+ out.log(`Approval: ${flags.approval ?? 'manual'}`);
60
+ if (flags.dryRun) out.log('Dry run only');
61
+ });
62
+
63
+ const status = command('status')
64
+ .description('Show workspace status')
65
+ .alias('st')
66
+ .flag('view', flag.enum(['summary', 'full', 'json']).default('summary').describe('Status view'))
67
+ .flag('watch', flag.boolean().alias('w').describe('Refresh continuously'))
68
+ .action(({ flags, out }) => {
69
+ out.log(`Status view: ${flags.view}`);
70
+ if (flags.watch) out.log('Watching for changes');
71
+ });
72
+
73
+ const configGet = command('get')
74
+ .description('Read a saved setting')
75
+ .alias('show')
76
+ .arg('key', arg.enum(['theme', 'region', 'profile']).describe('Setting to read'))
77
+ .action(({ args, out }) => {
78
+ out.log(`${args.key}=demo-value`);
79
+ });
80
+
81
+ const configSet = command('set')
82
+ .description('Write a saved setting')
83
+ .arg('key', arg.enum(['theme', 'region', 'profile']).describe('Setting to write'))
84
+ .arg('value', arg.string().describe('New value'))
85
+ .flag('scope', flag.enum(['user', 'workspace']).alias('s').default('user').describe('Config scope'))
86
+ .action(({ args, flags, out }) => {
87
+ out.log(`Saved ${args.key}=${args.value} to ${flags.scope} scope`);
88
+ });
89
+
90
+ const configReset = command('reset')
91
+ .description('Reset a saved setting')
92
+ .alias('rm')
93
+ .arg('key', arg.enum(['theme', 'region', 'profile']).describe('Setting to reset'))
94
+ .flag('scope', flag.enum(['user', 'workspace']).default('user').describe('Config scope'))
95
+ .action(({ args, flags, out }) => {
96
+ out.log(`Reset ${args.key} in ${flags.scope} scope`);
97
+ });
98
+
99
+ const config = group('config')
100
+ .description('Manage saved settings')
101
+ .command(configGet)
102
+ .command(configSet)
103
+ .command(configReset);
104
+
105
+ const debugDump = command('debug-dump')
106
+ .description('Dump internal playground state')
107
+ .hidden()
108
+ .flag(
109
+ 'section',
110
+ flag.enum(['schema', 'completions', 'runtime']).default('schema').describe('Section to inspect'),
111
+ )
112
+ .action(({ flags, out }) => {
113
+ out.log(JSON.stringify({ section: flags.section, hidden: true }, null, 2));
114
+ });
115
+
116
+ /**
117
+ * PowerShell completion playground CLI used by docs, smoke checks, and manual shell testing.
118
+ */
119
+ export const pwshDemo = cli('pwsh-demo')
120
+ .packageJson()
121
+ .default(open)
122
+ .command(deploy)
123
+ .command(status)
124
+ .command(config)
125
+ .command(debugDump)
126
+ .completions({ rootMode: 'surface' });
127
+
128
+ if (import.meta.main) {
129
+ pwshDemo.run();
130
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kjanat/dreamcli",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Schema-first, fully typed TypeScript CLI framework",
5
5
  "keywords": [
6
6
  "cli",
@@ -102,13 +102,16 @@
102
102
  "typecheck:tsc": "tsc --noEmit",
103
103
  "upgrade": "bun update --latest && bun add -D vitepress@next"
104
104
  },
105
+ "overrides": {
106
+ "vitepress": "next"
107
+ },
105
108
  "devDependencies": {
106
109
  "@arethetypeswrong/core": "^0.18.2",
107
110
  "@biomejs/biome": "^2.4.10",
108
111
  "@iarna/toml": "^2.2.5",
109
112
  "@shikijs/transformers": "^4.0.2",
110
113
  "@shikijs/vitepress-twoslash": "^4.0.2",
111
- "@types/bun": "^1.3.0",
114
+ "@types/bun": "^1.3.12",
112
115
  "@types/deno": "^2.5.0",
113
116
  "@types/node": ">=22.0.0, <23.0.0",
114
117
  "@typescript/native-preview": "^7.0.0-dev.20260406.1",
@@ -121,17 +124,15 @@
121
124
  "tsdown": "^0.21.7",
122
125
  "typedoc": "^0.28.18",
123
126
  "typescript": "^6.0.2",
127
+ "vite": "^8",
124
128
  "vitepress": "^2.0.0-alpha.17",
125
129
  "vitest": "^4.1.2",
126
130
  "wrangler": "^4.80.0",
127
131
  "yaml": "^2.8.3"
128
132
  },
129
- "optionalDependencies": {
130
- "vite": "^8"
131
- },
132
- "packageManager": "bun@1.3.11",
133
+ "packageManager": "bun@1.3.12",
133
134
  "engines": {
134
- "bun": ">=1.3.11",
135
+ "bun": ">=1.3",
135
136
  "deno": ">=2.6.0",
136
137
  "node": ">=22.22.2"
137
138
  },
@@ -146,7 +147,7 @@
146
147
  }
147
148
  },
148
149
  "volta": {
149
- "node": "25.8.2",
150
+ "node": "25.9.0",
150
151
  "npm": "11.12.1"
151
152
  },
152
153
  "publishConfig": {
@@ -154,7 +155,7 @@
154
155
  "provenance": true
155
156
  },
156
157
  "catalog": {
157
- "@types/bun": "^1.3.0",
158
+ "@types/bun": "^1.3.12",
158
159
  "@types/node": ">=22.0.0, <23.0.0",
159
160
  "typescript": "^6.0.2"
160
161
  }
@@ -44,7 +44,7 @@ function generatePowerShellCompletion(schema: CLISchema, options?: CompletionOpt
44
44
  lines.push(`# Generated by ${versionTag()}`);
45
45
  lines.push('#');
46
46
  lines.push('# Install for the current session:');
47
- lines.push(`# ${schema.name} completions powershell | Invoke-Expression`);
47
+ lines.push(`# ${schema.name} completions powershell | Out-String | Invoke-Expression`);
48
48
  lines.push('#');
49
49
  lines.push('# Install permanently:');
50
50
  lines.push(`# ${schema.name} completions powershell > $PROFILE.CurrentUserCurrentHost`);
@@ -69,6 +69,10 @@ function generatePowerShellCompletion(schema: CLISchema, options?: CompletionOpt
69
69
  lines.push('\t}');
70
70
  lines.push('');
71
71
  lines.push('\t$results = [System.Collections.Generic.List[CompletionResult]]::new()');
72
+ lines.push('\tif ($state.AfterSeparator) {');
73
+ lines.push('\t\treturn $results');
74
+ lines.push('\t}');
75
+ lines.push('');
72
76
  lines.push(
73
77
  `\t$wordPrefix = if ([string]::IsNullOrEmpty($wordToComplete)) { '' } else { $wordToComplete }`,
74
78
  );
@@ -236,12 +240,13 @@ function emitPowerShellHelpers(lines: string[], helperPrefix: string, dataVarNam
236
240
  lines.push('\tparam([CommandAst]$CommandAst, [int]$CursorPosition)');
237
241
  lines.push(`\t$pathData = $script:${dataVarName}[${quotePowerShellString('')}]`);
238
242
  lines.push('\t$path = ""');
243
+ lines.push('\t$afterSeparator = $false');
239
244
  lines.push('\t$expectingValue = $false');
240
245
  lines.push('\t$valueFlag = $null');
241
246
  lines.push('\t$tokens = [System.Collections.Generic.List[string]]::new()');
242
247
  lines.push('');
243
248
  lines.push('\tforeach ($element in $CommandAst.CommandElements | Select-Object -Skip 1) {');
244
- lines.push('\t\tif ($element.Extent.EndOffset -gt $CursorPosition) {');
249
+ lines.push('\t\tif ($element.Extent.EndOffset -ge $CursorPosition) {');
245
250
  lines.push('\t\t\tbreak');
246
251
  lines.push('\t\t}');
247
252
  lines.push(`\t\t$tokenText = ${helperPrefix}_UnquoteToken $element.Extent.Text`);
@@ -256,6 +261,7 @@ function emitPowerShellHelpers(lines: string[], helperPrefix: string, dataVarNam
256
261
  lines.push('\t\t}');
257
262
  lines.push('');
258
263
  lines.push("\t\tif ($token -eq '--') {");
264
+ lines.push('\t\t\t$afterSeparator = $true');
259
265
  lines.push('\t\t\tbreak');
260
266
  lines.push('\t\t}');
261
267
  lines.push('');
@@ -292,6 +298,7 @@ function emitPowerShellHelpers(lines: string[], helperPrefix: string, dataVarNam
292
298
  lines.push('\treturn @{');
293
299
  lines.push('\t\tPath = $path');
294
300
  lines.push('\t\tPathData = $pathData');
301
+ lines.push('\t\tAfterSeparator = $afterSeparator');
295
302
  lines.push('\t\tExpectingValue = $expectingValue');
296
303
  lines.push('\t\tValueFlag = $valueFlag');
297
304
  lines.push('\t}');
@@ -308,6 +315,30 @@ function emitPowerShellHelpers(lines: string[], helperPrefix: string, dataVarNam
308
315
  lines.push('\treturn $flagMatch');
309
316
  lines.push('}');
310
317
  lines.push('');
318
+ lines.push(`function ${helperPrefix}_FormatCompletionValue {`);
319
+ lines.push('\tparam([string]$Value)');
320
+ lines.push('\tif ($Value.Length -eq 0) {');
321
+ lines.push("\t\treturn \"''\"");
322
+ lines.push('\t}');
323
+ lines.push('');
324
+ lines.push("\tif ($Value -match '^[a-zA-Z0-9_./:\\\\-]+$') {");
325
+ lines.push('\t\treturn $Value');
326
+ lines.push('\t}');
327
+ lines.push('');
328
+ lines.push("\treturn \"'$([CodeGeneration]::EscapeSingleQuotedStringContent($Value))'\"");
329
+ lines.push('}');
330
+ lines.push('');
331
+ lines.push(`function ${helperPrefix}_MatchesCompletionPrefix {`);
332
+ lines.push('\tparam([string]$WordToComplete, [string]$RawText, [string]$CompletionText)');
333
+ lines.push('\tif ([string]::IsNullOrEmpty($WordToComplete)) {');
334
+ lines.push('\t\treturn $true');
335
+ lines.push('\t}');
336
+ lines.push('');
337
+ lines.push(
338
+ '\treturn $RawText.StartsWith($WordToComplete, [System.StringComparison]::OrdinalIgnoreCase) -or $CompletionText.StartsWith($WordToComplete, [System.StringComparison]::OrdinalIgnoreCase)',
339
+ );
340
+ lines.push('}');
341
+ lines.push('');
311
342
  lines.push(`function ${helperPrefix}_AddCommandResults {`);
312
343
  lines.push('\tparam($Results, $PathData, [string]$WordToComplete)');
313
344
  lines.push('\tforeach ($command in $PathData.Commands) {');
@@ -351,14 +382,15 @@ function emitPowerShellHelpers(lines: string[], helperPrefix: string, dataVarNam
351
382
  lines.push(`function ${helperPrefix}_AddEnumResults {`);
352
383
  lines.push('\tparam($Results, $Flag, [string]$WordToComplete)');
353
384
  lines.push('\tforeach ($value in $Flag.EnumValues) {');
385
+ lines.push(`\t\t$completionText = ${helperPrefix}_FormatCompletionValue -Value $value`);
354
386
  lines.push(
355
- '\t\tif (-not [string]::IsNullOrEmpty($WordToComplete) -and -not $value.StartsWith($WordToComplete, [System.StringComparison]::OrdinalIgnoreCase)) {',
387
+ `\t\tif (-not (${helperPrefix}_MatchesCompletionPrefix -WordToComplete $WordToComplete -RawText $value -CompletionText $completionText)) {`,
356
388
  );
357
389
  lines.push('\t\t\tcontinue');
358
390
  lines.push('\t\t}');
359
391
  lines.push('');
360
392
  lines.push('\t\t$Results.Add([CompletionResult]::new(');
361
- lines.push('\t\t\t$value,');
393
+ lines.push('\t\t\t$completionText,');
362
394
  lines.push('\t\t\t$value,');
363
395
  lines.push('\t\t\t[CompletionResultType]::ParameterValue,');
364
396
  lines.push('\t\t\t$Flag.Description');
@@ -369,15 +401,17 @@ function emitPowerShellHelpers(lines: string[], helperPrefix: string, dataVarNam
369
401
  lines.push(`function ${helperPrefix}_AddInlineEnumResults {`);
370
402
  lines.push('\tparam($Results, $InlineValue, [string]$WordToComplete)');
371
403
  lines.push('\tforeach ($value in $InlineValue.Flag.EnumValues) {');
372
- lines.push('\t\t$completion = "$($InlineValue.InlinePrefix)$value"');
404
+ lines.push('\t\t$rawCompletion = "$($InlineValue.InlinePrefix)$value"');
405
+ lines.push(`\t\t$formattedValue = ${helperPrefix}_FormatCompletionValue -Value $value`);
406
+ lines.push('\t\t$completionText = "$($InlineValue.InlinePrefix)$formattedValue"');
373
407
  lines.push(
374
- '\t\tif (-not [string]::IsNullOrEmpty($WordToComplete) -and -not $completion.StartsWith($WordToComplete, [System.StringComparison]::OrdinalIgnoreCase)) {',
408
+ `\t\tif (-not (${helperPrefix}_MatchesCompletionPrefix -WordToComplete $WordToComplete -RawText $rawCompletion -CompletionText $completionText)) {`,
375
409
  );
376
410
  lines.push('\t\t\tcontinue');
377
411
  lines.push('\t\t}');
378
412
  lines.push('');
379
413
  lines.push('\t\t$Results.Add([CompletionResult]::new(');
380
- lines.push('\t\t\t$completion,');
414
+ lines.push('\t\t\t$completionText,');
381
415
  lines.push('\t\t\t$value,');
382
416
  lines.push('\t\t\t[CompletionResultType]::ParameterValue,');
383
417
  lines.push('\t\t\t$InlineValue.Flag.Description');