File tree Expand file tree Collapse file tree 4 files changed +21
-4
lines changed
Expand file tree Collapse file tree 4 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import * as fish from './fish';
44import * as powershell from './powershell' ;
55import type { CAC } from 'cac' ;
66import { Completion } from './index' ;
7- import { CompletionConfig , noopHandler } from './shared' ;
7+ import { CompletionConfig , noopHandler , assertDoubleDashes } from './shared' ;
88
99const execPath = process . execPath ;
1010const processArgs = process . argv . slice ( 1 ) ;
@@ -85,7 +85,9 @@ export default async function tab(
8585 break ;
8686 }
8787 default : {
88- const args : string [ ] = extra [ '--' ] ;
88+ assertDoubleDashes ( instance . name ) ;
89+
90+ const args : string [ ] = extra [ '--' ] || [ ] ;
8991 instance . showHelpOnExit = false ;
9092
9193 // Parse current command context
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import type {
1111 SubCommandsDef ,
1212} from 'citty' ;
1313import { generateFigSpec } from './fig' ;
14- import { CompletionConfig , noopHandler } from './shared' ;
14+ import { CompletionConfig , noopHandler , assertDoubleDashes } from './shared' ;
1515
1616function quoteIfNeeded ( path : string ) {
1717 return path . includes ( ' ' ) ? `'${ path } '` : path ;
@@ -155,7 +155,6 @@ export default async function tab<TArgs extends ArgsDef>(
155155 } ,
156156 async run ( ctx ) {
157157 let shell : string | undefined = ctx . rawArgs [ 0 ] ;
158- const extra = ctx . rawArgs . slice ( ctx . rawArgs . indexOf ( '--' ) + 1 ) ;
159158
160159 if ( shell === '--' ) {
161160 shell = undefined ;
@@ -188,6 +187,9 @@ export default async function tab<TArgs extends ArgsDef>(
188187 break ;
189188 }
190189 default : {
190+ assertDoubleDashes ( name ) ;
191+
192+ const extra = ctx . rawArgs . slice ( ctx . rawArgs . indexOf ( '--' ) + 1 ) ;
191193 // const args = (await resolve(instance.args))!;
192194 // const parsed = parseArgs(extra, args);
193195 // TODO: this is not ideal at all
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import * as fish from './fish';
44import * as powershell from './powershell' ;
55import type { Command as CommanderCommand } from 'commander' ;
66import { Completion } from './' ;
7+ import { assertDoubleDashes } from './shared' ;
78
89const execPath = process . execPath ;
910const processArgs = process . argv . slice ( 1 ) ;
@@ -79,6 +80,8 @@ export default function tab(instance: CommanderCommand): Completion {
7980 break ;
8081 }
8182 default : {
83+ assertDoubleDashes ( programName ) ;
84+
8285 // Parse current command context for autocompletion
8386 return completion . parse ( extra ) ;
8487 }
Original file line number Diff line number Diff line change @@ -16,3 +16,13 @@ export interface CompletionConfig {
1616 }
1717 > ;
1818}
19+
20+ export function assertDoubleDashes ( programName : string = 'cli' ) : void {
21+ const dashDashIndex = process . argv . indexOf ( '--' ) ;
22+
23+ if ( dashDashIndex === - 1 ) {
24+ const errorMessage = `Error: You need to use -- to separate completion arguments.\nExample: ${ programName } complete -- <args>` ;
25+ console . error ( errorMessage ) ;
26+ process . exit ( 1 ) ;
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments