@@ -53,18 +53,24 @@ def source
53
53
end
54
54
end
55
55
56
- # An item of work that corresponds to the stdin content.
57
- class STDINItem
56
+ # An item of work that corresponds to a script content passed via the command line.
57
+ class ScriptItem
58
+ FILEPATH = :script
59
+
60
+ def initialize ( source )
61
+ @source = source
62
+ end
63
+
58
64
def handler
59
65
HANDLERS [ ".rb" ]
60
66
end
61
67
62
68
def filepath
63
- :stdin
69
+ FILEPATH
64
70
end
65
71
66
72
def source
67
- $stdin . read
73
+ @source
68
74
end
69
75
end
70
76
@@ -191,7 +197,7 @@ def run(item)
191
197
192
198
source = item . source
193
199
formatted = item . handler . format ( source , options . print_width )
194
- File . write ( filepath , formatted ) if filepath != :stdin
200
+ File . write ( filepath , formatted ) if FileItem === item
195
201
196
202
color = source == formatted ? Color . gray ( filepath ) : filepath
197
203
delta = ( ( Time . now - start ) * 1000 ) . round
@@ -206,25 +212,25 @@ def run(item)
206
212
# The help message displayed if the input arguments are not correctly
207
213
# ordered or formatted.
208
214
HELP = <<~HELP
209
- #{ Color . bold ( "stree ast [--plugins=...] [--print-width=NUMBER] FILE" ) }
215
+ #{ Color . bold ( "stree ast [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE" ) }
210
216
Print out the AST corresponding to the given files
211
217
212
- #{ Color . bold ( "stree check [--plugins=...] [--print-width=NUMBER] FILE" ) }
218
+ #{ Color . bold ( "stree check [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE" ) }
213
219
Check that the given files are formatted as syntax tree would format them
214
220
215
- #{ Color . bold ( "stree debug [--plugins=...] [--print-width=NUMBER] FILE" ) }
221
+ #{ Color . bold ( "stree debug [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE" ) }
216
222
Check that the given files can be formatted idempotently
217
223
218
- #{ Color . bold ( "stree doc [--plugins=...] FILE" ) }
224
+ #{ Color . bold ( "stree doc [--plugins=...] [-e SCRIPT] FILE" ) }
219
225
Print out the doc tree that would be used to format the given files
220
226
221
- #{ Color . bold ( "stree format [--plugins=...] [--print-width=NUMBER] FILE" ) }
227
+ #{ Color . bold ( "stree format [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE" ) }
222
228
Print out the formatted version of the given files
223
229
224
- #{ Color . bold ( "stree json [--plugins=...] FILE" ) }
230
+ #{ Color . bold ( "stree json [--plugins=...] [-e SCRIPT] FILE" ) }
225
231
Print out the JSON representation of the given files
226
232
227
- #{ Color . bold ( "stree match [--plugins=...] FILE" ) }
233
+ #{ Color . bold ( "stree match [--plugins=...] [-e SCRIPT] FILE" ) }
228
234
Print out a pattern-matching Ruby expression that would match the given files
229
235
230
236
#{ Color . bold ( "stree help" ) }
@@ -236,28 +242,32 @@ def run(item)
236
242
#{ Color . bold ( "stree version" ) }
237
243
Output the current version of syntax tree
238
244
239
- #{ Color . bold ( "stree write [--plugins=...] [--print-width=NUMBER] FILE" ) }
245
+ #{ Color . bold ( "stree write [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE" ) }
240
246
Read, format, and write back the source of the given files
241
247
242
248
--plugins=...
243
249
A comma-separated list of plugins to load.
244
250
245
251
--print-width=NUMBER
246
252
The maximum line width to use when formatting.
253
+
254
+ -e SCRIPT
255
+ Parse an inline Ruby string.
247
256
HELP
248
257
249
258
# This represents all of the options that can be passed to the CLI. It is
250
259
# responsible for parsing the list and then returning the file paths at the
251
260
# end.
252
261
class Options
253
- attr_reader :print_width
262
+ attr_reader :print_width , :scripts
254
263
255
264
def initialize ( print_width : DEFAULT_PRINT_WIDTH )
256
265
@print_width = print_width
266
+ @scripts = [ ]
257
267
end
258
268
259
269
def parse ( arguments )
260
- parser . parse ( arguments )
270
+ parser . parse! ( arguments )
261
271
end
262
272
263
273
private
@@ -283,6 +293,12 @@ def parser
283
293
opts . on ( "--print-width=NUMBER" , Integer ) do |print_width |
284
294
@print_width = print_width
285
295
end
296
+
297
+ # If there is a script specified on the command line, then parse
298
+ # it and add it to the list of scripts to run.
299
+ opts . on ( "-e SCRIPT" ) do |script |
300
+ @scripts << script
301
+ end
286
302
end
287
303
end
288
304
end
@@ -361,7 +377,7 @@ def run(argv)
361
377
362
378
# If we're not reading from stdin and the user didn't supply and
363
379
# filepaths to be read, then we exit with the usage message.
364
- if $stdin. tty? && arguments . empty?
380
+ if $stdin. tty? && arguments . empty? && options . scripts . empty?
365
381
warn ( HELP )
366
382
return 1
367
383
end
@@ -371,16 +387,19 @@ def run(argv)
371
387
372
388
# If we're reading from stdin, then we'll just add the stdin object to
373
389
# the queue. Otherwise, we'll add each of the filepaths to the queue.
374
- if $stdin. tty? || arguments . any?
390
+ if $stdin. tty? && ( arguments . any? || options . scripts . any? )
375
391
arguments . each do |pattern |
376
392
Dir
377
393
. glob ( pattern )
378
394
. each do |filepath |
379
395
queue << FileItem . new ( filepath ) if File . file? ( filepath )
380
396
end
381
397
end
398
+ options . scripts . each do |script |
399
+ queue << ScriptItem . new ( script )
400
+ end
382
401
else
383
- queue << STDINItem . new
402
+ queue << ScriptItem . new ( $stdin . read )
384
403
end
385
404
386
405
# At the end, we're going to return whether or not this worker ever
0 commit comments