11# frozen_string_literal: true
22
3+ require "optparse"
4+
35module SyntaxTree
46 # Syntax Tree ships with the `stree` CLI, which can be used to inspect and
57 # manipulate Ruby code. This module is responsible for powering that CLI.
@@ -70,6 +72,12 @@ def source
7072
7173 # The parent action class for the CLI that implements the basics.
7274 class Action
75+ attr_reader :options
76+
77+ def initialize ( options )
78+ @options = options
79+ end
80+
7381 def run ( item )
7482 end
7583
@@ -93,15 +101,9 @@ class Check < Action
93101 class UnformattedError < StandardError
94102 end
95103
96- attr_reader :print_width
97-
98- def initialize ( print_width :)
99- @print_width = print_width
100- end
101-
102104 def run ( item )
103105 source = item . source
104- if source != item . handler . format ( source , print_width )
106+ if source != item . handler . format ( source , options . print_width )
105107 raise UnformattedError
106108 end
107109 rescue StandardError
@@ -124,19 +126,13 @@ class Debug < Action
124126 class NonIdempotentFormatError < StandardError
125127 end
126128
127- attr_reader :print_width
128-
129- def initialize ( print_width :)
130- @print_width = print_width
131- end
132-
133129 def run ( item )
134130 handler = item . handler
135131
136132 warning = "[#{ Color . yellow ( "warn" ) } ] #{ item . filepath } "
137- formatted = handler . format ( item . source , print_width )
133+ formatted = handler . format ( item . source , options . print_width )
138134
139- if formatted != handler . format ( formatted , print_width )
135+ if formatted != handler . format ( formatted , options . print_width )
140136 raise NonIdempotentFormatError
141137 end
142138 rescue StandardError
@@ -166,14 +162,8 @@ def run(item)
166162
167163 # An action of the CLI that formats the input source and prints it out.
168164 class Format < Action
169- attr_reader :print_width
170-
171- def initialize ( print_width :)
172- @print_width = print_width
173- end
174-
175165 def run ( item )
176- puts item . handler . format ( item . source , print_width )
166+ puts item . handler . format ( item . source , options . print_width )
177167 end
178168 end
179169
@@ -197,18 +187,12 @@ def run(item)
197187 # An action of the CLI that formats the input source and writes the
198188 # formatted output back to the file.
199189 class Write < Action
200- attr_reader :print_width
201-
202- def initialize ( print_width :)
203- @print_width = print_width
204- end
205-
206190 def run ( item )
207191 filepath = item . filepath
208192 start = Time . now
209193
210194 source = item . source
211- formatted = item . handler . format ( source , print_width )
195+ formatted = item . handler . format ( source , options . print_width )
212196 File . write ( filepath , formatted ) if filepath != :stdin
213197
214198 color = source == formatted ? Color . gray ( filepath ) : filepath
@@ -264,74 +248,89 @@ def run(item)
264248 The maximum line width to use when formatting.
265249 HELP
266250
251+ # This represents all of the options that can be passed to the CLI. It is
252+ # responsible for parsing the list and then returning the file paths at the
253+ # end.
254+ class Options
255+ attr_reader :print_width
256+
257+ def initialize ( print_width : DEFAULT_PRINT_WIDTH )
258+ @print_width = print_width
259+ end
260+
261+ def parse ( arguments )
262+ parser . parse ( arguments )
263+ end
264+
265+ private
266+
267+ def parser
268+ OptionParser . new do |opts |
269+ # If there are any plugins specified on the command line, then load
270+ # them by requiring them here. We do this by transforming something
271+ # like
272+ #
273+ # stree format --plugins=haml template.haml
274+ #
275+ # into
276+ #
277+ # require "syntax_tree/haml"
278+ #
279+ opts . on ( "--plugins=PLUGINS" ) do |plugins |
280+ plugins . split ( "," ) . each { |plugin | require "syntax_tree/#{ plugin } " }
281+ end
282+
283+ # If there is a print width specified on the command line, then
284+ # parse that out here and use it when formatting.
285+ opts . on ( "--print-width=NUMBER" , Integer ) do |print_width |
286+ @print_width = print_width
287+ end
288+ end
289+ end
290+ end
291+
267292 class << self
268293 # Run the CLI over the given array of strings that make up the arguments
269294 # passed to the invocation.
270295 def run ( argv )
271296 name , *arguments = argv
272- print_width = DEFAULT_PRINT_WIDTH
273297
274298 config_file = File . join ( Dir . pwd , CONFIG_FILE )
275299 if File . readable? ( config_file )
276300 arguments . unshift ( *File . readlines ( config_file , chomp : true ) )
277301 end
278302
279- while arguments . first &.start_with? ( "--" )
280- case ( argument = arguments . shift )
281- when /^--plugins=(.+)$/
282- # If there are any plugins specified on the command line, then load
283- # them by requiring them here. We do this by transforming something
284- # like
285- #
286- # stree format --plugins=haml template.haml
287- #
288- # into
289- #
290- # require "syntax_tree/haml"
291- #
292- $1. split ( "," ) . each { |plugin | require "syntax_tree/#{ plugin } " }
293- when /^--print-width=(\d +)$/
294- # If there is a print width specified on the command line, then
295- # parse that out here and use it when formatting.
296- print_width = Integer ( $1)
297- else
298- warn ( "Unknown CLI option: #{ argument } " )
299- warn ( HELP )
300- return 1
301- end
302- end
303-
304- case name
305- when "help"
306- puts HELP
307- return 0
308- when "lsp"
309- require "syntax_tree/language_server"
310- LanguageServer . new ( print_width : print_width ) . run
311- return 0
312- when "version"
313- puts SyntaxTree ::VERSION
314- return 0
315- end
303+ options = Options . new
304+ options . parse ( arguments )
316305
317306 action =
318307 case name
319308 when "a" , "ast"
320- AST . new
309+ AST . new ( options )
321310 when "c" , "check"
322- Check . new ( print_width : print_width )
311+ Check . new ( options )
323312 when "debug"
324- Debug . new ( print_width : print_width )
313+ Debug . new ( options )
325314 when "doc"
326- Doc . new
315+ Doc . new ( options )
316+ when "help"
317+ puts HELP
318+ return 0
327319 when "j" , "json"
328- Json . new
320+ Json . new ( options )
321+ when "lsp"
322+ require "syntax_tree/language_server"
323+ LanguageServer . new ( print_width : options . print_width ) . run
324+ return 0
329325 when "m" , "match"
330- Match . new
326+ Match . new ( options )
331327 when "f" , "format"
332- Format . new ( print_width : print_width )
328+ Format . new ( options )
329+ when "version"
330+ puts SyntaxTree ::VERSION
331+ return 0
333332 when "w" , "write"
334- Write . new ( print_width : print_width )
333+ Write . new ( options )
335334 else
336335 warn ( HELP )
337336 return 1
0 commit comments