Class: ShellOpts::Grammar::OptionGroup

Inherits:
Node
  • Object
show all
Defined in:
lib/shellopts/grammar.rb,
lib/shellopts/renderer.rb,
lib/shellopts/formatter.rb

Overview

Note that all public attributes are assigned by #attach

Constant Summary

Constants inherited from Node

Node::ALLOWED_PARENTS

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #parent, #token

Instance Method Summary collapse

Methods inherited from Node

#analyzer_error, #ancestors, #dump_ast, #dump_attrs, #dump_idr, #inspect, #parents, #parse, parse, #parser_error, #puts_help, #puts_usage, #remove_arg_descr_nodes, #remove_arg_spec_nodes, #remove_brief_nodes, #traverse

Constructor Details

#initialize(parent, token) ⇒ OptionGroup

Returns a new instance of OptionGroup.



176
177
178
179
180
181
182
# File 'lib/shellopts/grammar.rb', line 176

def initialize(parent, token)
  @options = []
  @brief = nil
  @default_brief = nil
  @description = []
  super(parent, token)
end

Instance Attribute Details

#briefObject (readonly)

Brief description of option(s)



171
172
173
# File 'lib/shellopts/grammar.rb', line 171

def brief
  @brief
end

#descriptionObject (readonly)

Description of option(s)



174
175
176
# File 'lib/shellopts/grammar.rb', line 174

def description
  @description
end

#optionsObject (readonly)

Array of options in declaration order



168
169
170
# File 'lib/shellopts/grammar.rb', line 168

def options
  @options
end

Instance Method Details

#nameObject

Duck typing for compatibility with IdrNode (TODO: maybe just make OptionGroup an IdrNode and be over with it)



165
# File 'lib/shellopts/grammar.rb', line 165

def name() options.first&.name end

#puts_descrObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/shellopts/formatter.rb', line 14

def puts_descr
  puts Ansi.bold(render(:multi))
  indent {
    if description.any?
      description.each { |descr|
        descr.puts_descr
        puts if descr != description.last
      }
    elsif brief
      brief.puts_descr
    end
  }
end

#render(format) ⇒ Object

Formats:

:enum   -a, --all -r, --recursive
:long   --all --recursive
:short  -a -r
:multi  -a, --all
        -r, --recursive


66
67
68
69
70
71
72
73
# File 'lib/shellopts/renderer.rb', line 66

def render(format)
  constrain format, :enum, :long, :short, :multi
  if format == :multi
    options.map { |option| option.render(:enum) }.join("\n")
  else
    options.map { |option| option.render(format) }.join(" ")
  end
end