Class: Commands

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommands

Returns a new instance of Commands.



6
7
8
9
10
11
12
# File 'lib/ezgit/commands.rb', line 6

def initialize
  @all = []
  @symbols = []
  @names = []
  @help_list = ''
  @options = {}
end

Instance Attribute Details

#allObject

Returns the value of attribute all.



3
4
5
# File 'lib/ezgit/commands.rb', line 3

def all
  @all
end

#help_listObject

Returns the value of attribute help_list.



3
4
5
# File 'lib/ezgit/commands.rb', line 3

def help_list
  @help_list
end

#namesObject

Returns the value of attribute names.



3
4
5
# File 'lib/ezgit/commands.rb', line 3

def names
  @names
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/ezgit/commands.rb', line 3

def options
  @options
end

#symbolsObject

Returns the value of attribute symbols.



3
4
5
# File 'lib/ezgit/commands.rb', line 3

def symbols
  @symbols
end

Instance Method Details

#processObject



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

def process
  @cmd = ARGV.shift
  matched = false
  @all.each do |current|
    if current[:name].to_s.eql? @cmd
      matched = true
      @cmd_opts = Trollop::options do
        usage = current[:usage]
        usage ||= "ezgit #{current[:name].to_s} [<options>]"
        banner <<-HELP_DESCRIPTION
command: ezgit #{current[:name].to_s}\n
#{current[:help].to_s}

Usage:
      #{usage}

 options are:
HELP_DESCRIPTION

        current[:options].each do |cmd_opt|
          sym = cmd_opt[0]
          info = cmd_opt[1]
          flags = cmd_opt[2]
          opt sym, info, flags
        end
      end
      current[:action].call(@cmd_opts, ARGV)
      break
    end
  end
  Trollop::die "unknown subcommand #{@cmd.inspect}" if not matched
  if $commands.options[:debug]
    puts "Global options: #{$commands.options.inspect}"
    puts "Subcommand: #{@cmd.inspect}"
    puts "Subcommand options: #{@cmd_opts.inspect}"
    puts "Remaining arguments: #{ARGV.inspect}"
  end
end

#readObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ezgit/commands.rb', line 15

def read
  commands_dir = File.expand_path "commands", File.dirname(__FILE__)
  files = Dir["#{commands_dir}/*.rb"]
  files.each do |command_file|
    require command_file
    current_symbol = @all.last[:name]
    @symbols << current_symbol
    @names << current_symbol.to_s
    @help_list << "\t#{@names.last.cyan.bold}\t#{@all.last[:help].to_s.bold}\n"
  end
end