Class: Sandbox::Command
- Inherits:
-
Object
- Object
- Sandbox::Command
- Defined in:
- lib/sandbox/command.rb
Overview
Command
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#completion_proc ⇒ Object
readonly
Returns the value of attribute completion_proc.
-
#description ⇒ Object
Returns the value of attribute description.
-
#global ⇒ Object
Returns the value of attribute global.
-
#name ⇒ Object
Returns the value of attribute name.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#completion(&block) ⇒ Object
Sets a block for the readline auto completion.
-
#exec(tokens) ⇒ Object
Executes the command.
-
#global? ⇒ Boolean
Returns true if the command is global.
-
#initialize(name, shell, context, block, **options) ⇒ Command
constructor
Creates a new command.
-
#match?(name) ⇒ Boolean
Returns true if the name matches the command name or alias.
-
#print_usage ⇒ Object
Prints command usage.
-
#to_s ⇒ Object
Returns the string representation of the command.
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, **) @name = name.to_sym @shell = shell @context = context @block = block @description = [:description] @global = [:global] @aliases = [:aliases]&.map(&:to_sym) @params = {} params = [: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
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
7 8 9 |
# File 'lib/sandbox/command.rb', line 7 def aliases @aliases end |
#completion_proc ⇒ Object (readonly)
Returns the value of attribute completion_proc.
7 8 9 |
# File 'lib/sandbox/command.rb', line 7 def completion_proc @completion_proc end |
#description ⇒ Object
Returns the value of attribute description.
8 9 10 |
# File 'lib/sandbox/command.rb', line 8 def description @description end |
#global ⇒ Object
Returns the value of attribute global.
8 9 10 |
# File 'lib/sandbox/command.rb', line 8 def global @global end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/sandbox/command.rb', line 8 def name @name end |
#params ⇒ Object (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 |
#print_usage ⇒ Object
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_s ⇒ Object
Returns the string representation of the command
61 62 63 |
# File 'lib/sandbox/command.rb', line 61 def to_s @name.to_s end |