@@ -107,46 +107,52 @@ interface CliOptions {
107
107
[ k : string ] : string | boolean | number | undefined
108
108
}
109
109
110
- export function parseOptions ( options ?: CliOptions ) : object {
111
- if ( ! options ) {
112
- options = { }
113
- const args = process . argv . slice ( 2 )
114
-
115
- for ( let i = 0 ; i < args . length ; i ++ ) {
116
- const arg = args [ i ]
117
-
118
- if ( arg ?. startsWith ( '--' ) ) {
119
- const key = arg . substring ( 2 ) // remove the --
120
- const camelCaseKey = key . replace ( / - ( [ a - z ] ) / gi, g => ( g [ 1 ] ? g [ 1 ] . toUpperCase ( ) : '' ) ) // convert kebab-case to camelCase
121
-
122
- if ( i + 1 < args . length && ( args [ i + 1 ] === 'true' || args [ i + 1 ] === 'false' ) ) { // if the next arg is a boolean
110
+ export function parseOptions ( options ?: CliOptions ) : CliOptions | undefined {
111
+ options = options || { }
112
+ const args = process . argv . slice ( 2 )
113
+
114
+ for ( let i = 0 ; i < args . length ; i ++ ) {
115
+ const arg = args [ i ]
116
+ if ( arg ?. startsWith ( '--' ) ) {
117
+ const key = arg . substring ( 2 ) // remove the --
118
+ const camelCaseKey = key . replace (
119
+ / - ( [ a - z ] ) / gi,
120
+ g => ( g [ 1 ] ? g [ 1 ] . toUpperCase ( ) : '' ) , // convert kebab-case to camelCase
121
+ )
122
+
123
+ if ( i + 1 < args . length ) { // if the next arg exists
124
+ if ( args [ i + 1 ] === 'true' || args [ i + 1 ] === 'false' ) { // if the next arg is a boolean
123
125
options [ camelCaseKey ] = args [ i + 1 ] === 'true' // set the value to the boolean
124
126
i ++
125
127
}
126
128
else {
127
- options [ camelCaseKey ] = true
129
+ options [ camelCaseKey ] = args [ i + 1 ]
130
+ i ++
128
131
}
129
132
}
133
+ else {
134
+ options [ camelCaseKey ] = true
135
+ }
130
136
}
131
-
132
- return options
133
137
}
134
138
139
+ // if options has no keys, return undefined, e.g. `buddy release`
140
+ if ( Object . keys ( options ) . length === 0 )
141
+ return undefined
142
+
135
143
// convert the string 'true' or 'false' to a boolean
136
144
Object . keys ( options ) . forEach ( ( key ) => {
137
- let value
138
- if ( options )
139
- value = options [ key ]
145
+ if ( ! options )
146
+ return
140
147
141
- if ( value === 'true' || value === 'false' ) {
142
- if ( options )
143
- options [ key ] = value === 'true'
144
- }
148
+ const value = options [ key ]
149
+
150
+ if ( value === 'true' || value === 'false' )
151
+ options [ key ] = value === 'true'
145
152
} )
146
153
147
154
return options
148
155
}
149
-
150
156
// interface BuddyOptions {
151
157
// dryRun?: boolean
152
158
// verbose?: boolean
0 commit comments