11'use strict' ;
22
3+ const {
4+ ArrayIsArray,
5+ ArrayPrototypeConcat,
6+ ArrayPrototypeIncludes,
7+ ArrayPrototypeSlice,
8+ ArrayPrototypePush,
9+ StringPrototypeCharAt,
10+ StringPrototypeIncludes,
11+ StringPrototypeSlice,
12+ StringPrototypeSplit,
13+ StringPrototypeStartsWith,
14+ } = require ( './primordials' ) ;
15+
316function getMainArgs ( ) {
417 // This function is a placeholder for proposed process.mainArgs.
518 // Work out where to slice process.argv for user supplied arguments.
@@ -18,18 +31,20 @@ function getMainArgs() {
1831 // (Not included in tests as hopefully temporary example.)
1932 /* c8 ignore next 3 */
2033 if ( process . versions && process . versions . electron && ! process . defaultApp ) {
21- return process . argv . slice ( 1 ) ;
34+ return ArrayPrototypeSlice ( process . argv , 1 ) ;
2235 }
2336
2437 // Check node options for scenarios where user CLI args follow executable.
2538 const execArgv = process . execArgv ;
26- if ( execArgv . includes ( '-e' ) || execArgv . includes ( '--eval' ) ||
27- execArgv . includes ( '-p' ) || execArgv . includes ( '--print' ) ) {
28- return process . argv . slice ( 1 ) ;
39+ if ( StringPrototypeIncludes ( execArgv , '-e' ) ||
40+ StringPrototypeIncludes ( execArgv , '--eval' ) ||
41+ StringPrototypeIncludes ( execArgv , '-p' ) ||
42+ StringPrototypeIncludes ( execArgv , '--print' ) ) {
43+ return ArrayPrototypeSlice ( process . argv , 1 ) ;
2944 }
3045
3146 // Normally first two arguments are executable and script, then CLI arguments
32- return process . argv . slice ( 2 ) ;
47+ return ArrayPrototypeSlice ( process . argv , 2 ) ;
3348}
3449
3550const parseArgs = (
@@ -39,7 +54,7 @@ const parseArgs = (
3954 if ( typeof options !== 'object' || options === null ) {
4055 throw new Error ( 'Whoops!' ) ;
4156 }
42- if ( options . withValue !== undefined && ! Array . isArray ( options . withValue ) ) {
57+ if ( options . withValue !== undefined && ! ArrayIsArray ( options . withValue ) ) {
4358 throw new Error ( 'Whoops! options.withValue should be an array.' ) ;
4459 }
4560
@@ -53,37 +68,44 @@ const parseArgs = (
5368 while ( pos < argv . length ) {
5469 let arg = argv [ pos ] ;
5570
56- if ( arg . startsWith ( '-' ) ) {
71+ if ( StringPrototypeStartsWith ( arg , '-' ) ) {
5772 // Everything after a bare '--' is considered a positional argument
5873 // and is returned verbatim
5974 if ( arg === '--' ) {
60- result . positionals . push ( ...argv . slice ( ++ pos ) ) ;
75+ result . positionals = ArrayPrototypeConcat (
76+ result . positionals ,
77+ ArrayPrototypeSlice ( argv , ++ pos )
78+ ) ;
6179 return result ;
62- } else if ( arg . charAt ( 1 ) !== '-' ) { // Look for shortcodes: -fXzy
80+ } else if (
81+ StringPrototypeCharAt ( arg , 1 ) !== '-'
82+ ) { // Look for shortcodes: -fXzy
6383 throw new Error ( 'What are we doing with shortcodes!?!' ) ;
6484 }
6585
66- arg = arg . slice ( 2 ) ; // remove leading --
86+ arg = StringPrototypeSlice ( arg , 2 ) ; // remove leading --
6787
68- if ( arg . includes ( '=' ) ) {
88+ if ( StringPrototypeIncludes ( arg , '=' ) ) {
6989 // withValue equals(=) case
70- const argParts = arg . split ( '=' ) ;
90+ const argParts = StringPrototypeSplit ( arg , '=' ) ;
7191
7292 result . flags [ argParts [ 0 ] ] = true ;
7393 // If withValue option is specified, take 2nd part after '=' as value,
7494 // else set value as undefined
7595 const val = options . withValue &&
76- options . withValue . includes ( argParts [ 0 ] ) ?
96+ ArrayPrototypeIncludes ( options . withValue , argParts [ 0 ] ) ?
7797 argParts [ 1 ] : undefined ;
7898 // Append value to previous values array for case of multiples
7999 // option, else add to empty array
80- result . values [ argParts [ 0 ] ] = [ ] . concat (
81- options . multiples &&
82- options . multiples . includes ( argParts [ 0 ] ) &&
100+ result . values [ argParts [ 0 ] ] = ArrayPrototypeConcat ( [ ] ,
101+ options . multiples &&
102+ ArrayPrototypeIncludes ( options . multiples , argParts [ 0 ] ) &&
83103 result . values [ argParts [ 0 ] ] || [ ] ,
84- val ,
104+ val ,
85105 ) ;
86- } else if ( pos + 1 < argv . length && ! argv [ pos + 1 ] . startsWith ( '-' ) ) {
106+ } else if ( pos + 1 < argv . length &&
107+ ! StringPrototypeStartsWith ( argv [ pos + 1 ] , '-' )
108+ ) {
87109 // withValue option should also support setting values when '=
88110 // isn't used ie. both --foo=b and --foo b should work
89111
@@ -92,36 +114,39 @@ const parseArgs = (
92114 // value and then increment pos so that we don't re-evaluate that
93115 // arg, else set value as undefined ie. --foo b --bar c, after setting
94116 // b as the value for foo, evaluate --bar next and skip 'b'
95- const val = options . withValue && options . withValue . includes ( arg ) ?
96- argv [ ++ pos ] :
117+ const val = options . withValue &&
118+ ArrayPrototypeIncludes ( options . withValue , arg ) ? argv [ ++ pos ] :
97119 undefined ;
98120 // Append value to previous values array for case of multiples
99121 // option, else add to empty array
100- result . values [ arg ] = [ ] . concat (
101- options . multiples && options . multiples . includes ( arg ) &&
102- result . values [ arg ] ?
103- result . values [ arg ] :
104- [ ] ,
105- val ) ;
122+ result . values [ arg ] = ArrayPrototypeConcat (
123+ [ ] ,
124+ options . multiples &&
125+ ArrayPrototypeIncludes ( options . multiples , arg ) &&
126+ result . values [ arg ] ||
127+ [ ] ,
128+ val
129+ ) ;
106130 } else {
107131 // Cases when an arg is specified without a value, example
108132 // '--foo --bar' <- 'foo' and 'bar' flags should be set to true and
109133 // shave value as undefined
110134 result . flags [ arg ] = true ;
111135 // Append undefined to previous values array for case of
112136 // multiples option, else add to empty array
113- result . values [ arg ] = [ ] . concat (
114- options . multiples && options . multiples . includes ( arg ) &&
115- result . values [ arg ] ?
116- result . values [ arg ] :
117- [ ] ,
137+ result . values [ arg ] = ArrayPrototypeConcat (
138+ [ ] ,
139+ options . multiples &&
140+ ArrayPrototypeIncludes ( options . multiples , arg ) &&
141+ result . values [ arg ] ||
142+ [ ] ,
118143 undefined
119144 ) ;
120145 }
121146
122147 } else {
123148 // Arguements without a dash prefix are considered "positional"
124- result . positionals . push ( arg ) ;
149+ ArrayPrototypePush ( result . positionals , arg ) ;
125150 }
126151
127152 pos ++ ;
0 commit comments