SamovarSourceSamovarFlags

class Flags

Represents a collection of flag alternatives for an option.

Flags parse text like -f/--flag <value> into individual flag parsers.

Definitions

def initialize(text)

Initialize a new flags parser.

Signature

parameter text String

The flags specification string (e.g., -f/--flag <value>).

Implementation

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

def each(&block)

Iterate over each flag.

Signature

yields {|flag| ...}

Each flag in the collection.

Implementation

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

def first

Get the first flag.

Signature

returns Flag

The first flag.

Implementation

def first
	@ordered.first
end

def boolean?

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

Signature

returns Boolean

True if this is a boolean flag.

Implementation

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

def count

The number of flag alternatives.

Signature

returns Integer

The count of flags.

Implementation

def count
	return @ordered.count
end

def to_s

Generate a string representation for usage output.

Signature

returns String

The usage string.

Implementation

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

def parse(input)

Parse a flag from the input.

Signature

parameter input Array(String)

The command-line arguments.

returns Object | Nil

The parsed value, or nil if no match.

Implementation

def parse(input)
	@ordered.each do |flag|
		result = flag.parse(input)
		if result != nil
			return result
		end
	end
	
	return nil
end