Class: CyberarmEngine::Console::Command
- Inherits:
-
Object
- Object
- CyberarmEngine::Console::Command
- Defined in:
- lib/cyberarm_engine/console/command.rb,
lib/cyberarm_engine/console/subcommand.rb
Direct Known Subclasses
Defined Under Namespace
Classes: SubCommand
Class Method Summary collapse
- .find(command) ⇒ Object
- .inherited(subclass) ⇒ Object
- .list_commands ⇒ Object
- .setup ⇒ Object
- .use(command, arguments, console) ⇒ Object
Instance Method Summary collapse
- #autocomplete(console) ⇒ Object
- #command ⇒ Object
- #get(key) ⇒ Object
- #group ⇒ Object
- #handle(arguments, console) ⇒ Object
- #handle_subcommand(arguments, console) ⇒ Object
-
#initialize ⇒ Command
constructor
A new instance of Command.
- #set(key, value) ⇒ Object
- #setup ⇒ Object
- #subcommand(command, type) ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize ⇒ Command
Returns a new instance of Command.
61 62 63 64 65 66 |
# File 'lib/cyberarm_engine/console/command.rb', line 61 def initialize @store = {} @subcommands = [] setup end |
Class Method Details
.find(command) ⇒ Object
53 54 55 |
# File 'lib/cyberarm_engine/console/command.rb', line 53 def self.find(command) @commands.detect { |cmd| cmd.command == command.to_sym } end |
.inherited(subclass) ⇒ Object
24 25 26 27 28 |
# File 'lib/cyberarm_engine/console/command.rb', line 24 def self.inherited(subclass) @list ||= [] @commands ||= [] @list << subclass end |
.list_commands ⇒ Object
57 58 59 |
# File 'lib/cyberarm_engine/console/command.rb', line 57 def self.list_commands @commands end |
.setup ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cyberarm_engine/console/command.rb', line 30 def self.setup @list ||= [] @commands = [] @list.each do |subclass| cmd = subclass.new if @commands.detect { |c| c.command == cmd.command } raise "Command '#{cmd.command}' from '#{cmd.class}' already exists!" end @commands << cmd end end |
.use(command, arguments, console) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/cyberarm_engine/console/command.rb', line 43 def self.use(command, arguments, console) found_command = @commands.detect { |cmd| cmd.command == command.to_sym } if found_command found_command.handle(arguments, console) else console.stdin("Command #{Style.error(command)} not found.") end end |
Instance Method Details
#autocomplete(console) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cyberarm_engine/console/command.rb', line 99 def autocomplete(console) split = console.text_input.text.split(" ") if @subcommands.size.positive? if !console.text_input.text.end_with?(" ") && split.size == 2 list = console.abbrev_search(@subcommands.map { |cmd| cmd.command.to_s }, split.last) if list.size == 1 console.text_input.text = "#{split.first} #{list.first} " else return unless list.size.positive? console.stdin(list.map { |cmd| Console::Style.highlight(cmd) }.join(", ").to_s) end # List available options on subcommand elsif (console.text_input.text.end_with?(" ") && split.size == 2) || !console.text_input.text.end_with?(" ") && split.size == 3 subcommand = @subcommands.detect { |cmd| cmd.command.to_s == (split[1]) } if subcommand if split.size == 2 console.stdin("Available options: #{subcommand.values.map { |value| Console::Style.highlight(value) }.join(',')}") else list = console.abbrev_search(subcommand.values, split.last) if list.size == 1 console.text_input.text = "#{split.first} #{split[1]} #{list.first} " elsif list.size.positive? console.stdin("Available options: #{list.map { |value| Console::Style.highlight(value) }.join(',')}") end end end # List available subcommands if command was entered and has only a space after it elsif console.text_input.text.end_with?(" ") && split.size == 1 console.stdin("Available subcommands: #{@subcommands.map { |cmd| Console::Style.highlight(cmd.command) }.join(', ')}") end end end |
#command ⇒ Object
91 92 93 |
# File 'lib/cyberarm_engine/console/command.rb', line 91 def command raise NotImplementedError end |
#get(key) ⇒ Object
79 80 81 |
# File 'lib/cyberarm_engine/console/command.rb', line 79 def get(key) @store[key] end |
#group ⇒ Object
87 88 89 |
# File 'lib/cyberarm_engine/console/command.rb', line 87 def group raise NotImplementedError end |
#handle(arguments, console) ⇒ Object
95 96 97 |
# File 'lib/cyberarm_engine/console/command.rb', line 95 def handle(arguments, console) raise NotImplementedError end |
#handle_subcommand(arguments, console) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/cyberarm_engine/console/command.rb', line 138 def handle_subcommand(arguments, console) if arguments.size.zero? console.stdin(usage) return end subcommand = arguments.delete_at(0) found_command = @subcommands.detect { |cmd| cmd.command == subcommand.to_sym } if found_command found_command.handle(arguments, console) else console.stdin("Unknown subcommand #{Style.error(subcommand)} for #{Style.highlight(command)}") end end |
#set(key, value) ⇒ Object
83 84 85 |
# File 'lib/cyberarm_engine/console/command.rb', line 83 def set(key, value) @store[key] = value end |
#setup ⇒ Object
68 69 |
# File 'lib/cyberarm_engine/console/command.rb', line 68 def setup end |
#subcommand(command, type) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/cyberarm_engine/console/command.rb', line 71 def subcommand(command, type) if @subcommands.detect { |subcmd| subcmd.command == command.to_sym } raise "Subcommand '#{command}' for '#{self.command}' already exists!" end @subcommands << SubCommand.new(self, command, type) end |
#usage ⇒ Object
153 154 155 |
# File 'lib/cyberarm_engine/console/command.rb', line 153 def usage raise NotImplementedError end |