1
1
# frozen_string_literal: true
2
2
3
+ require "optparse"
4
+
3
5
module SyntaxTree
4
6
# Syntax Tree ships with the `stree` CLI, which can be used to inspect and
5
7
# manipulate Ruby code. This module is responsible for powering that CLI.
@@ -70,6 +72,12 @@ def source
70
72
71
73
# The parent action class for the CLI that implements the basics.
72
74
class Action
75
+ attr_reader :options
76
+
77
+ def initialize ( options )
78
+ @options = options
79
+ end
80
+
73
81
def run ( item )
74
82
end
75
83
@@ -93,15 +101,9 @@ class Check < Action
93
101
class UnformattedError < StandardError
94
102
end
95
103
96
- attr_reader :print_width
97
-
98
- def initialize ( print_width :)
99
- @print_width = print_width
100
- end
101
-
102
104
def run ( item )
103
105
source = item . source
104
- if source != item . handler . format ( source , print_width )
106
+ if source != item . handler . format ( source , options . print_width )
105
107
raise UnformattedError
106
108
end
107
109
rescue StandardError
@@ -124,19 +126,13 @@ class Debug < Action
124
126
class NonIdempotentFormatError < StandardError
125
127
end
126
128
127
- attr_reader :print_width
128
-
129
- def initialize ( print_width :)
130
- @print_width = print_width
131
- end
132
-
133
129
def run ( item )
134
130
handler = item . handler
135
131
136
132
warning = "[#{ Color . yellow ( "warn" ) } ] #{ item . filepath } "
137
- formatted = handler . format ( item . source , print_width )
133
+ formatted = handler . format ( item . source , options . print_width )
138
134
139
- if formatted != handler . format ( formatted , print_width )
135
+ if formatted != handler . format ( formatted , options . print_width )
140
136
raise NonIdempotentFormatError
141
137
end
142
138
rescue StandardError
@@ -166,14 +162,8 @@ def run(item)
166
162
167
163
# An action of the CLI that formats the input source and prints it out.
168
164
class Format < Action
169
- attr_reader :print_width
170
-
171
- def initialize ( print_width :)
172
- @print_width = print_width
173
- end
174
-
175
165
def run ( item )
176
- puts item . handler . format ( item . source , print_width )
166
+ puts item . handler . format ( item . source , options . print_width )
177
167
end
178
168
end
179
169
@@ -197,18 +187,12 @@ def run(item)
197
187
# An action of the CLI that formats the input source and writes the
198
188
# formatted output back to the file.
199
189
class Write < Action
200
- attr_reader :print_width
201
-
202
- def initialize ( print_width :)
203
- @print_width = print_width
204
- end
205
-
206
190
def run ( item )
207
191
filepath = item . filepath
208
192
start = Time . now
209
193
210
194
source = item . source
211
- formatted = item . handler . format ( source , print_width )
195
+ formatted = item . handler . format ( source , options . print_width )
212
196
File . write ( filepath , formatted ) if filepath != :stdin
213
197
214
198
color = source == formatted ? Color . gray ( filepath ) : filepath
@@ -264,74 +248,89 @@ def run(item)
264
248
The maximum line width to use when formatting.
265
249
HELP
266
250
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
+
267
292
class << self
268
293
# Run the CLI over the given array of strings that make up the arguments
269
294
# passed to the invocation.
270
295
def run ( argv )
271
296
name , *arguments = argv
272
- print_width = DEFAULT_PRINT_WIDTH
273
297
274
298
config_file = File . join ( Dir . pwd , CONFIG_FILE )
275
299
if File . readable? ( config_file )
276
300
arguments . unshift ( *File . readlines ( config_file , chomp : true ) )
277
301
end
278
302
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 )
316
305
317
306
action =
318
307
case name
319
308
when "a" , "ast"
320
- AST . new
309
+ AST . new ( options )
321
310
when "c" , "check"
322
- Check . new ( print_width : print_width )
311
+ Check . new ( options )
323
312
when "debug"
324
- Debug . new ( print_width : print_width )
313
+ Debug . new ( options )
325
314
when "doc"
326
- Doc . new
315
+ Doc . new ( options )
316
+ when "help"
317
+ puts HELP
318
+ return 0
327
319
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
329
325
when "m" , "match"
330
- Match . new
326
+ Match . new ( options )
331
327
when "f" , "format"
332
- Format . new ( print_width : print_width )
328
+ Format . new ( options )
329
+ when "version"
330
+ puts SyntaxTree ::VERSION
331
+ return 0
333
332
when "w" , "write"
334
- Write . new ( print_width : print_width )
333
+ Write . new ( options )
335
334
else
336
335
warn ( HELP )
337
336
return 1
0 commit comments