Class: ShellOpts::Grammar::Command

Inherits:
Node
  • Object
show all
Defined in:
lib/shellopts/grammar/command.rb

Overview

A command. Commands are organized hierarchically with a Program object as the root node

Sets Node#key to the name of the command incl. the exclamation point

Direct Known Subclasses

Program

Instance Attribute Summary collapse

Attributes inherited from Node

#key

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, option_list) ⇒ Command

Initialize a Command object. parent is the parent Command object or nil if this is the root object. name is the name of the command (without the exclamation mark), and option_list a list of Option objects



30
31
32
33
34
35
36
37
38
# File 'lib/shellopts/grammar/command.rb', line 30

def initialize(parent, name, option_list)
  super("#{name}!".to_sym)
  @name = name
  parent.attach(self) if parent
  @option_list = option_list
  @options = @option_list.flat_map { |opt| opt.names.map { |name| [name, opt] } }.to_h
  @commands = {}
  @command_list = []
end

Instance Attribute Details

#command_listObject (readonly)

List of commands in declaration order



25
26
27
# File 'lib/shellopts/grammar/command.rb', line 25

def command_list
  @command_list
end

#commandsObject (readonly)

Sub-commands of this command. Is a hash from sub-command name to command object



19
20
21
# File 'lib/shellopts/grammar/command.rb', line 19

def commands
  @commands
end

#nameObject (readonly)

Name of command (String). Name doesn’t include the exclamation point (‘!’)



12
13
14
# File 'lib/shellopts/grammar/command.rb', line 12

def name
  @name
end

#option_listObject (readonly)

List of options in declaration order



22
23
24
# File 'lib/shellopts/grammar/command.rb', line 22

def option_list
  @option_list
end

#optionsObject (readonly)

Hash from option names (both short and long names) to option. This means an option can occur more than once as the hash value



16
17
18
# File 'lib/shellopts/grammar/command.rb', line 16

def options
  @options
end

#parentObject (readonly)

Parent command. Nil if this is the top level command (the program)



9
10
11
# File 'lib/shellopts/grammar/command.rb', line 9

def parent
  @parent
end

Instance Method Details

#dump(&block) ⇒ Object

:nocov:



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/shellopts/grammar/command.rb', line 41

def dump(&block)
  puts "#{key.inspect}"
  indent {
    puts "parent: #{parent&.key.inspect}"
    puts "name: #{name.inspect}"
    yield if block_given?
    puts "options:"
    indent { option_list.each { |opt| opt.dump } }
    puts "commands: "
    indent { command_list.each { |cmd| cmd.dump } }
  }
end