@@ -1741,15 +1741,14 @@ Expecting one of '${allowedValues.join("', '")}'`);
17411741 * sub --unknown uuu op => [sub], [--unknown uuu op]
17421742 * sub -- --unknown uuu op => [sub --unknown uuu op], []
17431743 *
1744- * @param {string[] } argv
1744+ * @param {string[] } args
17451745 * @return {{operands: string[], unknown: string[]} }
17461746 */
17471747
1748- parseOptions ( argv ) {
1748+ parseOptions ( args ) {
17491749 const operands = [ ] ; // operands, not options or values
17501750 const unknown = [ ] ; // first unknown option and remaining unknown args
17511751 let dest = operands ;
1752- const args = argv . slice ( ) ;
17531752
17541753 function maybeOption ( arg ) {
17551754 return arg . length > 1 && arg [ 0 ] === '-' ;
@@ -1768,13 +1767,16 @@ Expecting one of '${allowedValues.join("', '")}'`);
17681767
17691768 // parse options
17701769 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 ;
17731775
17741776 // literal
17751777 if ( arg === '--' ) {
17761778 if ( dest === unknown ) dest . push ( arg ) ;
1777- dest . push ( ...args ) ;
1779+ dest . push ( ...args . slice ( i ) ) ;
17781780 break ;
17791781 }
17801782
@@ -1792,17 +1794,17 @@ Expecting one of '${allowedValues.join("', '")}'`);
17921794 // recognised option, call listener to assign value with possible custom processing
17931795 if ( option ) {
17941796 if ( option . required ) {
1795- const value = args . shift ( ) ;
1797+ const value = args [ i ++ ] ;
17961798 if ( value === undefined ) this . optionMissingArgument ( option ) ;
17971799 this . emit ( `option:${ option . name ( ) } ` , value ) ;
17981800 } else if ( option . optional ) {
17991801 let value = null ;
18001802 // historical behaviour is optional value is following arg unless an option
18011803 if (
1802- args . length > 0 &&
1803- ( ! maybeOption ( args [ 0 ] ) || negativeNumberArg ( args [ 0 ] ) )
1804+ i < args . length &&
1805+ ( ! maybeOption ( args [ i ] ) || negativeNumberArg ( args [ i ] ) )
18041806 ) {
1805- value = args . shift ( ) ;
1807+ value = args [ i ++ ] ;
18061808 }
18071809 this . emit ( `option:${ option . name ( ) } ` , value ) ;
18081810 } else {
@@ -1825,9 +1827,10 @@ Expecting one of '${allowedValues.join("', '")}'`);
18251827 // option with value following in same argument
18261828 this . emit ( `option:${ option . name ( ) } ` , arg . slice ( 2 ) ) ;
18271829 } else {
1828- // boolean option, emit and put back remainder of arg for further processing
1830+ // boolean option
18291831 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 ) } ` ;
18311834 }
18321835 continue ;
18331836 }
@@ -1864,26 +1867,23 @@ Expecting one of '${allowedValues.join("', '")}'`);
18641867 ) {
18651868 if ( this . _findCommand ( arg ) ) {
18661869 operands . push ( arg ) ;
1867- if ( args . length > 0 ) unknown . push ( ...args ) ;
1870+ unknown . push ( ...args . slice ( i ) ) ;
18681871 break ;
18691872 } else if (
18701873 this . _getHelpCommand ( ) &&
18711874 arg === this . _getHelpCommand ( ) . name ( )
18721875 ) {
1873- operands . push ( arg ) ;
1874- if ( args . length > 0 ) operands . push ( ...args ) ;
1876+ operands . push ( arg , ...args . slice ( i ) ) ;
18751877 break ;
18761878 } else if ( this . _defaultCommandName ) {
1877- unknown . push ( arg ) ;
1878- if ( args . length > 0 ) unknown . push ( ...args ) ;
1879+ unknown . push ( arg , ...args . slice ( i ) ) ;
18791880 break ;
18801881 }
18811882 }
18821883
18831884 // If using passThroughOptions, stop processing options at first command-argument.
18841885 if ( this . _passThroughOptions ) {
1885- dest . push ( arg ) ;
1886- if ( args . length > 0 ) dest . push ( ...args ) ;
1886+ dest . push ( arg , ...args . slice ( i ) ) ;
18871887 break ;
18881888 }
18891889
0 commit comments