@@ -4,7 +4,7 @@ import * as bash from "./bash";
44import * as fish from "./fish" ;
55import * as powershell from "./powershell" ;
66import { Completion } from "." ;
7- import type { ArgsDef , CommandDef , PositionalArgDef } from "citty" ;
7+ import type { ArgsDef , CommandDef , PositionalArgDef , SubCommandsDef } from "citty" ;
88
99function quoteIfNeeded ( path ) {
1010 return path . includes ( " " ) ? `'${ path } '` : path ;
@@ -17,32 +17,40 @@ const quotedProcessArgs = processArgs.map(quoteIfNeeded);
1717const quotedProcessExecArgs = process . execArgv . map ( quoteIfNeeded ) ;
1818const x = `${ quotedExecPath } ${ quotedProcessExecArgs . join ( " " ) } ${ quotedProcessArgs [ 0 ] } ` ;
1919
20- async function handleSubCommands (
20+ function isConfigPositional < T extends ArgsDef > ( config : CommandDef < T > ) {
21+ return config . args && Object . values ( config . args ) . some ( arg => arg . type === 'positional' )
22+ }
23+
24+ async function handleSubCommands < T extends ArgsDef = ArgsDef > (
2125 completion : Completion ,
22- subCommands : Record < string , any > ,
26+ subCommands : SubCommandsDef ,
2327 parentCmd ?: string
2428) {
2529 for ( const [ cmd , resolvableConfig ] of Object . entries ( subCommands ) ) {
2630 const config = await resolve ( resolvableConfig ) ;
2731 const meta = await resolve ( config . meta ) ;
32+ const subCommands = await resolve ( config . subCommands )
2833
2934 if ( ! meta || typeof meta ?. description !== "string" ) {
3035 throw new Error ( "Invalid meta or missing description." ) ;
3136 }
32-
33- const name = completion . addCommand ( cmd , meta . description , async ( previousArgs , toComplete , endsWithSpace ) => {
37+ const isPositional = isConfigPositional ( config )
38+ const name = completion . addCommand ( cmd , meta . description , isPositional ? [ false ] : [ ] , async ( previousArgs , toComplete , endsWithSpace ) => {
3439 return [ ]
3540 } , parentCmd ) ;
3641
3742 // Handle nested subcommands recursively
38- if ( config . subCommands ) {
39- await handleSubCommands ( completion , config . subCommands , name ) ;
43+ if ( subCommands ) {
44+ await handleSubCommands ( completion , subCommands , name ) ;
4045 }
4146
4247 // Handle arguments
4348 if ( config . args ) {
4449 for ( const [ argName , argConfig ] of Object . entries ( config . args ) ) {
4550 const conf = argConfig as ArgDef ;
51+ if ( conf . type === 'positional' ) {
52+ continue
53+ }
4654 completion . addOption (
4755 name ,
4856 `--${ argName } ` ,
@@ -75,7 +83,8 @@ export default async function tab<T extends ArgsDef = ArgsDef>(instance: Command
7583 }
7684
7785 const root = ''
78- completion . addCommand ( root , meta ?. description ?? "" , async ( previousArgs , toComplete , endsWithSpace ) => {
86+ const isPositional = isConfigPositional ( instance )
87+ completion . addCommand ( root , meta ?. description ?? "" , isPositional ? [ false ] : [ ] , async ( previousArgs , toComplete , endsWithSpace ) => {
7988 return [ ]
8089 } ) ;
8190
0 commit comments