Class: Duse::CLI::Command

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

Direct Known Subclasses

ApiCommand, Config, Help, MetaCommand, 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.



27
28
29
30
31
32
33
34
35
# File 'lib/duse/cli/command.rb', line 27

def initialize(options = {})
  self.output      = $stdout
  self.input       = $stdin
  self.arguments ||= []
  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.



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

def arguments
  @arguments
end

#force_interactiveObject

Returns the value of attribute force_interactive.



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

def force_interactive
  @force_interactive
end

#inputObject

Returns the value of attribute input.



8
9
10
# File 'lib/duse/cli/command.rb', line 8

def input
  @input
end

#outputObject

Returns the value of attribute output.



8
9
10
# File 'lib/duse/cli/command.rb', line 8

def output
  @output
end

Class Method Details

.abstractObject



69
70
71
# File 'lib/duse/cli/command.rb', line 69

def self.abstract
  @@abstract << self
end

.abstract?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/duse/cli/command.rb', line 65

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

.command_nameObject



153
154
155
156
# File 'lib/duse/cli/command.rb', line 153

def self.command_name
  return full_command.sub(super_command.full_command, '').strip.sub(' ', '-') if has_super_command?
  full_command
end

.description(description = nil) ⇒ Object



162
163
164
165
# File 'lib/duse/cli/command.rb', line 162

def self.description(description = nil)
  @description = description if description
  @description ||= ""
end

.full_commandObject



145
146
147
# File 'lib/duse/cli/command.rb', line 145

def self.full_command
  name[/[^:]*$/].split(/(?=[A-Z])/).map(&:downcase).join(' ')
end

.has_subcommands?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/duse/cli/command.rb', line 100

def self.has_subcommands?
  !@subcommands.empty?
end

.has_super_command?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/duse/cli/command.rb', line 81

def self.has_super_command?
  !@super_command.nil?
end

.skip(*names) ⇒ Object



73
74
75
# File 'lib/duse/cli/command.rb', line 73

def self.skip(*names)
  names.each { |n| define_method(n) {} }
end

.subcommand(command) ⇒ Object



89
90
91
92
93
94
# File 'lib/duse/cli/command.rb', line 89

def self.subcommand(command)
  return nil if command.nil?
  return subcommands.select { |sc| sc.command_name == command }.first if command.is_a? String
  command.super_command = self
  subcommands << command
end

.subcommandsObject



96
97
98
# File 'lib/duse/cli/command.rb', line 96

def self.subcommands
  @subcommands ||= []
end

.super_commandObject



85
86
87
# File 'lib/duse/cli/command.rb', line 85

def self.super_command
  @super_command
end

.super_command=(command_class) ⇒ Object



77
78
79
# File 'lib/duse/cli/command.rb', line 77

def self.super_command=(command_class)
  @super_command = command_class
end

Instance Method Details

#color(line, style) ⇒ Object



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

def color(line, style)
  return line.to_s unless interactive?
  terminal.color(line || '???', Array(style).map(&:to_sym))
end

#command_nameObject



158
159
160
# File 'lib/duse/cli/command.rb', line 158

def command_name
  self.class.command_name
end

#configObject



104
105
106
# File 'lib/duse/cli/command.rb', line 104

def config
  CLI.config
end

#error(message, &block) ⇒ Object



115
116
117
118
# File 'lib/duse/cli/command.rb', line 115

def error(message, &block)
  warn(message, &block)
  exit 1
end

#executeObject



43
44
45
46
47
# File 'lib/duse/cli/command.rb', line 43

def execute
  run *arguments
rescue Interrupt
  say "\naborted!"
end

#full_commandObject



149
150
151
# File 'lib/duse/cli/command.rb', line 149

def full_command
  self.class.full_command
end

#help(info = "") ⇒ Object



167
168
169
170
171
# File 'lib/duse/cli/command.rb', line 167

def help(info = "")
  return help_subcommands unless self.class.subcommands.empty?
  parser.banner = usage
  self.class.description.sub(/./) { |c| c.upcase } + ".\n\n" + info + parser.to_s
end

#help_subcommandsObject



173
174
175
176
177
178
179
# File 'lib/duse/cli/command.rb', line 173

def help_subcommands
  result = "#{self.class.description}\n\n"
  result << "Usage: duse #{full_command} COMMAND ...\n\nAvailable commands:\n\n"
  self.class.subcommands.each { |command_class| result << "\t#{color(command_class.command_name, :command).ljust(22)} #{color(command_class.description, :info)}\n" }
  result << "\nrun `duse help #{full_command} COMMAND` for more infos"
  result
end

#interactive?(io = output) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
139
# File 'lib/duse/cli/command.rb', line 136

def interactive?(io = output)
  return io.tty? if force_interactive.nil?
  force_interactive
end

#parse(args) ⇒ Object



37
38
39
40
41
# File 'lib/duse/cli/command.rb', line 37

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

#say(data, format = nil, style = nil) ⇒ Object



141
142
143
# File 'lib/duse/cli/command.rb', line 141

def say(data, format = nil, style = nil)
  terminal.say format(data, format, style)
end

#success(line) ⇒ Object



120
121
122
# File 'lib/duse/cli/command.rb', line 120

def success(line)
  say color(line, :success) if interactive?
end

#terminalObject



59
60
61
# File 'lib/duse/cli/command.rb', line 59

def terminal
  @terminal ||= HighLine.new(input, output)
end

#usageObject



181
182
183
# File 'lib/duse/cli/command.rb', line 181

def usage
  "Usage: " << color(usage_for(full_command, :run), :command)
end

#usage_for(prefix, method) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/duse/cli/command.rb', line 185

def usage_for(prefix, method)
  usage = "duse #{prefix}"
  method = method(method)
  if method.respond_to? :parameters
    method.parameters.each do |type, name|
      name = name.upcase
      name = "[#{name}]"   if type == :opt
      name = "[#{name}..]" if type == :rest
      usage << " #{name}"
    end
  elsif method.arity != 0
    usage << " ..."
  end
  usage << " [OPTIONS]"
end

#warn(message) ⇒ Object



108
109
110
111
112
113
# File 'lib/duse/cli/command.rb', line 108

def warn(message)
  write_to($stderr) do
    say color(message, :error)
    yield if block_given?
  end
end

#write_to(io) ⇒ Object



124
125
126
127
128
129
# File 'lib/duse/cli/command.rb', line 124

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