class IMGLint::CLI
CLI
class is responsible for handling command-line arguments.
If the first argument is an “install” command, then we just copying default config to project's root folder and exit.
Otherwise we'd extract path/max image size/image format arguments and run linter.
Public Instance Methods
run(args = [])
click to toggle source
CLI
is used when running img-lint from command line (check bin/img-lint) file or from a Raketask (check lib/img_lint/rake_task file).
In Raketask CLI#run
is called without arguments (expected that config file is there) and in bin/img-lint ARGV is passed in.
# File lib/img_lint/cli.rb, line 25 def run(args = []) if args.first == "install" require "fileutils" target_file = File.join(Dir.pwd, ".img-lint.yml") config_file = File.join(File.expand_path(__dir__), "..", "config", "default.yml") FileUtils.cp(config_file, target_file) 0 else options = extract_options(args) config = IMGLint::Config.load config.merge!(options) path = config.delete("path") linter = IMGLint::Linter.new(config: config) heavy_images = linter.lint(path: path) heavy_images.empty? ? 0 : 2 end end
Private Instance Methods
extract_options(args)
click to toggle source
# File lib/img_lint/cli.rb, line 60 def extract_options(args) options = {} OptionParser.new do |opts| opts.banner = unindent(<<-TEXT) img-lint help 1. img-lint install This will copy default config to your local .img-lint.yml file 2. img-lint [options] TEXT opts.on("-v", "--version", "Prints current version of img-lint") do puts IMGLint::VERSION exit 0 end opts.on("-p", "--path PATH", "Path to a folder with images") do |v| options["path"] = v end opts.on("-m", "--max-size MAX_SIZE", "Max image size allowed, '150' by default (150Kb)") do |v| options["max_file_size"] = v.to_i end opts.on("-f", "--format IMAGE_FORMATS", "Image formats, 'jpg,png,gif' by default") do |v| options["image_formats"] = v end end.parse!(args) options end
unindent(str)
click to toggle source
This method is needed to unindent [“here document”](en.wikibooks.org/wiki/Ruby_Programming/Here_documents) help description.
# File lib/img_lint/cli.rb, line 56 def unindent(str) str.gsub(/^#{str.scan(/^[ \t]+(?=\S)/).min}/, "") end