@@ -212,25 +212,25 @@ def run(item)
212
212
# The help message displayed if the input arguments are not correctly
213
213
# ordered or formatted.
214
214
HELP = <<~HELP
215
- #{ Color . bold ( "stree ast [--plugins=...] [--print-width=NUMBER] FILE" ) }
215
+ #{ Color . bold ( "stree ast [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE" ) }
216
216
Print out the AST corresponding to the given files
217
217
218
- #{ Color . bold ( "stree check [--plugins=...] [--print-width=NUMBER] FILE" ) }
218
+ #{ Color . bold ( "stree check [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE" ) }
219
219
Check that the given files are formatted as syntax tree would format them
220
220
221
- #{ Color . bold ( "stree debug [--plugins=...] [--print-width=NUMBER] FILE" ) }
221
+ #{ Color . bold ( "stree debug [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE" ) }
222
222
Check that the given files can be formatted idempotently
223
223
224
- #{ Color . bold ( "stree doc [--plugins=...] FILE" ) }
224
+ #{ Color . bold ( "stree doc [--plugins=...] [-e SCRIPT] FILE" ) }
225
225
Print out the doc tree that would be used to format the given files
226
226
227
- #{ Color . bold ( "stree format [--plugins=...] [--print-width=NUMBER] FILE" ) }
227
+ #{ Color . bold ( "stree format [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE" ) }
228
228
Print out the formatted version of the given files
229
229
230
- #{ Color . bold ( "stree json [--plugins=...] FILE" ) }
230
+ #{ Color . bold ( "stree json [--plugins=...] [-e SCRIPT] FILE" ) }
231
231
Print out the JSON representation of the given files
232
232
233
- #{ Color . bold ( "stree match [--plugins=...] FILE" ) }
233
+ #{ Color . bold ( "stree match [--plugins=...] [-e SCRIPT] FILE" ) }
234
234
Print out a pattern-matching Ruby expression that would match the given files
235
235
236
236
#{ Color . bold ( "stree help" ) }
@@ -242,28 +242,32 @@ def run(item)
242
242
#{ Color . bold ( "stree version" ) }
243
243
Output the current version of syntax tree
244
244
245
- #{ Color . bold ( "stree write [--plugins=...] [--print-width=NUMBER] FILE" ) }
245
+ #{ Color . bold ( "stree write [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE" ) }
246
246
Read, format, and write back the source of the given files
247
247
248
248
--plugins=...
249
249
A comma-separated list of plugins to load.
250
250
251
251
--print-width=NUMBER
252
252
The maximum line width to use when formatting.
253
+
254
+ -e SCRIPT
255
+ Parse an inline Ruby string.
253
256
HELP
254
257
255
258
# This represents all of the options that can be passed to the CLI. It is
256
259
# responsible for parsing the list and then returning the file paths at the
257
260
# end.
258
261
class Options
259
- attr_reader :print_width
262
+ attr_reader :print_width , :scripts
260
263
261
264
def initialize ( print_width : DEFAULT_PRINT_WIDTH )
262
265
@print_width = print_width
266
+ @scripts = [ ]
263
267
end
264
268
265
269
def parse ( arguments )
266
- parser . parse ( arguments )
270
+ parser . parse! ( arguments )
267
271
end
268
272
269
273
private
@@ -289,6 +293,12 @@ def parser
289
293
opts . on ( "--print-width=NUMBER" , Integer ) do |print_width |
290
294
@print_width = print_width
291
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
292
302
end
293
303
end
294
304
end
@@ -367,7 +377,7 @@ def run(argv)
367
377
368
378
# If we're not reading from stdin and the user didn't supply and
369
379
# filepaths to be read, then we exit with the usage message.
370
- if $stdin. tty? && arguments . empty?
380
+ if $stdin. tty? && arguments . empty? && options . scripts . empty?
371
381
warn ( HELP )
372
382
return 1
373
383
end
@@ -377,14 +387,17 @@ def run(argv)
377
387
378
388
# If we're reading from stdin, then we'll just add the stdin object to
379
389
# the queue. Otherwise, we'll add each of the filepaths to the queue.
380
- if $stdin. tty? || arguments . any?
390
+ if $stdin. tty? && ( arguments . any? || options . scripts . any? )
381
391
arguments . each do |pattern |
382
392
Dir
383
393
. glob ( pattern )
384
394
. each do |filepath |
385
395
queue << FileItem . new ( filepath ) if File . file? ( filepath )
386
396
end
387
397
end
398
+ options . scripts . each do |script |
399
+ queue << ScriptItem . new ( script )
400
+ end
388
401
else
389
402
queue << ScriptItem . new ( $stdin. read )
390
403
end
0 commit comments