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.



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

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)



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

def brief
  @brief
end

#descriptionObject (readonly)

Description of option(s)



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

def description
  @description
end

#optionsObject (readonly)

Array of options in declaration order



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

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)



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

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


73
74
75
76
77
78
79
80
# File 'lib/shellopts/renderer.rb', line 73

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