Class: Command::CommandSet
- Inherits:
-
Object
- Object
- Command::CommandSet
- Includes:
- DSL::CommandSetDefinition
- Defined in:
- lib/command-set/command-set.rb
Overview
This class packs up a set of commands, for presentation to an interpreter. CommandSet objects are defined using methods from DSL::CommandSetDefinition
Instance Attribute Summary collapse
-
#documentation(width, parent = "") ⇒ Object
(also: #short_docs)
Returns the value of attribute documentation.
-
#parent ⇒ Object
Returns the value of attribute parent.
Class Method Summary collapse
-
.define_commands(&block) ⇒ Object
The preferred way to use a CommandSet is to call CommandSet::define_commands with a block, and then call #command, #include_commands and #sub_command on it.
-
.require_commands(mod, file, cmd_path = []) ⇒ Object
In driver code, it’s often quickest to yank in commands from a file.
Instance Method Summary collapse
- #add_requirements(subject) ⇒ Object
- #command_list ⇒ Object
- #completion_list(terms, prefix, subject) ⇒ Object
-
#find_command(*terms) ⇒ Object
Given a set of terms, returns a Command or CommandSet, plus an extraneous (probably argument) terms.
- #get_root ⇒ Object
-
#initialize(parent = nil, name = "") ⇒ CommandSet
constructor
A new instance of CommandSet.
- #instance(subject) ⇒ Object
- #path ⇒ Object
-
#paths_update(command, name) ⇒ Object
:section: Workhorse methods - not usually used by client code.
- #prompt ⇒ Object
- #set_prompt(match, replace) ⇒ Object
Methods included from DSL::CommandSetDefinition
#command, #define_commands, #include_commands, #require_commands, #root_command, #sub_command
Constructor Details
#initialize(parent = nil, name = "") ⇒ CommandSet
Returns a new instance of CommandSet.
136 137 138 139 140 141 142 143 |
# File 'lib/command-set/command-set.rb', line 136 def initialize(parent=nil, name="") @parent = parent @name = name @command_list = Hash.new @subject_template = nil @documentation = "" @prompt = nil end |
Instance Attribute Details
#documentation(width, parent = "") ⇒ Object Also known as: short_docs
Returns the value of attribute documentation.
145 146 147 |
# File 'lib/command-set/command-set.rb', line 145 def documentation @documentation end |
#parent ⇒ Object
Returns the value of attribute parent.
145 146 147 |
# File 'lib/command-set/command-set.rb', line 145 def parent @parent end |
Class Method Details
.define_commands(&block) ⇒ Object
The preferred way to use a CommandSet is to call CommandSet::define_commands with a block, and then call #command, #include_commands and #sub_command on it.
151 152 153 154 155 |
# File 'lib/command-set/command-set.rb', line 151 def define_commands(&block) set = self.new set.define_commands(&block) return set end |
.require_commands(mod, file, cmd_path = []) ⇒ Object
In driver code, it’s often quickest to yank in commands from a file. To do that, create a code file with a module in it. The module needs a method of the form
def self.define_commands()
define_commands should return a CommandSet. Then, pass the require path and module name to require_commands, and it’ll take care of creating the command set. You can even call DSL::CommandSetDefinition#define_commands on the set that’s returned in order to add one-off commands or fold in other command sets.
168 169 170 171 172 |
# File 'lib/command-set/command-set.rb', line 168 def require_commands(mod, file, cmd_path=[]) set = self.new set.require_commands(mod, file, cmd_path) return set end |
Instance Method Details
#add_requirements(subject) ⇒ Object
208 209 210 211 212 213 |
# File 'lib/command-set/command-set.rb', line 208 def add_requirements(subject) @command_list.each_value do |command| command.add_requirements subject end return subject end |
#command_list ⇒ Object
294 295 296 |
# File 'lib/command-set/command-set.rb', line 294 def command_list return @command_list.dup end |
#completion_list(terms, prefix, subject) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/command-set/command-set.rb', line 245 def completion_list(terms, prefix, subject) if terms.length == 0 list = @command_list.keys.grep(%r{^#{prefix}.*}) if @command_list.has_key?(nil) list += @command_list[nil].completion_list([], prefix, subject) end return list end begin command,terms = find_command(*terms) return command.completion_list(terms, prefix, subject) rescue CommandException => ce return [] end end |
#find_command(*terms) ⇒ Object
Given a set of terms, returns a Command or CommandSet, plus an extraneous (probably argument) terms
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/command-set/command-set.rb', line 216 def find_command(*terms) if terms.empty? return self,[] end command_name = terms.shift command = @command_list[command_name] if Class === command && Command > command if command.defined? return command,terms else raise CommandError, "#{command_name}: unfinalized command: #{command.inspect}" end elsif CommandSet === command return command.find_command(*terms) elsif command.nil? if @command_list[nil].nil? raise CommandException, "Unknown command: #{command_name.inspect}" else return @command_list[nil], terms.unshift(command_name) end else raise ScriptError, "Somehow a non-command made it's " + "way into the command registry. -- (#{command.inspect})" end end |
#get_root ⇒ Object
188 189 190 |
# File 'lib/command-set/command-set.rb', line 188 def get_root command = @command_list[nil] end |
#instance(subject) ⇒ Object
286 287 288 289 290 291 292 |
# File 'lib/command-set/command-set.rb', line 286 def instance(subject) if @command_list.has_key?(nil) return @command_list[nil].new(subject) else raise CommandException, "incomplete command" end end |
#path ⇒ Object
262 263 264 265 266 267 268 |
# File 'lib/command-set/command-set.rb', line 262 def path if @parent.nil? return [] else return @parent.path + [@name] end end |
#paths_update(command, name) ⇒ Object
:section: Workhorse methods - not usually used by client code
180 181 182 183 184 185 186 |
# File 'lib/command-set/command-set.rb', line 180 def paths_update(command, name) command.add_path(self, [], name) @paths.each do |set, path| command.add_path(set, path, name) end end |
#prompt ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/command-set/command-set.rb', line 192 def prompt if @prompt.nil? if @name.empty? return [/$/, ""] else return [/$/, "#@name : "] end else return @prompt end end |
#set_prompt(match, replace) ⇒ Object
204 205 206 |
# File 'lib/command-set/command-set.rb', line 204 def set_prompt(match, replace) @prompt = [match, replace] end |