Class: Command::CommandSet
- Inherits:
-
Object
- Object
- Command::CommandSet
- Extended by:
- Forwardable
- Includes:
- Common, 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, prefix = []) ⇒ Object
(also: #short_docs)
Returns the value of attribute documentation.
-
#most_recent_args ⇒ Object
Returns the value of attribute most_recent_args.
-
#root_blocks ⇒ Object
readonly
Returns the value of attribute root_blocks.
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_defaults(subject) ⇒ Object
- #apply_root_blocks(blocks) ⇒ Object
- #command_list ⇒ Object
- #command_names ⇒ Object
- #consume_terms(terms, subject, arg_hash) ⇒ Object
-
#each_command(path, visitor) ⇒ Object
:section: Workhorse methods - not usually used by client code.
- #get_root ⇒ Object
- #get_subject ⇒ Object
-
#initialize(name = "") ⇒ CommandSet
constructor
A new instance of CommandSet.
- #initialize_copy(original) ⇒ Object
- #prompt ⇒ Object
- #root_visit(terms, visitor) ⇒ Object
- #set_prompt(match, replace) ⇒ Object
- #visit(terms, visitor) ⇒ Object
Methods included from Common
#add_requirements, #completion_list, #find_command, #path, #process_terms
Methods included from DSL::CommandSetDefinition
#command, #define_commands, #include_commands, #require_commands, #root_command, #sub_command, #subject_defaults
Constructor Details
#initialize(name = "") ⇒ CommandSet
Returns a new instance of CommandSet.
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/command-set/command-set.rb', line 155 def initialize(name="") @name = name @command_list = { nil => RootCommand.setup(self, nil) {} } @included_sets = [] @subject_template = nil @documentation = "" @prompt = nil @arguments = [] @most_recent_args = {} @subject_defaults = proc {|s|} @context = [] @root_blocks = [] end |
Instance Attribute Details
#documentation(width, prefix = []) ⇒ Object Also known as: short_docs
Returns the value of attribute documentation.
179 180 181 |
# File 'lib/command-set/command-set.rb', line 179 def documentation @documentation end |
#most_recent_args ⇒ Object
Returns the value of attribute most_recent_args.
179 180 181 |
# File 'lib/command-set/command-set.rb', line 179 def most_recent_args @most_recent_args end |
#root_blocks ⇒ Object (readonly)
Returns the value of attribute root_blocks.
180 181 182 |
# File 'lib/command-set/command-set.rb', line 180 def root_blocks @root_blocks 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.
186 187 188 189 190 |
# File 'lib/command-set/command-set.rb', line 186 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.
203 204 205 206 207 |
# File 'lib/command-set/command-set.rb', line 203 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_defaults(subject) ⇒ Object
270 271 272 273 274 275 276 |
# File 'lib/command-set/command-set.rb', line 270 def add_defaults(subject) included_subject = @included_sets.inject(Subject.new) do |merger, (subset, )| merger.merge([:context], subset.get_subject) end subject.absorb(included_subject) @subject_defaults.call(subject) end |
#apply_root_blocks(blocks) ⇒ Object
256 257 258 259 260 261 |
# File 'lib/command-set/command-set.rb', line 256 def apply_root_blocks(blocks) @root_blocks += blocks blocks.each do |block| @command_list[nil].instance_eval(&block) end end |
#command_list ⇒ Object
310 311 312 |
# File 'lib/command-set/command-set.rb', line 310 def command_list return @command_list.dup end |
#command_names ⇒ Object
314 315 316 |
# File 'lib/command-set/command-set.rb', line 314 def command_names return @command_list.keys.compact end |
#consume_terms(terms, subject, arg_hash) ⇒ Object
246 247 248 249 250 |
# File 'lib/command-set/command-set.rb', line 246 def consume_terms(terms, subject, arg_hash) @command_list[nil].consume_terms(terms, subject, arg_hash) @most_recent_args = arg_hash.dup return arg_hash end |
#each_command(path, visitor) ⇒ Object
:section: Workhorse methods - not usually used by client code
219 220 221 222 223 |
# File 'lib/command-set/command-set.rb', line 219 def each_command(path, visitor) @command_list.each_pair do |term, command| command.each_command(path + [term], visitor) end end |
#get_root ⇒ Object
252 253 254 |
# File 'lib/command-set/command-set.rb', line 252 def get_root command = @command_list[nil] end |
#get_subject ⇒ Object
263 264 265 266 267 268 |
# File 'lib/command-set/command-set.rb', line 263 def get_subject subject = Subject.new add_requirements(subject) add_defaults(subject) return subject end |
#initialize_copy(original) ⇒ Object
169 170 171 172 173 174 175 176 177 |
# File 'lib/command-set/command-set.rb', line 169 def initialize_copy(original) super base_list = original.instance_variable_get("@command_list") @command_list = {} @context = [] #original.context.dup base_list.each_pair do |name, cmd| @command_list[name] = cmd.dup end end |
#prompt ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/command-set/command-set.rb', line 278 def prompt if @prompt.nil? if @name.empty? return [/$/, ""] else return [/$/, "#@name : "] end else return @prompt end end |
#root_visit(terms, visitor) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/command-set/command-set.rb', line 225 def root_visit(terms, visitor) next_hop = if(terms.empty?) visitor.set_out_of_terms(self) @command_list[nil] else @command_list[terms.first] end return visitor.term_without_hop(terms, self) if(next_hop.nil?) visitor.leave_from(terms.shift, terms, self) return next_hop.visit(terms, visitor) end |
#set_prompt(match, replace) ⇒ Object
290 291 292 |
# File 'lib/command-set/command-set.rb', line 290 def set_prompt(match, replace) @prompt = [match, replace] end |
#visit(terms, visitor) ⇒ Object
240 241 242 243 244 |
# File 'lib/command-set/command-set.rb', line 240 def visit(terms, visitor) visitor.arrive_at(terms, self) root_visit(terms, visitor) end |