Class: Samovar::Flags

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(text) ⇒ Flags

Initialize a new flags parser.



16
17
18
19
20
# File 'lib/samovar/flags.rb', line 16

def initialize(text)
	@text = text
	
	@ordered = text.split(/\s+\|\s+/).map{|part| Flag.parse(part)}
end

Instance Method Details

#boolean?Boolean

Whether this flag should have a true/false value if not specified otherwise.

Returns:

  • (Boolean)


54
55
56
# File 'lib/samovar/flags.rb', line 54

def boolean?
	@ordered.count == 1 and @ordered.first.boolean?
end

#completionsObject

The possible flag prefixes for completion.



40
41
42
# File 'lib/samovar/flags.rb', line 40

def completions
	@ordered.flat_map(&:completions)
end

#countObject

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

#firstObject

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_sObject

Generate a string representation for usage output.



68
69
70
# File 'lib/samovar/flags.rb', line 68

def to_s
	"[#{@ordered.join(' | ')}]"
end