diff --git a/bin/sqlint b/bin/sqlint index 68bb42918374058fb58478c54d526819044dc180..350a1d0d9f54602afde37c8f3d312b0c9b6d1d27 100755 --- a/bin/sqlint +++ b/bin/sqlint @@ -7,7 +7,7 @@ require 'optparse' options = { limit: 1000 } optparse = OptionParser.new do |opts| - opts.banner = "Usage: #{File.basename($0)} [options] file.sql ..." + opts.banner = "Usage: #{File.basename($0)} [options] [file.sql ...]" opts.separator "" opts.separator "Options:" opts.on("--limit=N", Integer, "Limit checking to N errors (default: #{options[:limit]})") do |n| @@ -23,10 +23,6 @@ optparse = OptionParser.new do |opts| end end optparse.parse!(ARGV) -if ARGV.empty? - puts optparse - exit 1 -end ERROR_TYPES = {error: "ERROR", warning: "WARNING"} @@ -51,15 +47,25 @@ def display_lint(lint) end end -saw_errors = false -ARGV.each do |filename| - File.open(filename, 'r') do |file| - results = SQLint::Linter.new(filename, file).run.first(options[:limit]) - results.each do |lint| - display_lint(lint) +def each_input_file(&block) + if ARGV.empty? + yield [STDIN, "stdin"] + else + ARGV.each do |filename| + File.open(filename, 'r') do |file| + yield [file, filename] + end end - saw_errors ||= results.any? { |lint| lint.type == :error } end end +saw_errors = false +each_input_file do |file, filename| + results = SQLint::Linter.new(filename, file).run.first(options[:limit]) + results.each do |lint| + display_lint(lint) + end + saw_errors ||= results.any? { |lint| lint.type == :error } +end + exit 1 if saw_errors