Class: Cinch::Commands::Command
- Inherits:
-
Object
- Object
- Cinch::Commands::Command
- Defined in:
- lib/commands/command.rb
Constant Summary collapse
- ARG_FORMATS =
Argument formats
{ string: /\S+/, integer: /\d+/, float: /\d*\.\d+/, text: /.+/ }
Instance Attribute Summary collapse
-
#admin ⇒ Object
readonly
if admin-only.
-
#arguments ⇒ Object
readonly
Argument names/formats.
-
#description ⇒ Object
readonly
Long description of the command.
-
#name ⇒ Object
readonly
Name of the command.
-
#summary ⇒ Object
readonly
Short summary of the command.
Instance Method Summary collapse
-
#initialize(name, arguments, options = {}) ⇒ Command
constructor
Creates a new command.
-
#names ⇒ Array<String>
The names for the command.
-
#regexp ⇒ Regexp
Creates a Regular Expression that matches invocations of the command.
-
#usage ⇒ String
The usage string for the command.
Constructor Details
#initialize(name, arguments, options = {}) ⇒ Command
Creates a new command.
52 53 54 55 56 57 58 59 60 |
# File 'lib/commands/command.rb', line 52 def initialize(name,arguments,={}) @name = name.to_s @arguments = arguments @aliases = .fetch(:aliases,[]).map(&:to_s) @summary = [:summary] @description = [:description] @admin = [:admin] end |
Instance Attribute Details
#admin ⇒ Object (readonly)
if admin-only
29 30 31 |
# File 'lib/commands/command.rb', line 29 def admin @admin end |
#arguments ⇒ Object (readonly)
Argument names/formats
20 21 22 |
# File 'lib/commands/command.rb', line 20 def arguments @arguments end |
#description ⇒ Object (readonly)
Long description of the command
26 27 28 |
# File 'lib/commands/command.rb', line 26 def description @description end |
#name ⇒ Object (readonly)
Name of the command
17 18 19 |
# File 'lib/commands/command.rb', line 17 def name @name end |
#summary ⇒ Object (readonly)
Short summary of the command
23 24 25 |
# File 'lib/commands/command.rb', line 23 def summary @summary end |
Instance Method Details
#names ⇒ Array<String>
The names for the command.
68 69 70 |
# File 'lib/commands/command.rb', line 68 def names [@name] + @aliases end |
#regexp ⇒ Regexp
Creates a Regular Expression that matches invocations of the command.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/commands/command.rb', line 79 def regexp pattern = '(?:' + Regexp.union([@name] + @aliases).source + ')' @arguments.each_value do |format| arg_regexp = case format when Symbol ARG_FORMATS.fetch(format) when Regexp format else Regexp.union(format) end pattern << ' (' << arg_regexp.source << ')' end # match the full message pattern << '$' Regexp.new(pattern) end |
#usage ⇒ String
The usage string for the command.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/commands/command.rb', line 107 def usage usage = "#{@name}" @arguments.each do |arg,format| usage << ' ' << case format when Array '[' + format.join('|') + ']' when String format.to_s else arg.to_s.upcase end end usage end |