Class: XRay::CMDOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/cmd_runner.rb

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cmd_runner.rb', line 15

def self.parse( args )
  files = []
  options = {
    :encoding   => 'gb2312',
    :colorful   => true,
    :type       => nil,
    :check_min  => false,
    :format     => :console
  }

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: fdlint"
    %w(css js html).each do |type|
      opts.on("--#{type}", "check #{type} files only") do
        options[:type] = type.intern
      end
    end
    opts.on("--charset set", "-c", "file charset") do |enc|
      options[:encoding] = enc
    end
    opts.on("--debug", "-d", "print debug info") do
      options[:debug] = true
    end
    opts.on("--list", "-l", "list results without source, the same as '--format=nocolor'") do
      options[:format] = :nocolor
    end
    opts.on("--checkmin", "-m", "check minified files too. (e.g. *-min.js; *-min.css)") do
      options[:check_min] = true
    end
    opts.on("--format [type]", [:console, :nocolor, :vim], "output format. Can be 'vim', 'console' or 'nocolor'. Default is 'console'") do |f|
      options[:format] = f.intern
    end
    opts.on("--level [log_level]", [:warn, :error, :fatal], "determine the log level. Can be 'warn', 'error' or 'fatal'") do |level|
      options[:log_level] = level
    end
  end

  begin
    rest = opts.parse! args
    files.concat rest

    if files.empty?
      unless $stdin.tty?
        str = ARGF.read
        options[:text] = str
      else
        raise ArgumentError.new("") 
      end
    end

  rescue => e
    puts e.message.capitalize + "\n\n"
    puts opts
    exit 1
  end
  [options, files]

end