@@ -1741,15 +1741,14 @@ Expecting one of '${allowedValues.join("', '")}'`);
1741
1741
* sub --unknown uuu op => [sub], [--unknown uuu op]
1742
1742
* sub -- --unknown uuu op => [sub --unknown uuu op], []
1743
1743
*
1744
- * @param {string[] } argv
1744
+ * @param {string[] } args
1745
1745
* @return {{operands: string[], unknown: string[]} }
1746
1746
*/
1747
1747
1748
- parseOptions ( argv ) {
1748
+ parseOptions ( args ) {
1749
1749
const operands = [ ] ; // operands, not options or values
1750
1750
const unknown = [ ] ; // first unknown option and remaining unknown args
1751
1751
let dest = operands ;
1752
- const args = argv . slice ( ) ;
1753
1752
1754
1753
function maybeOption ( arg ) {
1755
1754
return arg . length > 1 && arg [ 0 ] === '-' ;
@@ -1768,13 +1767,16 @@ Expecting one of '${allowedValues.join("', '")}'`);
1768
1767
1769
1768
// parse options
1770
1769
let activeVariadicOption = null ;
1771
- while ( args . length ) {
1772
- const arg = args . shift ( ) ;
1770
+ let activeGroup = null ; // working through group of short options, like -abc
1771
+ let i = 0 ;
1772
+ while ( i < args . length || activeGroup ) {
1773
+ const arg = activeGroup ?? args [ i ++ ] ;
1774
+ activeGroup = null ;
1773
1775
1774
1776
// literal
1775
1777
if ( arg === '--' ) {
1776
1778
if ( dest === unknown ) dest . push ( arg ) ;
1777
- dest . push ( ...args ) ;
1779
+ dest . push ( ...args . slice ( i ) ) ;
1778
1780
break ;
1779
1781
}
1780
1782
@@ -1792,17 +1794,17 @@ Expecting one of '${allowedValues.join("', '")}'`);
1792
1794
// recognised option, call listener to assign value with possible custom processing
1793
1795
if ( option ) {
1794
1796
if ( option . required ) {
1795
- const value = args . shift ( ) ;
1797
+ const value = args [ i ++ ] ;
1796
1798
if ( value === undefined ) this . optionMissingArgument ( option ) ;
1797
1799
this . emit ( `option:${ option . name ( ) } ` , value ) ;
1798
1800
} else if ( option . optional ) {
1799
1801
let value = null ;
1800
1802
// historical behaviour is optional value is following arg unless an option
1801
1803
if (
1802
- args . length > 0 &&
1803
- ( ! maybeOption ( args [ 0 ] ) || negativeNumberArg ( args [ 0 ] ) )
1804
+ i < args . length &&
1805
+ ( ! maybeOption ( args [ i ] ) || negativeNumberArg ( args [ i ] ) )
1804
1806
) {
1805
- value = args . shift ( ) ;
1807
+ value = args [ i ++ ] ;
1806
1808
}
1807
1809
this . emit ( `option:${ option . name ( ) } ` , value ) ;
1808
1810
} else {
@@ -1825,9 +1827,10 @@ Expecting one of '${allowedValues.join("', '")}'`);
1825
1827
// option with value following in same argument
1826
1828
this . emit ( `option:${ option . name ( ) } ` , arg . slice ( 2 ) ) ;
1827
1829
} else {
1828
- // boolean option, emit and put back remainder of arg for further processing
1830
+ // boolean option
1829
1831
this . emit ( `option:${ option . name ( ) } ` ) ;
1830
- args . unshift ( `-${ arg . slice ( 2 ) } ` ) ;
1832
+ // remove the processed option and keep processing group
1833
+ activeGroup = `-${ arg . slice ( 2 ) } ` ;
1831
1834
}
1832
1835
continue ;
1833
1836
}
@@ -1864,26 +1867,23 @@ Expecting one of '${allowedValues.join("', '")}'`);
1864
1867
) {
1865
1868
if ( this . _findCommand ( arg ) ) {
1866
1869
operands . push ( arg ) ;
1867
- if ( args . length > 0 ) unknown . push ( ...args ) ;
1870
+ unknown . push ( ...args . slice ( i ) ) ;
1868
1871
break ;
1869
1872
} else if (
1870
1873
this . _getHelpCommand ( ) &&
1871
1874
arg === this . _getHelpCommand ( ) . name ( )
1872
1875
) {
1873
- operands . push ( arg ) ;
1874
- if ( args . length > 0 ) operands . push ( ...args ) ;
1876
+ operands . push ( arg , ...args . slice ( i ) ) ;
1875
1877
break ;
1876
1878
} else if ( this . _defaultCommandName ) {
1877
- unknown . push ( arg ) ;
1878
- if ( args . length > 0 ) unknown . push ( ...args ) ;
1879
+ unknown . push ( arg , ...args . slice ( i ) ) ;
1879
1880
break ;
1880
1881
}
1881
1882
}
1882
1883
1883
1884
// If using passThroughOptions, stop processing options at first command-argument.
1884
1885
if ( this . _passThroughOptions ) {
1885
- dest . push ( arg ) ;
1886
- if ( args . length > 0 ) dest . push ( ...args ) ;
1886
+ dest . push ( arg , ...args . slice ( i ) ) ;
1887
1887
break ;
1888
1888
}
1889
1889
0 commit comments