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 = nil, 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
:section: Workhorse methods - not usually used by client code.
-
#initialize(parent = nil, name = "") ⇒ CommandSet
constructor
A new instance of CommandSet.
- #path ⇒ Object
- #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 = nil, 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=nil, cmd_path=[]) set = self.new set.require_commands(mod, file, cmd_path) return set end |
Instance Method Details
#add_requirements(subject) ⇒ Object
200 201 202 203 204 205 |
# File 'lib/command-set/command-set.rb', line 200 def add_requirements(subject) @command_list.each_value do |command| command.add_requirements subject end return subject end |
#command_list ⇒ Object
278 279 280 |
# File 'lib/command-set/command-set.rb', line 278 def command_list return @command_list.dup end |
#completion_list(terms, prefix, subject) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/command-set/command-set.rb', line 237 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
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/command-set/command-set.rb', line 208 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
:section: Workhorse methods - not usually used by client code
180 181 182 |
# File 'lib/command-set/command-set.rb', line 180 def get_root command = @command_list[nil] end |
#path ⇒ Object
254 255 256 257 258 259 260 |
# File 'lib/command-set/command-set.rb', line 254 def path if @parent.nil? return [] else return @parent.path + [@name] end end |
#prompt ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/command-set/command-set.rb', line 184 def prompt if @prompt.nil? if @name.empty? return [/$/, ""] else return [/$/, "#@name : "] end else return @prompt end end |
#set_prompt(match, replace) ⇒ Object
196 197 198 |
# File 'lib/command-set/command-set.rb', line 196 def set_prompt(match, replace) @prompt = [match, replace] end |