Class: Samovar::Flags
- Inherits:
-
Object
- Object
- Samovar::Flags
- Includes:
- Enumerable
- Defined in:
- lib/samovar/flags.rb
Overview
Represents a collection of flag alternatives for an option.
Flags parse text like ‘-f/–flag <value>` into individual flag parsers.
Instance Method Summary collapse
-
#boolean? ⇒ Boolean
Whether this flag should have a true/false value if not specified otherwise.
-
#completions ⇒ Object
The possible flag prefixes for completion.
-
#count ⇒ Object
The number of flag alternatives.
-
#each(&block) ⇒ Object
Iterate over each flag.
-
#first ⇒ Object
Get the first flag.
-
#flag_for(token) ⇒ Object
Find the flag that matches the given token.
-
#initialize(text) ⇒ Flags
constructor
Initialize a new flags parser.
-
#parse(input) ⇒ Object
Parse a flag from the input.
-
#to_s ⇒ Object
Generate a string representation for usage output.
Constructor Details
Instance Method Details
#boolean? ⇒ Boolean
Whether this flag should have a true/false value if not specified otherwise.
54 55 56 |
# File 'lib/samovar/flags.rb', line 54 def boolean? @ordered.count == 1 and @ordered.first.boolean? end |
#completions ⇒ Object
The possible flag prefixes for completion.
40 41 42 |
# File 'lib/samovar/flags.rb', line 40 def completions @ordered.flat_map(&:completions) end |
#count ⇒ Object
The number of flag alternatives.
61 62 63 |
# File 'lib/samovar/flags.rb', line 61 def count return @ordered.count end |
#each(&block) ⇒ Object
Iterate over each flag.
25 26 27 |
# File 'lib/samovar/flags.rb', line 25 def each(&block) @ordered.each(&block) end |
#first ⇒ Object
Get the first flag.
47 48 49 |
# File 'lib/samovar/flags.rb', line 47 def first @ordered.first end |
#flag_for(token) ⇒ Object
Find the flag that matches the given token.
33 34 35 |
# File 'lib/samovar/flags.rb', line 33 def flag_for(token) @ordered.find{|flag| flag.prefix?(token)} end |
#parse(input) ⇒ Object
Parse a flag from the input.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/samovar/flags.rb', line 76 def parse(input) @ordered.each do |flag| result = flag.parse(input) if result != nil return result end end return nil end |
#to_s ⇒ Object
Generate a string representation for usage output.
68 69 70 |
# File 'lib/samovar/flags.rb', line 68 def to_s "[#{@ordered.join(' | ')}]" end |