File tree Expand file tree Collapse file tree 2 files changed +26
-5
lines changed
Expand file tree Collapse file tree 2 files changed +26
-5
lines changed Original file line number Diff line number Diff line change @@ -40,13 +40,34 @@ function buildSchema(options: OptionDef[]): FlagSchema {
4040
4141/**
4242 * Quick scan: collect positional (non-dash) args to determine the command path.
43- * Does not consume flag values — just skips dash-prefixed tokens.
43+ * Skips global flags and their values so that e.g. `--output json text chat`
44+ * correctly produces ['text', 'chat'] instead of ['json', 'text', 'chat'].
4445 */
45- export function scanCommandPath ( argv : string [ ] ) : string [ ] {
46+ export function scanCommandPath ( argv : string [ ] , globalOptions : OptionDef [ ] = [ ] ) : string [ ] {
47+ const globalSchema = buildSchema ( globalOptions ) ;
4648 const path : string [ ] = [ ] ;
47- for ( const arg of argv ) {
49+ let i = 0 ;
50+ while ( i < argv . length ) {
51+ const arg = argv [ i ] ! ;
4852 if ( arg === '--' ) break ;
49- if ( ! arg . startsWith ( '-' ) ) path . push ( arg ) ;
53+
54+ if ( arg . startsWith ( '--' ) ) {
55+ const eqIdx = arg . indexOf ( '=' ) ;
56+ const key = eqIdx !== - 1 ? arg . slice ( 2 , eqIdx ) : arg . slice ( 2 ) ;
57+ const camelKey = kebabToCamel ( key ) ;
58+
59+ if ( ! globalSchema . booleans . has ( camelKey ) && eqIdx === - 1 ) {
60+ i += 2 ;
61+ } else {
62+ i += 1 ;
63+ }
64+ continue ;
65+ }
66+
67+ if ( arg . startsWith ( '-' ) ) { i ++ ; continue ; }
68+
69+ path . push ( arg ) ;
70+ i ++ ;
5071 }
5172 return path ;
5273}
Original file line number Diff line number Diff line change @@ -35,7 +35,7 @@ async function main() {
3535 process . exit ( 0 ) ;
3636 }
3737
38- const commandPath = scanCommandPath ( argv ) ;
38+ const commandPath = scanCommandPath ( argv , GLOBAL_OPTIONS ) ;
3939
4040 if ( argv . includes ( '--help' ) || argv . includes ( '-h' ) ) {
4141 registry . printHelp ( commandPath , process . stderr ) ;
You can’t perform that action at this time.
0 commit comments