From a7c51bf4f1dd2716039989ae3c017ef36a41d8b2 Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Tue, 29 Apr 2025 14:51:49 +0330 Subject: [PATCH 1/7] fix espace args --- src/cac.ts | 12 +++++++++++- src/citty.ts | 14 +++++++++++++- src/commander.ts | 12 ++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/cac.ts b/src/cac.ts index 7c1d219..9ca84f7 100644 --- a/src/cac.ts +++ b/src/cac.ts @@ -24,6 +24,10 @@ export default async function tab( ) { const completion = new Completion(); + // a hidden flag to track if -- is there in the raw arguments? we might need a better way to do this? + const dashDashIndex = process.argv.indexOf('--'); + const wasDashDashProvided = dashDashIndex !== -1; + // Add all commands and their options for (const cmd of [instance.globalCommand, ...instance.commands]) { if (cmd.name === 'complete') continue; // Skip completion command @@ -85,7 +89,13 @@ export default async function tab( break; } default: { - const args: string[] = extra['--']; + if (!wasDashDashProvided) { + console.error( + 'Error: You need to use -- to separate completion arguments' + ); + return; + } + const args: string[] = extra['--'] || []; instance.showHelpOnExit = false; // Parse current command context diff --git a/src/citty.ts b/src/citty.ts index e786cd1..6ff5f7e 100644 --- a/src/citty.ts +++ b/src/citty.ts @@ -98,6 +98,10 @@ export default async function tab( ) { const completion = new Completion(); + // a hidden flag to track if -- is there in the raw arguments? we might need a better way to do this? + const dashDashIndex = process.argv.indexOf('--'); + const wasDashDashProvided = dashDashIndex !== -1; + const meta = await resolve(instance.meta); if (!meta) { @@ -155,7 +159,6 @@ export default async function tab( }, async run(ctx) { let shell: string | undefined = ctx.rawArgs[0]; - const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1); if (shell === '--') { shell = undefined; @@ -188,6 +191,15 @@ export default async function tab( break; } default: { + // check if -- separator was provided + if (!wasDashDashProvided) { + console.error( + 'Error: You need to use -- to separate completion arguments' + ); + return; + } + + const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1); // const args = (await resolve(instance.args))!; // const parsed = parseArgs(extra, args); // TODO: this is not ideal at all diff --git a/src/commander.ts b/src/commander.ts index bcdd5d4..78c5cc1 100644 --- a/src/commander.ts +++ b/src/commander.ts @@ -21,6 +21,10 @@ export default function tab(instance: CommanderCommand): Completion { const completion = new Completion(); const programName = instance.name(); + // a hidden flag to track if -- is there in the raw arguments? we might need a better way to do this? + const dashDashIndex = process.argv.indexOf('--'); + const wasDashDashProvided = dashDashIndex !== -1; + // Process the root command processRootCommand(completion, instance, programName); @@ -79,6 +83,14 @@ export default function tab(instance: CommanderCommand): Completion { break; } default: { + // check if -- separator is provided + if (!wasDashDashProvided) { + console.error( + 'Error: You need to use -- to separate completion arguments' + ); + return; + } + // Parse current command context for autocompletion return completion.parse(extra); } From 42264161b7ac388b20d945d322fc0296c0375606 Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Tue, 29 Apr 2025 15:11:35 +0330 Subject: [PATCH 2/7] update --- src/cac.ts | 12 +++--------- src/citty.ts | 12 ++---------- src/commander.ts | 11 ++--------- src/shared.ts | 11 +++++++++++ 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/cac.ts b/src/cac.ts index 9ca84f7..c3fbdca 100644 --- a/src/cac.ts +++ b/src/cac.ts @@ -4,7 +4,7 @@ import * as fish from './fish'; import * as powershell from './powershell'; import type { CAC } from 'cac'; import { Completion } from './index'; -import { CompletionConfig, noopHandler } from './shared'; +import { CompletionConfig, noopHandler, requireDashDashSeparator } from './shared'; const execPath = process.execPath; const processArgs = process.argv.slice(1); @@ -24,10 +24,6 @@ export default async function tab( ) { const completion = new Completion(); - // a hidden flag to track if -- is there in the raw arguments? we might need a better way to do this? - const dashDashIndex = process.argv.indexOf('--'); - const wasDashDashProvided = dashDashIndex !== -1; - // Add all commands and their options for (const cmd of [instance.globalCommand, ...instance.commands]) { if (cmd.name === 'complete') continue; // Skip completion command @@ -89,12 +85,10 @@ export default async function tab( break; } default: { - if (!wasDashDashProvided) { - console.error( - 'Error: You need to use -- to separate completion arguments' - ); + if (!requireDashDashSeparator(instance.name)) { return; } + const args: string[] = extra['--'] || []; instance.showHelpOnExit = false; diff --git a/src/citty.ts b/src/citty.ts index 6ff5f7e..875231d 100644 --- a/src/citty.ts +++ b/src/citty.ts @@ -11,7 +11,7 @@ import type { SubCommandsDef, } from 'citty'; import { generateFigSpec } from './fig'; -import { CompletionConfig, noopHandler } from './shared'; +import { CompletionConfig, noopHandler, requireDashDashSeparator } from './shared'; function quoteIfNeeded(path: string) { return path.includes(' ') ? `'${path}'` : path; @@ -98,10 +98,6 @@ export default async function tab( ) { const completion = new Completion(); - // a hidden flag to track if -- is there in the raw arguments? we might need a better way to do this? - const dashDashIndex = process.argv.indexOf('--'); - const wasDashDashProvided = dashDashIndex !== -1; - const meta = await resolve(instance.meta); if (!meta) { @@ -191,11 +187,7 @@ export default async function tab( break; } default: { - // check if -- separator was provided - if (!wasDashDashProvided) { - console.error( - 'Error: You need to use -- to separate completion arguments' - ); + if (!requireDashDashSeparator(name)) { return; } diff --git a/src/commander.ts b/src/commander.ts index 78c5cc1..3824228 100644 --- a/src/commander.ts +++ b/src/commander.ts @@ -4,6 +4,7 @@ import * as fish from './fish'; import * as powershell from './powershell'; import type { Command as CommanderCommand } from 'commander'; import { Completion } from './'; +import { requireDashDashSeparator } from './shared'; const execPath = process.execPath; const processArgs = process.argv.slice(1); @@ -21,10 +22,6 @@ export default function tab(instance: CommanderCommand): Completion { const completion = new Completion(); const programName = instance.name(); - // a hidden flag to track if -- is there in the raw arguments? we might need a better way to do this? - const dashDashIndex = process.argv.indexOf('--'); - const wasDashDashProvided = dashDashIndex !== -1; - // Process the root command processRootCommand(completion, instance, programName); @@ -83,11 +80,7 @@ export default function tab(instance: CommanderCommand): Completion { break; } default: { - // check if -- separator is provided - if (!wasDashDashProvided) { - console.error( - 'Error: You need to use -- to separate completion arguments' - ); + if (!requireDashDashSeparator(programName)) { return; } diff --git a/src/shared.ts b/src/shared.ts index 23e4acf..32631c2 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -16,3 +16,14 @@ export interface CompletionConfig { } >; } + +export function requireDashDashSeparator(programName: string): boolean { + const dashDashIndex = process.argv.indexOf('--'); + const wasDashDashProvided = dashDashIndex !== -1; + + if (!wasDashDashProvided) { + console.error('Error: You need to use -- to separate completion arguments'); + } + + return wasDashDashProvided; +} From b213b69f9b0d6d81155c1a4405c74c8f254a5f7c Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Tue, 29 Apr 2025 15:13:05 +0330 Subject: [PATCH 3/7] prettier --- src/cac.ts | 6 +++++- src/citty.ts | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cac.ts b/src/cac.ts index c3fbdca..87eafa7 100644 --- a/src/cac.ts +++ b/src/cac.ts @@ -4,7 +4,11 @@ import * as fish from './fish'; import * as powershell from './powershell'; import type { CAC } from 'cac'; import { Completion } from './index'; -import { CompletionConfig, noopHandler, requireDashDashSeparator } from './shared'; +import { + CompletionConfig, + noopHandler, + requireDashDashSeparator, +} from './shared'; const execPath = process.execPath; const processArgs = process.argv.slice(1); diff --git a/src/citty.ts b/src/citty.ts index 875231d..b2b66d5 100644 --- a/src/citty.ts +++ b/src/citty.ts @@ -11,7 +11,11 @@ import type { SubCommandsDef, } from 'citty'; import { generateFigSpec } from './fig'; -import { CompletionConfig, noopHandler, requireDashDashSeparator } from './shared'; +import { + CompletionConfig, + noopHandler, + requireDashDashSeparator, +} from './shared'; function quoteIfNeeded(path: string) { return path.includes(' ') ? `'${path}'` : path; From 8a16758a7526a51314fcb235f429c8b12c7fadb1 Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Tue, 29 Apr 2025 15:31:19 +0330 Subject: [PATCH 4/7] update --- src/cac.ts | 32 +++++++++++++++++--------------- src/citty.ts | 26 ++++++++++++++------------ src/commander.ts | 12 +++++++----- src/shared.ts | 10 ++++------ 4 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/cac.ts b/src/cac.ts index 87eafa7..d009a71 100644 --- a/src/cac.ts +++ b/src/cac.ts @@ -7,7 +7,7 @@ import { Completion } from './index'; import { CompletionConfig, noopHandler, - requireDashDashSeparator, + assertDoubleDashes, } from './shared'; const execPath = process.execPath; @@ -89,23 +89,25 @@ export default async function tab( break; } default: { - if (!requireDashDashSeparator(instance.name)) { - return; - } + try { + assertDoubleDashes(instance.name); - const args: string[] = extra['--'] || []; - instance.showHelpOnExit = false; + const args: string[] = extra['--'] || []; + instance.showHelpOnExit = false; - // Parse current command context - instance.unsetMatchedCommand(); - instance.parse([execPath, processArgs[0], ...args], { - run: false, - }); + // Parse current command context + instance.unsetMatchedCommand(); + instance.parse([execPath, processArgs[0], ...args], { + run: false, + }); - // const matchedCommand = instance.matchedCommand?.name || ''; - // const potentialCommand = args.join(' ') - // console.log(potentialCommand) - return completion.parse(args); + // const matchedCommand = instance.matchedCommand?.name || ''; + // const potentialCommand = args.join(' ') + // console.log(potentialCommand) + return completion.parse(args); + } catch (error) { + return; + } } } }); diff --git a/src/citty.ts b/src/citty.ts index b2b66d5..e708253 100644 --- a/src/citty.ts +++ b/src/citty.ts @@ -14,7 +14,7 @@ import { generateFigSpec } from './fig'; import { CompletionConfig, noopHandler, - requireDashDashSeparator, + assertDoubleDashes, } from './shared'; function quoteIfNeeded(path: string) { @@ -191,19 +191,21 @@ export default async function tab( break; } default: { - if (!requireDashDashSeparator(name)) { + try { + assertDoubleDashes(name); + + const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1); + // const args = (await resolve(instance.args))!; + // const parsed = parseArgs(extra, args); + // TODO: this is not ideal at all + // const matchedCommand = parsed._.join(' ').trim(); //TODO: this was passed to parse line 170 + // TODO: `command lint i` does not work because `lint` and `i` are potential commands + // instead the potential command should only be `lint` + // and `i` is the to be completed part + return completion.parse(extra); + } catch (error) { return; } - - const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1); - // const args = (await resolve(instance.args))!; - // const parsed = parseArgs(extra, args); - // TODO: this is not ideal at all - // const matchedCommand = parsed._.join(' ').trim(); //TODO: this was passed to parse line 170 - // TODO: `command lint i` does not work because `lint` and `i` are potential commands - // instead the potential command should only be `lint` - // and `i` is the to be completed part - return completion.parse(extra); } } }, diff --git a/src/commander.ts b/src/commander.ts index 3824228..3a64765 100644 --- a/src/commander.ts +++ b/src/commander.ts @@ -4,7 +4,7 @@ import * as fish from './fish'; import * as powershell from './powershell'; import type { Command as CommanderCommand } from 'commander'; import { Completion } from './'; -import { requireDashDashSeparator } from './shared'; +import { assertDoubleDashes } from './shared'; const execPath = process.execPath; const processArgs = process.argv.slice(1); @@ -80,12 +80,14 @@ export default function tab(instance: CommanderCommand): Completion { break; } default: { - if (!requireDashDashSeparator(programName)) { + try { + assertDoubleDashes(programName); + + // Parse current command context for autocompletion + return completion.parse(extra); + } catch (error) { return; } - - // Parse current command context for autocompletion - return completion.parse(extra); } } }); diff --git a/src/shared.ts b/src/shared.ts index 32631c2..42d6570 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -17,13 +17,11 @@ export interface CompletionConfig { >; } -export function requireDashDashSeparator(programName: string): boolean { +export function assertDoubleDashes(programName: string = 'cli'): void { const dashDashIndex = process.argv.indexOf('--'); - const wasDashDashProvided = dashDashIndex !== -1; - if (!wasDashDashProvided) { - console.error('Error: You need to use -- to separate completion arguments'); + if (dashDashIndex === -1) { + const errorMessage = `Error: You need to use -- to separate completion arguments.\nExample: ${programName} complete -- `; + console.error(errorMessage); } - - return wasDashDashProvided; } From 4f991e82c53d13a696797c7976ce1943fab761b4 Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Tue, 29 Apr 2025 15:31:50 +0330 Subject: [PATCH 5/7] prettier --- src/cac.ts | 6 +----- src/citty.ts | 6 +----- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/cac.ts b/src/cac.ts index d009a71..bc94937 100644 --- a/src/cac.ts +++ b/src/cac.ts @@ -4,11 +4,7 @@ import * as fish from './fish'; import * as powershell from './powershell'; import type { CAC } from 'cac'; import { Completion } from './index'; -import { - CompletionConfig, - noopHandler, - assertDoubleDashes, -} from './shared'; +import { CompletionConfig, noopHandler, assertDoubleDashes } from './shared'; const execPath = process.execPath; const processArgs = process.argv.slice(1); diff --git a/src/citty.ts b/src/citty.ts index e708253..407ebff 100644 --- a/src/citty.ts +++ b/src/citty.ts @@ -11,11 +11,7 @@ import type { SubCommandsDef, } from 'citty'; import { generateFigSpec } from './fig'; -import { - CompletionConfig, - noopHandler, - assertDoubleDashes, -} from './shared'; +import { CompletionConfig, noopHandler, assertDoubleDashes } from './shared'; function quoteIfNeeded(path: string) { return path.includes(' ') ? `'${path}'` : path; From 08fa225005360eaeae58e22b4a6c6b1c2915b02b Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Tue, 29 Apr 2025 15:36:01 +0330 Subject: [PATCH 6/7] throw error --- src/shared.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/shared.ts b/src/shared.ts index 42d6570..690480a 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -23,5 +23,6 @@ export function assertDoubleDashes(programName: string = 'cli'): void { if (dashDashIndex === -1) { const errorMessage = `Error: You need to use -- to separate completion arguments.\nExample: ${programName} complete -- `; console.error(errorMessage); + throw new Error('Missing -- separator'); } } From 83e900b548335a5273579ad68cf52632c6ad19a7 Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Tue, 29 Apr 2025 16:09:21 +0330 Subject: [PATCH 7/7] update --- src/cac.ts | 28 ++++++++++++---------------- src/citty.ts | 26 +++++++++++--------------- src/commander.ts | 10 +++------- src/shared.ts | 2 +- 4 files changed, 27 insertions(+), 39 deletions(-) diff --git a/src/cac.ts b/src/cac.ts index bc94937..8ddc14e 100644 --- a/src/cac.ts +++ b/src/cac.ts @@ -85,25 +85,21 @@ export default async function tab( break; } default: { - try { - assertDoubleDashes(instance.name); + assertDoubleDashes(instance.name); - const args: string[] = extra['--'] || []; - instance.showHelpOnExit = false; + const args: string[] = extra['--'] || []; + instance.showHelpOnExit = false; - // Parse current command context - instance.unsetMatchedCommand(); - instance.parse([execPath, processArgs[0], ...args], { - run: false, - }); + // Parse current command context + instance.unsetMatchedCommand(); + instance.parse([execPath, processArgs[0], ...args], { + run: false, + }); - // const matchedCommand = instance.matchedCommand?.name || ''; - // const potentialCommand = args.join(' ') - // console.log(potentialCommand) - return completion.parse(args); - } catch (error) { - return; - } + // const matchedCommand = instance.matchedCommand?.name || ''; + // const potentialCommand = args.join(' ') + // console.log(potentialCommand) + return completion.parse(args); } } }); diff --git a/src/citty.ts b/src/citty.ts index 407ebff..f7affe4 100644 --- a/src/citty.ts +++ b/src/citty.ts @@ -187,21 +187,17 @@ export default async function tab( break; } default: { - try { - assertDoubleDashes(name); - - const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1); - // const args = (await resolve(instance.args))!; - // const parsed = parseArgs(extra, args); - // TODO: this is not ideal at all - // const matchedCommand = parsed._.join(' ').trim(); //TODO: this was passed to parse line 170 - // TODO: `command lint i` does not work because `lint` and `i` are potential commands - // instead the potential command should only be `lint` - // and `i` is the to be completed part - return completion.parse(extra); - } catch (error) { - return; - } + assertDoubleDashes(name); + + const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf('--') + 1); + // const args = (await resolve(instance.args))!; + // const parsed = parseArgs(extra, args); + // TODO: this is not ideal at all + // const matchedCommand = parsed._.join(' ').trim(); //TODO: this was passed to parse line 170 + // TODO: `command lint i` does not work because `lint` and `i` are potential commands + // instead the potential command should only be `lint` + // and `i` is the to be completed part + return completion.parse(extra); } } }, diff --git a/src/commander.ts b/src/commander.ts index 3a64765..7a9d842 100644 --- a/src/commander.ts +++ b/src/commander.ts @@ -80,14 +80,10 @@ export default function tab(instance: CommanderCommand): Completion { break; } default: { - try { - assertDoubleDashes(programName); + assertDoubleDashes(programName); - // Parse current command context for autocompletion - return completion.parse(extra); - } catch (error) { - return; - } + // Parse current command context for autocompletion + return completion.parse(extra); } } }); diff --git a/src/shared.ts b/src/shared.ts index 690480a..798b525 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -23,6 +23,6 @@ export function assertDoubleDashes(programName: string = 'cli'): void { if (dashDashIndex === -1) { const errorMessage = `Error: You need to use -- to separate completion arguments.\nExample: ${programName} complete -- `; console.error(errorMessage); - throw new Error('Missing -- separator'); + process.exit(1); } }