1
- //@ts -check
1
+ // @ts -check
2
+
2
3
class StringBuilder {
3
4
constructor ( ) {
4
5
this . val = "" ;
@@ -13,23 +14,32 @@ class StringBuilder {
13
14
return this ;
14
15
}
15
16
}
17
+
16
18
class ArgError extends Error { }
17
19
18
20
/**
19
- *
20
21
* @param {string } s
21
22
*/
22
23
function bad_arg ( s ) {
23
24
throw new ArgError ( s ) ;
24
25
}
25
26
26
27
/**
27
- * @typedef {{val : string} } stringref
28
- * @typedef {{val : boolean} } boolref
29
- * @typedef {{kind:"Unit_call",data : ()=>void } | {kind : "Unit_set", data : boolref} } unit_action
30
- * @typedef {{kind:"String_call",data:(s : string)=>void} | {kind : "String_set",data: stringref} } string_action
31
- * @typedef {{kind:"Unit",data : unit_action } | {kind:"String", data: string_action} } action
32
- * @typedef {Array<[string,action,string]> } specs
28
+ * @typedef {{ val: string } } stringref
29
+ * @typedef {{ val: boolean } } boolref
30
+ * @typedef {(
31
+ * | { kind: "Unit_call", data: () => void }
32
+ * | { kind: "Unit_set", data: boolref }
33
+ * )} unit_action
34
+ * @typedef {(
35
+ * | { kind: "String_call", data: (s: string) => void }
36
+ * | {kind: "String_set", data: stringref }
37
+ * )} string_action
38
+ * @typedef {(
39
+ * | { kind: "Unit", data: unit_action }
40
+ * | { kind: "String", data: string_action}
41
+ * )} action
42
+ * @typedef {Array<[string, action, string]> } specs
33
43
* @param {StringBuilder } b
34
44
* @param {string } usage
35
45
* @param {specs } specs
@@ -39,33 +49,32 @@ function usage_b(b, usage, specs) {
39
49
if ( specs . length === 0 ) {
40
50
return ;
41
51
}
42
- b . add ( ` \nOptions:\n` ) ;
43
- var max_col = 0 ;
44
- for ( let [ key ] of specs ) {
52
+ b . add ( " \nOptions:\n" ) ;
53
+ let max_col = 0 ;
54
+ for ( const [ key ] of specs ) {
45
55
if ( key . length > max_col ) {
46
56
max_col = key . length ;
47
57
}
48
58
}
49
59
for ( let i = 0 ; i < specs . length ; i ++ ) {
50
- let [ key , _ , doc ] = specs [ i ] ;
60
+ const [ key , _ , doc ] = specs [ i ] ;
51
61
if ( ! doc . startsWith ( "*internal*" ) ) {
52
62
b . add ( " " )
53
63
. add ( key )
54
64
. add ( " " . repeat ( max_col - key . length + 2 ) ) ;
55
65
let cur = 0 ;
56
- let doc_length = doc . length ;
66
+ const doc_length = doc . length ;
57
67
while ( cur < doc_length ) {
58
68
if ( cur !== 0 ) {
59
69
b . add ( "\n" ) . add ( " " . repeat ( max_col + 4 ) ) ;
60
70
}
61
- let i = doc . indexOf ( "\n" , cur ) ;
71
+ const i = doc . indexOf ( "\n" , cur ) ;
62
72
if ( i < 0 ) {
63
73
b . add ( doc . substring ( cur ) ) ;
64
74
break ;
65
- } else {
66
- b . add ( doc . substr ( cur , i - cur ) ) ;
67
- cur = i + 1 ;
68
75
}
76
+ b . add ( doc . substring ( cur , i - cur ) ) ;
77
+ cur = i + 1 ;
69
78
}
70
79
b . add ( "\n" ) ;
71
80
}
@@ -79,7 +88,7 @@ function usage_b(b, usage, specs) {
79
88
* @param {specs } specs
80
89
*/
81
90
function stop_raise ( usage , error , specs ) {
82
- var b = new StringBuilder ( ) ;
91
+ const b = new StringBuilder ( ) ;
83
92
switch ( error . kind ) {
84
93
case "Unknown" :
85
94
if ( [ "-help" , "--help" , "-h" ] . includes ( error . data ) ) {
@@ -89,8 +98,10 @@ function stop_raise(usage, error, specs) {
89
98
} else {
90
99
b . add ( `Unknown option "${ error . data } ".\n'` ) ;
91
100
}
101
+ break ;
92
102
case "Missing" :
93
103
b . add ( `Option "${ error . data } " needs an argument.\n'` ) ;
104
+ break ;
94
105
}
95
106
usage_b ( b , usage , specs ) ;
96
107
bad_arg ( b . val ) ;
@@ -114,15 +125,15 @@ function parse_exn(
114
125
// first 3 are [node, rescript, subcommand]
115
126
finish = argv . length ,
116
127
) {
117
- var current = start ;
118
- var list = [ ] ;
128
+ let current = start ;
129
+ const list = [ ] ;
119
130
while ( current < finish ) {
120
- let s = argv [ current ] ;
131
+ const s = argv [ current ] ;
121
132
++ current ;
122
133
if ( s !== "" && s [ 0 ] === "-" ) {
123
- var out = specs . find ( ( [ flag ] ) => flag === s ) ;
134
+ const out = specs . find ( ( [ flag ] ) => flag === s ) ;
124
135
if ( out !== undefined ) {
125
- let [ _ , action ] = out ;
136
+ const [ _ , action ] = out ;
126
137
switch ( action . kind ) {
127
138
case "Unit" :
128
139
switch ( action . data . kind ) {
@@ -139,7 +150,7 @@ function parse_exn(
139
150
if ( current >= finish ) {
140
151
stop_raise ( usage , { kind : "Missing" , data : s } , specs ) ;
141
152
} else {
142
- let arg = argv [ current ] ;
153
+ const arg = argv [ current ] ;
143
154
++ current ;
144
155
switch ( action . data . kind ) {
145
156
case "String_call" :
0 commit comments