1- //@ts -check
1+ // @ts -check
2+
23class StringBuilder {
34 constructor ( ) {
45 this . val = "" ;
@@ -13,23 +14,32 @@ class StringBuilder {
1314 return this ;
1415 }
1516}
17+
1618class ArgError extends Error { }
1719
1820/**
19- *
2021 * @param {string } s
2122 */
2223function bad_arg ( s ) {
2324 throw new ArgError ( s ) ;
2425}
2526
2627/**
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
3343 * @param {StringBuilder } b
3444 * @param {string } usage
3545 * @param {specs } specs
@@ -39,33 +49,32 @@ function usage_b(b, usage, specs) {
3949 if ( specs . length === 0 ) {
4050 return ;
4151 }
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 ) {
4555 if ( key . length > max_col ) {
4656 max_col = key . length ;
4757 }
4858 }
4959 for ( let i = 0 ; i < specs . length ; i ++ ) {
50- let [ key , _ , doc ] = specs [ i ] ;
60+ const [ key , _ , doc ] = specs [ i ] ;
5161 if ( ! doc . startsWith ( "*internal*" ) ) {
5262 b . add ( " " )
5363 . add ( key )
5464 . add ( " " . repeat ( max_col - key . length + 2 ) ) ;
5565 let cur = 0 ;
56- let doc_length = doc . length ;
66+ const doc_length = doc . length ;
5767 while ( cur < doc_length ) {
5868 if ( cur !== 0 ) {
5969 b . add ( "\n" ) . add ( " " . repeat ( max_col + 4 ) ) ;
6070 }
61- let i = doc . indexOf ( "\n" , cur ) ;
71+ const i = doc . indexOf ( "\n" , cur ) ;
6272 if ( i < 0 ) {
6373 b . add ( doc . substring ( cur ) ) ;
6474 break ;
65- } else {
66- b . add ( doc . substr ( cur , i - cur ) ) ;
67- cur = i + 1 ;
6875 }
76+ b . add ( doc . substring ( cur , i - cur ) ) ;
77+ cur = i + 1 ;
6978 }
7079 b . add ( "\n" ) ;
7180 }
@@ -79,7 +88,7 @@ function usage_b(b, usage, specs) {
7988 * @param {specs } specs
8089 */
8190function stop_raise ( usage , error , specs ) {
82- var b = new StringBuilder ( ) ;
91+ const b = new StringBuilder ( ) ;
8392 switch ( error . kind ) {
8493 case "Unknown" :
8594 if ( [ "-help" , "--help" , "-h" ] . includes ( error . data ) ) {
@@ -89,8 +98,10 @@ function stop_raise(usage, error, specs) {
8998 } else {
9099 b . add ( `Unknown option "${ error . data } ".\n'` ) ;
91100 }
101+ break ;
92102 case "Missing" :
93103 b . add ( `Option "${ error . data } " needs an argument.\n'` ) ;
104+ break ;
94105 }
95106 usage_b ( b , usage , specs ) ;
96107 bad_arg ( b . val ) ;
@@ -114,15 +125,15 @@ function parse_exn(
114125 // first 3 are [node, rescript, subcommand]
115126 finish = argv . length ,
116127) {
117- var current = start ;
118- var list = [ ] ;
128+ let current = start ;
129+ const list = [ ] ;
119130 while ( current < finish ) {
120- let s = argv [ current ] ;
131+ const s = argv [ current ] ;
121132 ++ current ;
122133 if ( s !== "" && s [ 0 ] === "-" ) {
123- var out = specs . find ( ( [ flag ] ) => flag === s ) ;
134+ const out = specs . find ( ( [ flag ] ) => flag === s ) ;
124135 if ( out !== undefined ) {
125- let [ _ , action ] = out ;
136+ const [ _ , action ] = out ;
126137 switch ( action . kind ) {
127138 case "Unit" :
128139 switch ( action . data . kind ) {
@@ -139,7 +150,7 @@ function parse_exn(
139150 if ( current >= finish ) {
140151 stop_raise ( usage , { kind : "Missing" , data : s } , specs ) ;
141152 } else {
142- let arg = argv [ current ] ;
153+ const arg = argv [ current ] ;
143154 ++ current ;
144155 switch ( action . data . kind ) {
145156 case "String_call" :
0 commit comments