@@ -86,19 +86,16 @@ function Usage (yargs) {
86
86
example [ 0 ] = example [ 0 ] . replace ( / \$ 0 / g, yargs . $0 ) ;
87
87
} ) ;
88
88
89
- var commandlen = longest ( examples . map ( function ( a ) {
90
- return a [ 0 ] ;
91
- } ) ) ;
92
-
93
- var exampleLines = examples . map ( function ( example ) {
94
- var command = example [ 0 ] ;
95
- var description = example [ 1 ] ;
96
- command += Array ( commandlen + 5 - command . length ) . join ( ' ' ) ;
97
- return ' ' + command + description ;
89
+ var examplesTable = { } ;
90
+ examples . forEach ( function ( example ) {
91
+ examplesTable [ example [ 0 ] ] = {
92
+ desc : example [ 1 ] ,
93
+ extra : ''
94
+ } ;
98
95
} ) ;
99
96
100
- exampleLines . push ( '' ) ;
101
- help = exampleLines . concat ( help ) ;
97
+ help = formatTable ( examplesTable , 5 ) . concat ( help ) ;
98
+ help . push ( '' ) ;
102
99
help . unshift ( 'Examples:' ) ;
103
100
}
104
101
@@ -114,6 +111,7 @@ function Usage (yargs) {
114
111
return - 1 == ( options . alias [ alias ] || [ ] ) . indexOf ( key ) ;
115
112
} ) ;
116
113
} ) ;
114
+
117
115
var switches = keys . reduce ( function ( acc , key ) {
118
116
acc [ key ] = [ key ] . concat ( options . alias [ key ] || [ ] )
119
117
. map ( function ( sw ) {
@@ -124,44 +122,17 @@ function Usage (yargs) {
124
122
return acc ;
125
123
} , { } ) ;
126
124
127
- var switchlen = longest ( Object . keys ( switches ) . map ( function ( s ) {
128
- return switches [ s ] || '' ;
129
- } ) ) ;
130
-
131
- var desclen = longest ( Object . keys ( descriptions ) . map ( function ( d ) {
132
- return descriptions [ d ] || '' ;
133
- } ) ) ;
134
-
125
+ var switchTable = { } ;
135
126
keys . forEach ( function ( key ) {
136
127
var kswitch = switches [ key ] ;
137
128
var desc = descriptions [ key ] || '' ;
138
-
139
- if ( wrap ) {
140
- desc = wordwrap ( switchlen + 4 , wrap ) ( desc )
141
- . slice ( switchlen + 4 )
142
- ;
143
- }
144
-
145
- var spadding = new Array (
146
- Math . max ( switchlen - kswitch . length + 3 , 0 )
147
- ) . join ( ' ' ) ;
148
-
149
- var dpadding = new Array (
150
- Math . max ( desclen - desc . length + 1 , 0 )
151
- ) . join ( ' ' ) ;
152
-
153
129
var type = null ;
154
130
155
131
if ( options . boolean [ key ] ) type = '[boolean]' ;
156
132
if ( options . count [ key ] ) type = '[count]' ;
157
133
if ( options . string [ key ] ) type = '[string]' ;
158
134
if ( options . normalize [ key ] ) type = '[string]' ;
159
135
160
- if ( ! wrap && dpadding . length > 0 ) {
161
- desc += dpadding ;
162
- }
163
-
164
- var prelude = ' ' + kswitch + spadding ;
165
136
var extra = [
166
137
type ,
167
138
demanded [ key ]
@@ -174,6 +145,59 @@ function Usage (yargs) {
174
145
: null
175
146
] . filter ( Boolean ) . join ( ' ' ) ;
176
147
148
+ switchTable [ kswitch ] = {
149
+ desc : desc ,
150
+ extra : extra
151
+ } ;
152
+ } ) ;
153
+ help . push . apply ( help , formatTable ( switchTable , 3 ) ) ;
154
+
155
+ if ( keys . length ) help . push ( '' ) ;
156
+ return help . join ( '\n' ) ;
157
+ } ;
158
+
159
+ self . showHelp = function ( level ) {
160
+ level = level || 'error' ;
161
+ console [ level ] ( self . help ( ) ) ;
162
+ }
163
+
164
+ // word-wrapped two-column layout used by
165
+ // examples and options.
166
+ function formatTable ( table , padding ) {
167
+ var output = [ ] ;
168
+
169
+ // determine lengths of left column, and
170
+ // description column.
171
+ var llen = longest ( Object . keys ( table ) ) ;
172
+
173
+ var desclen = longest ( Object . keys ( table ) . map ( function ( k ) {
174
+ return table [ k ] . desc ;
175
+ } ) ) ;
176
+
177
+ Object . keys ( table ) . forEach ( function ( left ) {
178
+ var desc = table [ left ] . desc ,
179
+ extra = table [ left ] . extra ;
180
+
181
+ if ( wrap ) {
182
+ desc = wordwrap ( llen + padding + 1 , wrap ) ( desc )
183
+ . slice ( llen + padding + 1 )
184
+ ;
185
+ }
186
+
187
+ var lpadding = new Array (
188
+ Math . max ( llen - left . length + padding , 0 )
189
+ ) . join ( ' ' ) ;
190
+
191
+ var dpadding = new Array (
192
+ Math . max ( desclen - desc . length + 1 , 0 )
193
+ ) . join ( ' ' ) ;
194
+
195
+ if ( ! wrap && dpadding . length > 0 ) {
196
+ desc += dpadding ;
197
+ }
198
+
199
+ var prelude = ' ' + left + lpadding ;
200
+
177
201
var body = [ desc , extra ] . filter ( Boolean ) . join ( ' ' ) ;
178
202
179
203
if ( wrap ) {
@@ -182,7 +206,7 @@ function Usage (yargs) {
182
206
+ ( dlines . length === 1 ? prelude . length : 0 )
183
207
184
208
if ( extra . length > wrap ) {
185
- body = desc + '\n' + wordwrap ( switchlen + 4 , wrap ) ( extra )
209
+ body = desc + '\n' + wordwrap ( llen + 4 , wrap ) ( extra )
186
210
} else {
187
211
body = desc + ( dlen + extra . length > wrap - 2
188
212
? '\n'
@@ -194,18 +218,13 @@ function Usage (yargs) {
194
218
}
195
219
}
196
220
197
- help . push ( prelude + body ) ;
221
+ output . push ( prelude + body ) ;
198
222
} ) ;
199
223
200
- if ( keys . length ) help . push ( '' ) ;
201
- return help . join ( '\n' ) ;
202
- } ;
203
-
204
- self . showHelp = function ( level ) {
205
- level = level || 'error' ;
206
- console [ level ] ( self . help ( ) ) ;
224
+ return output ;
207
225
}
208
226
227
+ // find longest string in array of strings.
209
228
function longest ( xs ) {
210
229
return Math . max . apply (
211
230
null ,
0 commit comments