@@ -12,7 +12,7 @@ module.exports = class Option {
12
12
: { default : opt1 } ;
13
13
14
14
return {
15
- default : ! ensureFunction ( raw . action ) ? raw . default : undefined ,
15
+ default : raw . default ,
16
16
normalize : ensureFunction ( raw . normalize , self ) ,
17
17
shortcut : ensureFunction ( raw . shortcut ) ,
18
18
action : ensureFunction ( raw . action ) ,
@@ -22,44 +22,43 @@ module.exports = class Option {
22
22
23
23
static parseUsage ( usage ) {
24
24
const [ m , short , long = '' ] = usage . trim ( )
25
- . match ( / ^ (?: ( - [ a - z \d ] ) (?: \s * , \s * | \s + ) ) ? ( - - [ a - z ] [ a - z \d \- \_ ] * ) ? \s * / i) || [ ] ;
25
+ . match ( / ^ (?: ( - [ a - z \d ] ) (?: \s * , \s * | \s + ) ) ? ( - - [ a - z ] [ a - z \d \- \_ ] * ) \s * / i) || [ ] ;
26
26
27
- if ( ! long ) {
27
+ if ( ! m ) {
28
28
throw new Error ( `Usage has no long name: ${ usage } ` ) ;
29
29
}
30
30
31
- let name = long . replace ( / ^ - - ( n o - ) ? / , '' ) ; // --no-flag - invert value if flag is boolean
32
- let defValue = / - - n o - / . test ( long ) ;
33
31
let params = new Params ( usage . slice ( m . length ) , `option usage: ${ usage } ` ) ;
34
32
35
- if ( params . maxCount > 0 ) {
36
- name = long . slice ( 2 ) ;
37
- defValue = undefined ;
38
- }
39
-
40
- return { short, long, name, params, defValue } ;
33
+ return { short, long, params } ;
41
34
}
42
35
43
- constructor ( usage , description , ...options ) {
44
- const { short, long, name, params, defValue } = Option . parseUsage ( usage ) ;
36
+ constructor ( usage , description , ...rawOptions ) {
37
+ const { short, long, params } = Option . parseUsage ( usage ) ;
38
+ const options = Option . normalizeOptions ( ...rawOptions ) ;
39
+
40
+ const isBool = params . maxCount === 0 && ! options . action ;
41
+ let name = camelcase ( long . replace ( isBool ? / ^ - - ( n o - ) ? / : / ^ - - / , '' ) ) ; // --no-flag - invert value if flag is boolean
42
+
43
+ if ( options . action ) {
44
+ options . default = undefined ;
45
+ } else if ( isBool ) {
46
+ options . normalize = Boolean ;
47
+ options . default = long . startsWith ( '--no-' ) ;
48
+ }
45
49
46
50
// names
47
51
this . short = short ;
48
52
this . long = long ;
49
- this . name = camelcase ( name ) ;
53
+ this . name = name ;
50
54
51
55
// meta
52
56
this . usage = usage . trim ( ) ;
53
57
this . description = description || '' ;
54
58
55
59
// attributes
56
60
this . params = params ;
57
- Object . assign ( this , Option . normalizeOptions ( ...options ) ) ;
58
-
59
- // ignore defValue from config for boolean options
60
- if ( typeof defValue === 'boolean' && ! this . action ) {
61
- this . default = defValue ;
62
- }
61
+ Object . assign ( this , options ) ;
63
62
}
64
63
65
64
messageRef ( ) {
0 commit comments