Class: DontRepeatYourself::CLI

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

Constant Summary collapse

FORMATS =
DontRepeatYourself::REPORT_TYPES.map{|format| format.downcase}
REPORTERS =
DontRepeatYourself::REPORTER_TYPES.map{|reporter| reporter.downcase}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



24
25
26
# File 'lib/dont_repeat_yourself/cli.rb', line 24

def initialize
  
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/dont_repeat_yourself/cli.rb', line 20

def options
  @options
end

Class Method Details

.executeObject



9
10
11
# File 'lib/dont_repeat_yourself/cli.rb', line 9

def execute
  parse(ARGV).execute!
end

.parse(args) ⇒ Object



13
14
15
16
17
# File 'lib/dont_repeat_yourself/cli.rb', line 13

def parse(args)
  cli = new
  cli.parse_options!(args)
  cli
end

Instance Method Details

#execute!Object



71
72
73
74
75
76
# File 'lib/dont_repeat_yourself/cli.rb', line 71

def execute!
  ruby_project_reporter = DontRepeatYourself::ProjectReporterBase.build_reporter(@options[:reporter], @options[:basedir])
  ruby_project_reporter.with_threshold_of_duplicate_lines(@options[:threshold]) if @options.has_key?(:threshold)
  ruby_project_reporter.send("with_#{@options[:format]}_reporting") if @options.has_key?(:format)      
  STDOUT.print(ruby_project_reporter.report)
end

#parse_options!(args) ⇒ Object



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
# File 'lib/dont_repeat_yourself/cli.rb', line 28

def parse_options!(args)
  args.extend(OptionParser::Arguable)

  @options = { :basedir => './', :format => 'default', :reporter => 'ruby' }
  args.options do |opts|
    opts.banner = "Usage: dry-report [options] "
    
    opts.on("-d BASEDIR", "--basedir BASEDIR", "set up the base directory of your ruby project, current directory by default") do |b|
      @options[:basedir] = b
    end
    
    opts.on("-f FORMAT", "--format FORMAT", "report format (default is plain text)",
      "Available formats: #{FORMATS.join(", ")}") do |v|
      unless FORMATS.index(v) 
        STDERR.puts "Invalid format: #{v}\n"
        STDERR.puts opts.help
        exit 1
      end
      @options[:format] = v
    end
            
    opts.on("-t THRESHOLD", "--threshold THRESHOLD", "threshold of duplicate lines") do |t|
      @options[:threshold] = t.to_i
    end
   
    opts.on("-r REPORTER", "--reporter REPORTER", "reporter to use (default is ruby): ruby, rails, or plugin",
      "Available reporters: #{REPORTERS.join(", ")}") do |v|
      unless REPORTERS.index(v) 
        STDERR.puts "Invalid reporter: #{v}\n"
        STDERR.puts opts.help
        exit 1
      end
      @options[:reporter] = v
    end

    opts.on_tail("--help", "You're looking at it. Any question? http://21croissants.blogspot.com/2008/10/dry.html") do
      puts opts.help
      exit
    end
  end.parse!
        
end