Class: Hammer::Command
- Inherits:
-
Object
- Object
- Hammer::Command
- Defined in:
- lib/hammer/command.rb
Overview
A single registered command on a Hammer class.
Instance Attribute Summary collapse
-
#alts ⇒ Object
readonly
Returns the value of attribute alts.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#examples ⇒ Object
readonly
Returns the value of attribute examples.
-
#handler ⇒ Object
Returns the value of attribute handler.
-
#location ⇒ Object
Returns the value of attribute location.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#needs ⇒ Object
readonly
Returns the value of attribute needs.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#prev_location ⇒ Object
Returns the value of attribute prev_location.
Instance Method Summary collapse
- #add_alt(name) ⇒ Object
- #add_example(text) ⇒ Object
- #add_need(name) ⇒ Object
- #add_option(option) ⇒ Object
-
#brief ⇒ Object
First line of ‘desc`, used in the flat command listing.
-
#finalize! ⇒ Object
Auto-assign a single-letter short alias (first letter of the opt name) to any opt that does not already declare one.
-
#initialize(name:, desc: '', handler: nil) ⇒ Command
constructor
A new instance of Command.
- #matches?(name) ⇒ Boolean
-
#to_h(path = name) ⇒ Object
Structured form for JSON export (‘h:json`).
Constructor Details
#initialize(name:, desc: '', handler: nil) ⇒ Command
Returns a new instance of Command.
7 8 9 10 11 12 13 14 15 |
# 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 = [] @needs = [] end |
Instance Attribute Details
#alts ⇒ Object (readonly)
Returns the value of attribute alts.
4 5 6 |
# File 'lib/hammer/command.rb', line 4 def alts @alts end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
4 5 6 |
# File 'lib/hammer/command.rb', line 4 def desc @desc end |
#examples ⇒ Object (readonly)
Returns the value of attribute examples.
4 5 6 |
# File 'lib/hammer/command.rb', line 4 def examples @examples end |
#handler ⇒ Object
Returns the value of attribute handler.
5 6 7 |
# File 'lib/hammer/command.rb', line 5 def handler @handler end |
#location ⇒ Object
Returns the value of attribute location.
5 6 7 |
# File 'lib/hammer/command.rb', line 5 def location @location end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/hammer/command.rb', line 4 def name @name end |
#needs ⇒ Object (readonly)
Returns the value of attribute needs.
4 5 6 |
# File 'lib/hammer/command.rb', line 4 def needs @needs end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/hammer/command.rb', line 4 def @options end |
#prev_location ⇒ Object
Returns the value of attribute prev_location.
5 6 7 |
# File 'lib/hammer/command.rb', line 5 def prev_location @prev_location end |
Instance Method Details
#add_alt(name) ⇒ Object
30 31 32 |
# File 'lib/hammer/command.rb', line 30 def add_alt(name) @alts << name.to_s end |
#add_example(text) ⇒ Object
26 27 28 |
# File 'lib/hammer/command.rb', line 26 def add_example(text) @examples << text.to_s end |
#add_need(name) ⇒ Object
34 35 36 |
# File 'lib/hammer/command.rb', line 34 def add_need(name) @needs << name.to_s end |
#add_option(option) ⇒ Object
22 23 24 |
# File 'lib/hammer/command.rb', line 22 def add_option(option) @options << option end |
#brief ⇒ Object
First line of ‘desc`, used in the flat command listing.
18 19 20 |
# File 'lib/hammer/command.rb', line 18 def brief @desc.lines.first&.chomp.to_s end |
#finalize! ⇒ Object
Auto-assign a single-letter short alias (first letter of the opt name) to any opt that does not already declare one. Explicit aliases and ‘-h` are reserved first, so they always win. On collision the opt simply gets no short form - long flag still works. Idempotent.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/hammer/command.rb', line 48 def finalize! return if @finalized @finalized = true claimed = ['-h'] @options.each do |o| o.aliases.each { |a| claimed << a if short_flag?(a) } end @options.each do |o| next if o.aliases.any? { |a| short_flag?(a) } short = "-#{o.name.to_s[0]}" next if claimed.include?(short) o.aliases << short claimed << short end end |
#matches?(name) ⇒ Boolean
38 39 40 41 |
# File 'lib/hammer/command.rb', line 38 def matches?(name) name = name.to_s name == @name || @alts.include?(name) end |
#to_h(path = name) ⇒ Object
Structured form for JSON export (‘h:json`). `path` is the full colon path supplied by the tree walk - a Command doesn’t know its own namespace prefix. ‘hidden` follows the help rule: a task with no `desc` still dispatches but is hidden from listings.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/hammer/command.rb', line 70 def to_h(path = name) { name: name, path: path, desc: desc, brief: brief, hidden: desc.empty?, redefined: !prev_location.nil?, location: location, alts: alts, needs: needs, examples: examples, options: .map(&:to_h) } end |