Class: ShellOpts::Grammar::Program

Inherits:
Command show all
Defined in:
lib/shellopts/grammar/program.rb

Overview

Program is the root object of the grammar

Instance Attribute Summary collapse

Attributes inherited from Command

#command_list, #commands, #name, #option_list, #options, #parent

Attributes inherited from Node

#key

Instance Method Summary collapse

Constructor Details

#initialize(name, option_list) ⇒ Program

Initialize a top-level Program object



10
11
12
13
# File 'lib/shellopts/grammar/program.rb', line 10

def initialize(name, option_list)
  super(nil, name, option_list)
  @args = []
end

Instance Attribute Details

#argsObject (readonly)

Array of non-option litteral arguments (ie. what comes after the double dash (‘--’) in the usage definition). Initially empty but filled out during compilation



7
8
9
# File 'lib/shellopts/grammar/program.rb', line 7

def args
  @args
end

Instance Method Details

#dump(&block) ⇒ Object

:nocov:



26
27
28
29
30
31
# File 'lib/shellopts/grammar/program.rb', line 26

def dump(&block)
  super { 
    puts "args: #{args.inspect}" 
    puts "usage: #{usage.inspect}"
  }
end

#usageObject

Usage string to be used in error messages. The string is kept short by only listing the shortest option (if there is more than one)



17
18
19
20
21
22
23
# File 'lib/shellopts/grammar/program.rb', line 17

def usage
  (
    render_options(option_list) + 
    commands.values.map { |cmd| render_command(cmd) } + 
    args
  ).flatten.join(" ")
end