Class: Sandbox::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/sandbox/command.rb

Overview

Command

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, shell, context, block, **options) ⇒ Command

Creates a new command



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sandbox/command.rb', line 12

def initialize(name, shell, context, block, **options)
  @name = name.to_sym
  @shell = shell
  @context = context
  @block = block
  @description = options[:description]
  @global = options[:global]
  @aliases = options[:aliases]&.map(&:to_sym)

  @params = {}
  params = options[:params]
  params&.each do |param|
    param = param.strip
    if param.start_with?('[') && param.end_with?(']')
      @params[param] = false
    elsif param.start_with?('<') && param.end_with?('>')
      @params[param] = true
    end
  end

  @completion_proc = nil
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



7
8
9
# File 'lib/sandbox/command.rb', line 7

def aliases
  @aliases
end

#completion_procObject (readonly)

Returns the value of attribute completion_proc.



7
8
9
# File 'lib/sandbox/command.rb', line 7

def completion_proc
  @completion_proc
end

#descriptionObject

Returns the value of attribute description.



8
9
10
# File 'lib/sandbox/command.rb', line 8

def description
  @description
end

#globalObject

Returns the value of attribute global.



8
9
10
# File 'lib/sandbox/command.rb', line 8

def global
  @global
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/sandbox/command.rb', line 8

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/sandbox/command.rb', line 7

def params
  @params
end

Instance Method Details

#completion(&block) ⇒ Object

Sets a block for the readline auto completion



49
50
51
# File 'lib/sandbox/command.rb', line 49

def completion(&block)
  @completion_proc = block
end

#exec(tokens) ⇒ Object

Executes the command



37
38
39
40
41
42
43
44
45
# File 'lib/sandbox/command.rb', line 37

def exec(tokens)
  mandatory = @params.count { |_, v| v }
  if mandatory > (tokens.length - 1)
    print_usage
    return
  end

  @block&.call(tokens, @shell, @context, self)
end

#global?Boolean

Returns true if the command is global



55
56
57
# File 'lib/sandbox/command.rb', line 55

def global?
  @global
end

#match?(name) ⇒ Boolean

Returns true if the name matches the command name or alias



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

def match?(name)
  name = name&.to_sym
  @name == name || @aliases&.include?(name)
end

Prints command usage



74
75
76
77
# File 'lib/sandbox/command.rb', line 74

def print_usage
  @shell.print("Usage: #{@name} ")
  @shell.puts(params.keys.join(' '))
end

#to_sObject

Returns the string representation of the command



61
62
63
# File 'lib/sandbox/command.rb', line 61

def to_s
  @name.to_s
end