Class: Hammer::Command

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

Overview

A single registered command on a Hammer class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, desc: '', handler: nil) ⇒ Command

Returns a new instance of Command.



7
8
9
10
11
12
13
14
# File 'lib/hammer/command.rb', line 7

def initialize(name:, desc: '', handler: nil)
  @name     = name.to_s
  @desc     = desc.to_s.rstrip
  @handler  = handler
  @options  = []
  @examples = []
  @alts     = []
end

Instance Attribute Details

#altsObject (readonly)

Returns the value of attribute alts.



4
5
6
# File 'lib/hammer/command.rb', line 4

def alts
  @alts
end

#descObject (readonly)

Returns the value of attribute desc.



4
5
6
# File 'lib/hammer/command.rb', line 4

def desc
  @desc
end

#examplesObject (readonly)

Returns the value of attribute examples.



4
5
6
# File 'lib/hammer/command.rb', line 4

def examples
  @examples
end

#handlerObject

Returns the value of attribute handler.



5
6
7
# File 'lib/hammer/command.rb', line 5

def handler
  @handler
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/hammer/command.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/hammer/command.rb', line 4

def options
  @options
end

Instance Method Details

#add_alt(name) ⇒ Object



29
30
31
# File 'lib/hammer/command.rb', line 29

def add_alt(name)
  @alts << name.to_s
end

#add_example(text) ⇒ Object



25
26
27
# File 'lib/hammer/command.rb', line 25

def add_example(text)
  @examples << text.to_s
end

#add_option(option) ⇒ Object



21
22
23
# File 'lib/hammer/command.rb', line 21

def add_option(option)
  @options << option
end

#briefObject

First line of ‘desc`, used in the flat command listing.



17
18
19
# File 'lib/hammer/command.rb', line 17

def brief
  @desc.lines.first&.chomp.to_s
end

#matches?(name) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/hammer/command.rb', line 33

def matches?(name)
  name = name.to_s
  name == @name || @alts.include?(name)
end