Class: Cinch::Commands::Command
- Inherits:
-
Object
- Object
- Cinch::Commands::Command
- Defined in:
- lib/cinch/commands/command.rb
Constant Summary collapse
- ARG_FORMATS =
Argument formats
{ string: /\S+/, integer: /\d+/, float: /\d*\.\d+/, text: /.+/ }
Instance Attribute Summary collapse
-
#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.
49 50 51 52 53 54 55 56 |
# File 'lib/cinch/commands/command.rb', line 49 def initialize(name,arguments,={}) @name = name.to_s @arguments = arguments @aliases = .fetch(:aliases,[]).map(&:to_s) @summary = [:summary] @description = [:description] end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Argument names/formats
20 21 22 |
# File 'lib/cinch/commands/command.rb', line 20 def arguments @arguments end |
#description ⇒ Object (readonly)
Long description of the command
26 27 28 |
# File 'lib/cinch/commands/command.rb', line 26 def description @description end |
#name ⇒ Object (readonly)
Name of the command
17 18 19 |
# File 'lib/cinch/commands/command.rb', line 17 def name @name end |
#summary ⇒ Object (readonly)
Short summary of the command
23 24 25 |
# File 'lib/cinch/commands/command.rb', line 23 def summary @summary end |
Instance Method Details
#names ⇒ Array<String>
The names for the command.
64 65 66 |
# File 'lib/cinch/commands/command.rb', line 64 def names [@name] + @aliases end |
#regexp ⇒ Regexp
Creates a Regular Expression that matches invocations of the command.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/cinch/commands/command.rb', line 75 def regexp pattern = '(?:' + Regexp.union([@name] + @aliases).source + ')' @arguments.each_value do |format| arg_regexp = case format when Array then Regexp.union(format) when Regexp then format when Symbol then ARG_FORMATS.fetch(format) else Regexp.escape(format.to_s) end pattern << ' (' << arg_regexp.source << ')' end # match the full message pattern << '$' return Regexp.new(pattern) end |
#usage ⇒ String
The usage string for the command.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cinch/commands/command.rb', line 101 def usage usage = "#{@name}" @arguments.each do |arg,format| usage << ' ' << case format when Array then "[#{format.join('|')}]" when Regexp then format.source when Symbol then arg.to_s.upcase else format.to_s end end return usage end |