Class: Travis::CLI::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Parser
Defined in:
lib/travis/cli/command.rb

Direct Known Subclasses

ApiCommand, Help, Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parser

new, on, on_initialize

Constructor Details

#initialize(options = {}) ⇒ Command

Returns a new instance of Command.



53
54
55
56
57
58
59
60
61
# File 'lib/travis/cli/command.rb', line 53

def initialize(options = {})
  @output   = SimpleDelegator.new($stdout)
  @input    = SimpleDelegator.new($stdin)
  @terminal = HighLine.new(@input, @output)
  options.each do |key, value|
    public_send("#{key}=", value) if respond_to? "#{key}="
  end
  @arguments ||= []
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



51
52
53
# File 'lib/travis/cli/command.rb', line 51

def arguments
  @arguments
end

#configObject

Returns the value of attribute config.



51
52
53
# File 'lib/travis/cli/command.rb', line 51

def config
  @config
end

#force_interactiveObject

Returns the value of attribute force_interactive.



51
52
53
# File 'lib/travis/cli/command.rb', line 51

def force_interactive
  @force_interactive
end

#terminalObject

Returns the value of attribute terminal.



51
52
53
# File 'lib/travis/cli/command.rb', line 51

def terminal
  @terminal
end

Class Method Details

.abstractObject



43
44
45
# File 'lib/travis/cli/command.rb', line 43

def self.abstract
  @@abstract << self
end

.abstract?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/travis/cli/command.rb', line 39

def self.abstract?
  @@abstract.include? self
end

.command_nameObject



34
35
36
# File 'lib/travis/cli/command.rb', line 34

def self.command_name
  name[/[^:]*$/].downcase
end

.skip(name) ⇒ Object



47
48
49
# File 'lib/travis/cli/command.rb', line 47

def self.skip(name)
  define_method(name) {}
end

Instance Method Details

#command_nameObject



107
108
109
# File 'lib/travis/cli/command.rb', line 107

def command_name
  self.class.command_name
end

#executeObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/travis/cli/command.rb', line 96

def execute
  check_arity(method(:run), *arguments)
  load_config
  setup
  run(*arguments)
  store_config
rescue StandardError => e
  raise(e) if explode?
  error e.message
end

#helpObject



126
127
128
129
# File 'lib/travis/cli/command.rb', line 126

def help
  parser.banner = usage
  parser.to_s
end

#inputObject



63
64
65
# File 'lib/travis/cli/command.rb', line 63

def input
  @input.__getobj__
end

#input=(io) ⇒ Object



67
68
69
# File 'lib/travis/cli/command.rb', line 67

def input=(io)
  @input.__setobj__(io)
end

#outputObject



71
72
73
# File 'lib/travis/cli/command.rb', line 71

def output
  @output.__getobj__
end

#output=(io) ⇒ Object



75
76
77
# File 'lib/travis/cli/command.rb', line 75

def output=(io)
  @output.__setobj__(io)
end

#parse(args) ⇒ Object



86
87
88
89
90
91
# File 'lib/travis/cli/command.rb', line 86

def parse(args)
  rest = parser.parse(args)
  arguments.concat(rest)
rescue OptionParser::ParseError => e
  error e.message
end

#say(data, format = nil) ⇒ Object



131
132
133
134
# File 'lib/travis/cli/command.rb', line 131

def say(data, format = nil)
  data = format % color(data, :important) if format and interactive?
  terminal.say data.gsub(/<\[\[/, '<%=').gsub(/\]\]>/, '%>')
end

#setupObject



93
94
# File 'lib/travis/cli/command.rb', line 93

def setup
end

#usageObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/travis/cli/command.rb', line 111

def usage
  usage  = "#$0 #{command_name} [options]"
  method = method(:run)
  if method.respond_to? :parameters
    method.parameters.each do |type, name|
      name = "[#{name}]"      if type == :opt
      name = "[#{name}..]" if type == :rest
      usage << " #{name}"
    end
  else
    usage << " ..."
  end
  "Usage: " << color(usage, :command)
end

#write_to(io) ⇒ Object



79
80
81
82
83
84
# File 'lib/travis/cli/command.rb', line 79

def write_to(io)
  io_was, self.output = output, io
  yield
ensure
  self.output = io_was if io_was
end