From 5d5761b510209c0d2fb514373b7377aaf4f2acc7 Mon Sep 17 00:00:00 2001 From: Kieran Trezona-le Comte <trezona-lecomte@gmail.com> Date: Fri, 17 Jul 2015 15:31:39 +1200 Subject: [PATCH] Use optparse --- bin/sqlint | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/bin/sqlint b/bin/sqlint index db7069f..0a84a3d 100755 --- a/bin/sqlint +++ b/bin/sqlint @@ -3,17 +3,31 @@ $LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/../lib') require 'pg_query' require 'sqlint' +require 'optparse' LIMIT = 1000 -if ARGV.include?("--version") - puts SQLint::VERSION - exit 0 +options = { limit: 1000 } +optparse = OptionParser.new do |opts| + opts.banner = "Usage: #{File.basename($0)} [options] file.sql ..." + opts.separator "" + opts.separator "Options:" + opts.on("--limit=N", Integer, "Limit checking to N errors") do |n| + options[:limit] = n + end + opts.on_tail("-h", "--help", "Print this help") do + puts opts + exit 0 + end + opts.on_tail("-v", "--version", "Display the version") do + puts SQLint::VERSION + exit 0 + end end - -if ARGV.empty? || ARGV.include?("--help") || ARGV.include?("-h") - puts "Usage: sqlint file.sql ..." - exit 0 +optparse.parse!(ARGV) +if ARGV.empty? + puts optparse + exit 1 end ERROR_TYPES = {error: "ERROR", warning: "WARNING"} @@ -29,7 +43,7 @@ end saw_errors = false ARGV.each do |filename| File.open(filename, 'r') do |file| - results = SQLint::Linter.new(filename, file).run.first(LIMIT) + results = SQLint::Linter.new(filename, file).run.first(options[:limit]) results.each do |lint| message_lines = lint.message.split("\n") puts [ -- GitLab