Class: Consoler::Options
- Inherits:
-
Object
- Object
- Consoler::Options
- Defined in:
- lib/consoler/options.rb
Overview
List of options
Instance Attribute Summary collapse
-
#description ⇒ String
readonly
Description of the options.
Instance Method Summary collapse
-
#each {|Consoler::Option, Integer| ... } ⇒ Consoler::Options
Loop through all options.
-
#get(name) ⇒ Consoler::Option?
Get a option by its name.
-
#get_with_alias(name) ⇒ [(Consoler::Option, nil), (Consoler::Option, nil)]
Get a option by its name, with matched alias.
-
#initialize(options_def) ⇒ Options
constructor
Create a list of option based on a string definition.
-
#size ⇒ Integer
Get the number of options.
-
#to_definition ⇒ String
Get the definition for these options.
Constructor Details
#initialize(options_def) ⇒ Options
Create a list of option based on a string definition
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/consoler/options.rb', line 16 def initialize() @options = [] @description = nil return if .nil? # strip the description if (match = /(^|\s+)-- (?<description>.*)$/.match()) @description = match[:description] = [0...-match[0].size] end = .split ' ' tracker = Consoler::OptionalsTracker.new option_names = [] while (option_def = .shift) Consoler::Option.create option_def, tracker do |option| raise "Duplicate option name: #{option.name}" if option_names.include? option.name option_names.push option.name option.aliases.each do |alias_| raise "Duplicate alias name: #{alias_.name}" if option_names.include? alias_.name option_names.push alias_.name end @options.push option end end end |
Instance Attribute Details
#description ⇒ String (readonly)
Description of the options
9 10 11 |
# File 'lib/consoler/options.rb', line 9 def description @description end |
Instance Method Details
#each {|Consoler::Option, Integer| ... } ⇒ Consoler::Options
Loop through all options
83 84 85 86 87 88 89 |
# File 'lib/consoler/options.rb', line 83 def each @options.each_with_index do |option, i| yield option, i end self end |
#get(name) ⇒ Consoler::Option?
Get a option by its name
May be matched by one of its aliases
56 57 58 59 |
# File 'lib/consoler/options.rb', line 56 def get(name) option, = get_with_alias name option end |
#get_with_alias(name) ⇒ [(Consoler::Option, nil), (Consoler::Option, nil)]
Get a option by its name, with matched alias
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/consoler/options.rb', line 65 def get_with_alias(name) each do |option, _| if option.name == name return option, option end option.aliases.each do |alias_| return option, alias_ if alias_.name == name end end [nil, nil] end |
#size ⇒ Integer
Get the number of options
94 95 96 |
# File 'lib/consoler/options.rb', line 94 def size @options.size end |
#to_definition ⇒ String
Get the definition for these options
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/consoler/options.rb', line 101 def to_definition definition = '' optional = nil each do |option, i| definition += ' ' if optional.nil? && option.is_optional definition += '[' optional = option.is_optional end definition += option.to_definition # only close when the next option is not optional, or another optional group if option.is_optional && (@options[i + 1].nil? || optional != @options[i + 1].is_optional) definition += ']' optional = nil end end definition.strip end |