@@ -150,19 +150,19 @@ def run(handler, filepath, source)
150
150
# The help message displayed if the input arguments are not correctly
151
151
# ordered or formatted.
152
152
HELP = <<~HELP
153
- #{ Color . bold ( "stree ast [FILE]" ) }
153
+ #{ Color . bold ( "stree ast [OPTIONS] [ FILE]" ) }
154
154
Print out the AST corresponding to the given files
155
155
156
- #{ Color . bold ( "stree check [FILE]" ) }
156
+ #{ Color . bold ( "stree check [OPTIONS] [ FILE]" ) }
157
157
Check that the given files are formatted as syntax tree would format them
158
158
159
- #{ Color . bold ( "stree debug [FILE]" ) }
159
+ #{ Color . bold ( "stree debug [OPTIONS] [ FILE]" ) }
160
160
Check that the given files can be formatted idempotently
161
161
162
- #{ Color . bold ( "stree doc [FILE]" ) }
162
+ #{ Color . bold ( "stree doc [OPTIONS] [ FILE]" ) }
163
163
Print out the doc tree that would be used to format the given files
164
164
165
- #{ Color . bold ( "stree format [FILE]" ) }
165
+ #{ Color . bold ( "stree format [OPTIONS] [ FILE]" ) }
166
166
Print out the formatted version of the given files
167
167
168
168
#{ Color . bold ( "stree help" ) }
@@ -174,17 +174,22 @@ def run(handler, filepath, source)
174
174
#{ Color . bold ( "stree version" ) }
175
175
Output the current version of syntax tree
176
176
177
- #{ Color . bold ( "stree write [FILE]" ) }
177
+ #{ Color . bold ( "stree write [OPTIONS] [ FILE]" ) }
178
178
Read, format, and write back the source of the given files
179
+
180
+ [OPTIONS]
181
+
182
+ --plugins=...
183
+ A comma-separated list of plugins to load.
179
184
HELP
180
185
181
186
class << self
182
187
# Run the CLI over the given array of strings that make up the arguments
183
188
# passed to the invocation.
184
189
def run ( argv )
185
- arg , *patterns = argv
190
+ name , *arguments = argv
186
191
187
- case arg
192
+ case name
188
193
when "help"
189
194
puts HELP
190
195
return 0
@@ -197,13 +202,13 @@ def run(argv)
197
202
return 0
198
203
end
199
204
200
- if patterns . empty?
205
+ if arguments . empty?
201
206
warn ( HELP )
202
207
return 1
203
208
end
204
209
205
210
action =
206
- case arg
211
+ case name
207
212
when "a" , "ast"
208
213
AST . new
209
214
when "c" , "check"
@@ -221,17 +226,31 @@ def run(argv)
221
226
return 1
222
227
end
223
228
229
+ # If there are any plugins specified on the command line, then load them
230
+ # by requiring them here. We do this by transforming something like
231
+ #
232
+ # stree format --plugins=haml template.haml
233
+ #
234
+ # into
235
+ #
236
+ # require "syntax_tree/haml"
237
+ #
238
+ if arguments . first . start_with? ( "--plugins=" )
239
+ plugins = arguments . shift [ /^--plugins=(.*)$/ , 1 ]
240
+ plugins . split ( "," ) . each { |plugin | require "syntax_tree/#{ plugin } " }
241
+ end
242
+
224
243
errored = false
225
- patterns . each do |pattern |
244
+ arguments . each do |pattern |
226
245
Dir . glob ( pattern ) . each do |filepath |
227
246
next unless File . file? ( filepath )
228
247
229
248
handler = HANDLERS [ File . extname ( filepath ) ]
230
- source = SyntaxTree . read ( filepath )
249
+ source = handler . read ( filepath )
231
250
232
251
begin
233
252
action . run ( handler , filepath , source )
234
- rescue ParseError => error
253
+ rescue Parser :: ParseError => error
235
254
warn ( "Error: #{ error . message } " )
236
255
237
256
if error . lineno
0 commit comments