@@ -53,10 +53,13 @@ def source
5353 end
5454 end
5555
56- # An item of work that corresponds to a script content passed via the command line.
56+ # An item of work that corresponds to a script content passed via the
57+ # command line.
5758 class ScriptItem
5859 FILEPATH = :script
5960
61+ attr_reader :source
62+
6063 def initialize ( source )
6164 @source = source
6265 end
@@ -68,10 +71,6 @@ def handler
6871 def filepath
6972 FILEPATH
7073 end
71-
72- def source
73- @source
74- end
7574 end
7675
7776 # The parent action class for the CLI that implements the basics.
@@ -197,7 +196,7 @@ def run(item)
197196
198197 source = item . source
199198 formatted = item . handler . format ( source , options . print_width )
200- File . write ( filepath , formatted ) if FileItem === item
199+ File . write ( filepath , formatted ) if item . filepath != :script
201200
202201 color = source == formatted ? Color . gray ( filepath ) : filepath
203202 delta = ( ( Time . now - start ) * 1000 ) . round
@@ -259,13 +258,19 @@ def run(item)
259258 # responsible for parsing the list and then returning the file paths at the
260259 # end.
261260 class Options
262- attr_reader :print_width , :scripts
261+ attr_reader :plugins , : print_width, :scripts , :target_ruby_version
263262
264263 def initialize ( print_width : DEFAULT_PRINT_WIDTH )
264+ @plugins = [ ]
265265 @print_width = print_width
266266 @scripts = [ ]
267+ @target_ruby_version = nil
267268 end
268269
270+ # TODO: This function causes a couple of side-effects that I really don't
271+ # like to have here. It mutates the global state by requiring the plugins,
272+ # and mutates the global options hash by adding the target ruby version.
273+ # That should be done on a config-by-config basis, not here.
269274 def parse ( arguments )
270275 parser . parse! ( arguments )
271276 end
@@ -285,7 +290,8 @@ def parser
285290 # require "syntax_tree/haml"
286291 #
287292 opts . on ( "--plugins=PLUGINS" ) do |plugins |
288- plugins . split ( "," ) . each { |plugin | require "syntax_tree/#{ plugin } " }
293+ @plugins = plugins . split ( "," )
294+ @plugins . each { |plugin | require "syntax_tree/#{ plugin } " }
289295 end
290296
291297 # If there is a print width specified on the command line, then
@@ -296,8 +302,13 @@ def parser
296302
297303 # If there is a script specified on the command line, then parse
298304 # it and add it to the list of scripts to run.
299- opts . on ( "-e SCRIPT" ) do |script |
300- @scripts << script
305+ opts . on ( "-e SCRIPT" ) { |script | @scripts << script }
306+
307+ # If there is a target ruby version specified on the command line,
308+ # parse that out and use it when formatting.
309+ opts . on ( "--target-ruby-version=VERSION" ) do |version |
310+ @target_ruby_version = Gem ::Version . new ( version )
311+ Formatter ::OPTIONS [ :target_ruby_version ] = @target_ruby_version
301312 end
302313 end
303314 end
@@ -395,9 +406,7 @@ def run(argv)
395406 queue << FileItem . new ( filepath ) if File . file? ( filepath )
396407 end
397408 end
398- options . scripts . each do |script |
399- queue << ScriptItem . new ( script )
400- end
409+ options . scripts . each { |script | queue << ScriptItem . new ( script ) }
401410 else
402411 queue << ScriptItem . new ( $stdin. read )
403412 end
0 commit comments