1
1
'use strict' ;
2
2
3
- const yargs = require ( 'yargs/yargs' ) ;
4
3
const flatten = require ( 'flat' ) ;
5
4
const camelcase = require ( 'camelcase' ) ;
6
5
const decamelize = require ( 'decamelize' ) ;
@@ -24,6 +23,40 @@ function keyToFlag(key) {
24
23
return key . length === 1 ? `-${ key } ` : `--${ key } ` ;
25
24
}
26
25
26
+ function parseCommand ( cmd ) {
27
+ const extraSpacesStrippedCommand = cmd . replace ( / \s { 2 , } / g, ' ' ) ;
28
+ const splitCommand = extraSpacesStrippedCommand . split ( / \s + (? ! [ ^ [ ] * ] | [ ^ < ] * > ) / ) ;
29
+ const bregex = / \. * [ \] [ < > ] / g;
30
+ const firstCommand = splitCommand . shift ( ) ;
31
+
32
+ if ( ! firstCommand ) { throw new Error ( `No command found in: ${ cmd } ` ) ; }
33
+ const parsedCommand = {
34
+ cmd : firstCommand . replace ( bregex , '' ) ,
35
+ demanded : [ ] ,
36
+ optional : [ ] ,
37
+ } ;
38
+
39
+ splitCommand . forEach ( ( cmd , i ) => {
40
+ let variadic = false ;
41
+
42
+ cmd = cmd . replace ( / \s / g, '' ) ;
43
+ if ( / \. + [ \] > ] / . test ( cmd ) && i === splitCommand . length - 1 ) { variadic = true ; }
44
+ if ( / ^ \[ / . test ( cmd ) ) {
45
+ parsedCommand . optional . push ( {
46
+ cmd : cmd . replace ( bregex , '' ) . split ( '|' ) ,
47
+ variadic,
48
+ } ) ;
49
+ } else {
50
+ parsedCommand . demanded . push ( {
51
+ cmd : cmd . replace ( bregex , '' ) . split ( '|' ) ,
52
+ variadic,
53
+ } ) ;
54
+ }
55
+ } ) ;
56
+
57
+ return parsedCommand ;
58
+ }
59
+
27
60
function unparseOption ( key , value , unparsed ) {
28
61
if ( typeof value === 'string' ) {
29
62
unparsed . push ( keyToFlag ( key ) , value ) ;
@@ -54,9 +87,7 @@ function unparsePositional(argv, options, unparsed) {
54
87
// e.g.: build <first> <second> <rest...>
55
88
if ( options . command ) {
56
89
const { 0 : cmd , index } = options . command . match ( / [ ^ < [ ] * / ) ;
57
- const { demanded, optional } = yargs ( )
58
- . getCommandInstance ( )
59
- . parseCommand ( `foo ${ options . command . substr ( index + cmd . length ) } ` ) ;
90
+ const { demanded, optional } = parseCommand ( `foo ${ options . command . substr ( index + cmd . length ) } ` ) ;
60
91
61
92
// Push command (can be a deep command)
62
93
unparsed . push ( ...cmd . trim ( ) . split ( / \s + / ) ) ;
0 commit comments