@@ -7,7 +7,10 @@ interface ParsedArgv {
7
7
}
8
8
}
9
9
10
- function isLongOption ( arg : string ) : boolean {
10
+ function isLongOption ( arg ?: string ) : boolean {
11
+ if ( ! arg )
12
+ return false
13
+
11
14
return arg . startsWith ( '--' )
12
15
}
13
16
@@ -32,33 +35,37 @@ function parseValue(value: string): string | boolean | number {
32
35
function parseLongOption ( arg : string , argv : string [ ] , index : number , options : { [ k : string ] : string | boolean | number } ) : number {
33
36
const [ key , value ] = arg . slice ( 2 ) . split ( '=' )
34
37
if ( value !== undefined ) {
35
- options [ key ] = parseValue ( value )
38
+ options [ key as string ] = parseValue ( value )
36
39
}
37
- else if ( index + 1 < argv . length && ! argv [ index + 1 ] . startsWith ( '-' ) ) {
38
- options [ key ] = argv [ index + 1 ]
40
+ else if ( index + 1 < argv . length && ! argv [ index + 1 ] ! . startsWith ( '-' ) ) {
41
+ options [ key as string ] = argv [ index + 1 ] as string
39
42
index ++
40
43
}
41
44
else {
42
- options [ key ] = true
45
+ options [ key as string ] = true
43
46
}
44
47
return index
45
48
}
46
49
47
50
function parseShortOption ( arg : string , argv : string [ ] , index : number , options : { [ k : string ] : string | boolean | number } ) : number {
48
51
const [ key , value ] = arg . slice ( 1 ) . split ( '=' )
49
52
50
- if ( value !== undefined ) {
53
+ // Check if key is undefined and handle it
54
+ if ( key === undefined )
55
+ return index
56
+
57
+ if ( value !== undefined && key !== undefined ) {
51
58
for ( let j = 0 ; j < key . length ; j ++ )
52
- options [ key [ j ] ] = parseValue ( value )
59
+ options [ key [ j ] as string ] = parseValue ( value )
53
60
}
54
61
else {
55
62
for ( let j = 0 ; j < key . length ; j ++ ) {
56
- if ( index + 1 < argv . length && j === key . length - 1 && ! argv [ index + 1 ] . startsWith ( '-' ) ) {
57
- options [ key [ j ] ] = parseValue ( argv [ index + 1 ] )
63
+ if ( index + 1 < argv . length && j === key . length - 1 && ! argv [ index + 1 ] ! . startsWith ( '-' ) ) {
64
+ options [ key [ j ] as string ] = parseValue ( argv [ index + 1 ] ! )
58
65
index ++
59
66
}
60
67
else {
61
- options [ key [ j ] ] = true
68
+ options [ key [ j ] as string ] = true
62
69
}
63
70
}
64
71
}
@@ -75,6 +82,8 @@ export function parseArgv(argv?: string[]): ParsedArgv {
75
82
76
83
for ( let i = 0 ; i < argv . length ; i ++ ) {
77
84
const arg = argv [ i ]
85
+ if ( ! arg )
86
+ continue
78
87
if ( isLongOption ( arg ) )
79
88
i = parseLongOption ( arg , argv , i , options )
80
89
else if ( isShortOption ( arg ) )
@@ -102,11 +111,14 @@ export function parseOptions(options?: CliOptions): object {
102
111
if ( ! options ) {
103
112
options = { }
104
113
const args = process . argv . slice ( 2 )
114
+
105
115
for ( let i = 0 ; i < args . length ; i ++ ) {
106
116
const arg = args [ i ]
117
+
107
118
if ( arg ?. startsWith ( '--' ) ) {
108
119
const key = arg . substring ( 2 ) // remove the --
109
120
const camelCaseKey = key . replace ( / - ( [ a - z ] ) / gi, g => ( g [ 1 ] ? g [ 1 ] . toUpperCase ( ) : '' ) ) // convert kebab-case to camelCase
121
+
110
122
if ( i + 1 < args . length && ( args [ i + 1 ] === 'true' || args [ i + 1 ] === 'false' ) ) { // if the next arg is a boolean
111
123
options [ camelCaseKey ] = args [ i + 1 ] === 'true' // set the value to the boolean
112
124
i ++
@@ -116,6 +128,7 @@ export function parseOptions(options?: CliOptions): object {
116
128
}
117
129
}
118
130
}
131
+
119
132
return options
120
133
}
121
134
@@ -134,6 +147,10 @@ export function parseOptions(options?: CliOptions): object {
134
147
return options
135
148
}
136
149
150
+ // interface BuddyOptions {
151
+ // dryRun?: boolean
152
+ // verbose?: boolean
153
+ // }
137
154
export function buddyOptions ( options ?: any ) : string {
138
155
if ( ! options ) {
139
156
options = process . argv . slice ( 2 )
@@ -143,10 +160,12 @@ export function buddyOptions(options?: any): string {
143
160
if ( options [ 0 ] && ! options [ 0 ] . startsWith ( '-' ) )
144
161
options . shift ( )
145
162
}
163
+
146
164
if ( options ?. verbose ) {
147
165
log . debug ( 'process.argv' , process . argv )
148
166
log . debug ( 'process.argv.slice(2)' , process . argv . slice ( 2 ) )
149
167
log . debug ( 'options inside buddyOptions' , options )
150
168
}
169
+
151
170
return options . join ( ' ' )
152
171
}
0 commit comments