Class: Codetip::Commander

Inherits:
App
  • Object
show all
Defined in:
lib/codetip/app/commander.rb

Instance Method Summary collapse

Methods inherited from App

#parse

Instance Method Details

#run(argv = ARGV) ⇒ Object



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
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/codetip/app/commander.rb', line 17

def run(argv = ARGV)
  begin
    opts = {}
    if argv.length > 0
      loop {
        case argv[0]
          # -h or --help
          when /^-(h|-help)$/i then
            argv.shift
            
            puts "Usage: codetip [options] <location>"
            puts
            puts "Options:"
            puts "\t-h, --help\tDisplays what you are looking at"
            puts "\t-r, --restrict\tRestrict file search. e.g, '.js' or '*.{js,cpp}'"
            puts "\t-u, --username\tOnly display comments mentioning username"
            puts
            puts "Example:"
            puts "\tcodetip --username='kurt_wagner' -r '*.{js,pl}' ~/path/to/project"
            puts
            
            return
            
          # -r or --restrict
          when /^-(r|-opts[:restrict])(=(.+))?$/i then
            if !$3.nil?
              opts[:restrict] = $3
              argv.shift
            else
              argv.shift
              opts[:restrict] = argv.shift
            end
          # -u or --username
          when /^-(u|-username)(=(.+))?$/i then
            if !$3.nil?
              opts[:username] = $3
              argv.shift
            else
              argv.shift
              opts[:username] = argv.shift
            end
          # other unknown options
          when /^-/ then
            puts "Unknown option: #{argv.shift}"
          else
            # remove any trailing forward slashes from directory
            directory = argv.shift
            opts[:path] = directory.gsub(/\/+$/, '') if !directory.nil?
            break
        end
      }
    end
    
    parse(opts[:path] || '.', opts[:restrict] || "*", opts[:username]).each do |filename, types|
      puts filename.red.bold;
      types.each do |type, comments|
        if comments.count > 0
          puts "[ #{type.upcase} ]".blue.bold
          comments.each do |comment|
            puts "  Line #{comment[:line]} - ".gray + comment[:message].highlightMessage
          end
          puts
        end
      end
    end
  rescue Exception => e  
    puts e.message
  end
end