Class: Samovar::Flags

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



14
15
16
17
18
# File 'lib/samovar/flags.rb', line 14

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)


37
38
39
# File 'lib/samovar/flags.rb', line 37

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

#countObject

The number of flag alternatives.



44
45
46
# File 'lib/samovar/flags.rb', line 44

def count
	return @ordered.count
end

#each(&block) ⇒ Object

Iterate over each flag.



23
24
25
# File 'lib/samovar/flags.rb', line 23

def each(&block)
	@ordered.each(&block)
end

#firstObject

Get the first flag.



30
31
32
# File 'lib/samovar/flags.rb', line 30

def first
	@ordered.first
end

#parse(input) ⇒ Object

Parse a flag from the input.



59
60
61
62
63
64
65
66
67
68
# File 'lib/samovar/flags.rb', line 59

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.



51
52
53
# File 'lib/samovar/flags.rb', line 51

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