Class: Cinch::Commands::Command

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

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.



52
53
54
55
56
57
58
59
60
# File 'lib/commands/command.rb', line 52

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

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

Instance Attribute Details

#adminObject (readonly)

if admin-only



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

def admin
  @admin
end

#argumentsObject (readonly)

Argument names/formats



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

def arguments
  @arguments
end

#descriptionObject (readonly)

Long description of the command



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

def description
  @description
end

#nameObject (readonly)

Name of the command



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

def name
  @name
end

#summaryObject (readonly)

Short summary of the command



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

def summary
  @summary
end

Instance Method Details

#namesArray<String>

The names for the command.

Returns:

  • (Array<String>)

    Command names.



68
69
70
# File 'lib/commands/command.rb', line 68

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.



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

#usageString

The usage string for the command.

Returns:

  • (String)

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



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