@@ -63,12 +63,13 @@ def writable?
6363 class ScriptItem
6464 attr_reader :source
6565
66- def initialize ( source )
66+ def initialize ( source , handler_extension )
6767 @source = source
68+ @handler_extension = handler_extension
6869 end
6970
7071 def handler
71- HANDLERS [ ".rb" ]
72+ HANDLERS [ @handler_extension ]
7273 end
7374
7475 def filepath
@@ -82,8 +83,12 @@ def writable?
8283
8384 # An item of work that correspond to the content passed in via stdin.
8485 class STDINItem
86+ def initialize ( handler_extension )
87+ @handler_extension = handler_extension
88+ end
89+
8590 def handler
86- HANDLERS [ ".rb" ]
91+ HANDLERS [ @handler_extension ]
8792 end
8893
8994 def filepath
@@ -457,7 +462,10 @@ def run(item)
457462 The maximum line width to use when formatting.
458463
459464 -e SCRIPT
460- Parse an inline Ruby string.
465+ Parse an inline string.
466+
467+ --handler=EXTENSION
468+ A file extension matching the content passed in via STDIN or -e. Defaults to 'rb'
461469 HELP
462470
463471 # This represents all of the options that can be passed to the CLI. It is
@@ -468,13 +476,15 @@ class Options
468476 :plugins ,
469477 :print_width ,
470478 :scripts ,
479+ :handler_extension ,
471480 :target_ruby_version
472481
473482 def initialize
474483 @ignore_files = [ ]
475484 @plugins = [ ]
476485 @print_width = DEFAULT_PRINT_WIDTH
477486 @scripts = [ ]
487+ @handler_extension = ".rb"
478488 @target_ruby_version = DEFAULT_RUBY_VERSION
479489 end
480490
@@ -523,6 +533,13 @@ def parser
523533 # it and add it to the list of scripts to run.
524534 opts . on ( "-e SCRIPT" ) { |script | @scripts << script }
525535
536+ # If there is a handler specified, then parse it and use it for
537+ # STDIN and scripts.
538+ opts . on ( "--handler=EXTENSION" ) do |extension |
539+ # Both ".rb" and "rb" are going to work
540+ @handler_extension = ".#{ extension . delete_prefix ( "." ) } "
541+ end
542+
526543 # If there is a target ruby version specified on the command line,
527544 # parse that out and use it when formatting.
528545 opts . on ( "--target-ruby-version=VERSION" ) do |version |
@@ -630,9 +647,11 @@ def run(argv)
630647 end
631648 end
632649
633- options . scripts . each { |script | queue << ScriptItem . new ( script ) }
650+ options . scripts . each do |script |
651+ queue << ScriptItem . new ( script , options . handler_extension )
652+ end
634653 else
635- queue << STDINItem . new
654+ queue << STDINItem . new ( options . handler_extension )
636655 end
637656
638657 # At the end, we're going to return whether or not this worker ever
0 commit comments