Class: Roll::Command

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

Overview

Command-line Interface

– TODO: clean command to remove dead directories from environment ++

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*argv) ⇒ Command

New Command.



37
38
39
40
41
42
43
# File 'lib/roll/command.rb', line 37

def initialize(*argv)
  # only need optparse when command is run
  require 'optparse'
  @op   = OptionParser.new
  @args = argv
  @opts = {}
end

Instance Attribute Details

#argsObject (readonly)

Command-line arguments.



13
14
15
# File 'lib/roll/command.rb', line 13

def args
  @args
end

#opObject (readonly)

Instance of OptionParser.



19
20
21
# File 'lib/roll/command.rb', line 19

def op
  @op
end

#optsObject (readonly)

Command-line options.



16
17
18
# File 'lib/roll/command.rb', line 16

def opts
  @opts
end

Class Method Details

.main(*argv) ⇒ Object

Initialize and execute command.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/roll/command.rb', line 22

def self.main(*argv)
  #cmd   = argv.shift
  idx = argv.index{ |e| e !~ /^\-/ }
  cmd = idx ? argv.delete_at(idx) : 'help'
  begin
    require "roll/commands/#{cmd}"
  rescue LoadError
    cmd = 'help'
    require "roll/commands/#{cmd}"
  end
  klass = ::Roll.const_get("Command#{cmd.capitalize}")
  klass.new(*argv).execute
end

Instance Method Details

#executeObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/roll/command.rb', line 46

def execute
  setup

  op.on_tail("--warn", "-w", "Show warnings.") do
    $VERBOSE = true
  end

  op.on_tail("--debug", "Run in debugging mode.") do
    $DEBUG   = true
    $VERBOSE = true
  end

  op.on_tail("--help", "-h", "Display this help message.") do
    puts op
    exit
  end

  op.parse!(args)

  call
end