Class: Cinch::Commands::Command

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name, arguments, options = {}) ⇒ Command

Creates a new command.

Parameters:

  • name (Symbol)

    Name of the command.

  • arguments (Hash{Symbol => Symbol,Regexp,String,Array})

    Arguments names and their formats or possible values.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :aliases (Array)

    Additiona aliases for the command.

  • :summary (String)

    Short summary of the command.

  • :description (String)

    Long description of the command.



49
50
51
52
53
54
55
56
# File 'lib/cinch/commands/command.rb', line 49

def initialize(name,arguments,options={})
  @name        = name.to_s
  @arguments   = arguments
  @aliases     = options.fetch(:aliases,[]).map(&:to_s)

  @summary     = options[:summary]
  @description = options[:description]
end

Instance Attribute Details

#argumentsObject (readonly)

Argument names/formats



20
21
22
# File 'lib/cinch/commands/command.rb', line 20

def arguments
  @arguments
end

#descriptionObject (readonly)

Long description of the command



26
27
28
# File 'lib/cinch/commands/command.rb', line 26

def description
  @description
end

#nameObject (readonly)

Name of the command



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

def name
  @name
end

#summaryObject (readonly)

Short summary of the command



23
24
25
# File 'lib/cinch/commands/command.rb', line 23

def summary
  @summary
end

Instance Method Details

#namesArray<String>

The names for the command.

Returns:

  • (Array<String>)

    Command names.



64
65
66
# File 'lib/cinch/commands/command.rb', line 64

def names
  [@name] + @aliases
end

#regexpRegexp

Creates a Regular Expression that matches invocations of the command.

Returns:

  • (Regexp)

    A Regular Expression that matches the command and captures it's arguments.



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

#usageString

The usage string for the command.

Returns:

  • (String)

    The usage string for the command and it's arguments.



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