Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1be1965

Browse files
committed
Add inline script option
1 parent d728d25 commit 1be1965

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

lib/syntax_tree/cli.rb

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,25 +212,25 @@ def run(item)
212212
# The help message displayed if the input arguments are not correctly
213213
# ordered or formatted.
214214
HELP = <<~HELP
215-
#{Color.bold("stree ast [--plugins=...] [--print-width=NUMBER] FILE")}
215+
#{Color.bold("stree ast [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
216216
Print out the AST corresponding to the given files
217217
218-
#{Color.bold("stree check [--plugins=...] [--print-width=NUMBER] FILE")}
218+
#{Color.bold("stree check [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
219219
Check that the given files are formatted as syntax tree would format them
220220
221-
#{Color.bold("stree debug [--plugins=...] [--print-width=NUMBER] FILE")}
221+
#{Color.bold("stree debug [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
222222
Check that the given files can be formatted idempotently
223223
224-
#{Color.bold("stree doc [--plugins=...] FILE")}
224+
#{Color.bold("stree doc [--plugins=...] [-e SCRIPT] FILE")}
225225
Print out the doc tree that would be used to format the given files
226226
227-
#{Color.bold("stree format [--plugins=...] [--print-width=NUMBER] FILE")}
227+
#{Color.bold("stree format [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
228228
Print out the formatted version of the given files
229229
230-
#{Color.bold("stree json [--plugins=...] FILE")}
230+
#{Color.bold("stree json [--plugins=...] [-e SCRIPT] FILE")}
231231
Print out the JSON representation of the given files
232232
233-
#{Color.bold("stree match [--plugins=...] FILE")}
233+
#{Color.bold("stree match [--plugins=...] [-e SCRIPT] FILE")}
234234
Print out a pattern-matching Ruby expression that would match the given files
235235
236236
#{Color.bold("stree help")}
@@ -242,28 +242,32 @@ def run(item)
242242
#{Color.bold("stree version")}
243243
Output the current version of syntax tree
244244
245-
#{Color.bold("stree write [--plugins=...] [--print-width=NUMBER] FILE")}
245+
#{Color.bold("stree write [--plugins=...] [--print-width=NUMBER] [-e SCRIPT] FILE")}
246246
Read, format, and write back the source of the given files
247247
248248
--plugins=...
249249
A comma-separated list of plugins to load.
250250
251251
--print-width=NUMBER
252252
The maximum line width to use when formatting.
253+
254+
-e SCRIPT
255+
Parse an inline Ruby string.
253256
HELP
254257

255258
# This represents all of the options that can be passed to the CLI. It is
256259
# responsible for parsing the list and then returning the file paths at the
257260
# end.
258261
class Options
259-
attr_reader :print_width
262+
attr_reader :print_width, :scripts
260263

261264
def initialize(print_width: DEFAULT_PRINT_WIDTH)
262265
@print_width = print_width
266+
@scripts = []
263267
end
264268

265269
def parse(arguments)
266-
parser.parse(arguments)
270+
parser.parse!(arguments)
267271
end
268272

269273
private
@@ -289,6 +293,12 @@ def parser
289293
opts.on("--print-width=NUMBER", Integer) do |print_width|
290294
@print_width = print_width
291295
end
296+
297+
# If there is a script specified on the command line, then parse
298+
# it and add it to the list of scripts to run.
299+
opts.on("-e SCRIPT") do |script|
300+
@scripts << script
301+
end
292302
end
293303
end
294304
end
@@ -367,7 +377,7 @@ def run(argv)
367377

368378
# If we're not reading from stdin and the user didn't supply and
369379
# filepaths to be read, then we exit with the usage message.
370-
if $stdin.tty? && arguments.empty?
380+
if $stdin.tty? && arguments.empty? && options.scripts.empty?
371381
warn(HELP)
372382
return 1
373383
end
@@ -377,14 +387,17 @@ def run(argv)
377387

378388
# If we're reading from stdin, then we'll just add the stdin object to
379389
# the queue. Otherwise, we'll add each of the filepaths to the queue.
380-
if $stdin.tty? || arguments.any?
390+
if $stdin.tty? && (arguments.any? || options.scripts.any?)
381391
arguments.each do |pattern|
382392
Dir
383393
.glob(pattern)
384394
.each do |filepath|
385395
queue << FileItem.new(filepath) if File.file?(filepath)
386396
end
387397
end
398+
options.scripts.each do |script|
399+
queue << ScriptItem.new(script)
400+
end
388401
else
389402
queue << ScriptItem.new($stdin.read)
390403
end

test/cli_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ def test_no_arguments_no_tty
133133
$stdin = stdin
134134
end
135135

136+
def test_inline_script
137+
stdio, = capture_io { SyntaxTree::CLI.run(["format", "-e", "1+1"]) }
138+
assert_equal("1 + 1\n", stdio)
139+
end
140+
141+
def test_multiple_inline_scripts
142+
stdio, = capture_io { SyntaxTree::CLI.run(["format", "-e", "1+1", "-e", "2+2"]) }
143+
assert_equal("1 + 1\n2 + 2\n", stdio)
144+
end
145+
136146
def test_generic_error
137147
SyntaxTree.stub(:format, ->(*) { raise }) do
138148
result = run_cli("format")

0 commit comments

Comments
 (0)