SamovarSourceSamovarValueFlag

class ValueFlag

Represents a flag that accepts a value or acts as a boolean.

Definitions

def initialize(text, prefix, value)

Initialize a new value flag.

Signature

parameter text String

The full flag specification text.

parameter prefix String

The primary flag prefix with alternatives (e.g., -f/--flag).

parameter value String | Nil

The value placeholder (e.g., <file>).

Implementation

def initialize(text, prefix, value)
	super(text, prefix)
	
	@value = value
	
	*@alternatives, @prefix = @prefix.split("/")
end

attr :alternatives

Alternative flag prefixes.

Signature

attribute Array(String)

attr :value

The value placeholder.

Signature

attribute String | Nil

def boolean?

Whether this is a boolean flag (no value required).

Signature

returns Boolean

True if no value is required.

Implementation

def boolean?
	@value.nil?
end

def prefix?(token)

Check if the token matches this flag.

Signature

parameter token String

The token to check.

returns Boolean

True if the token matches.

Implementation

def prefix?(token)
	@prefix == token or @alternatives.include?(token)
end

def parse(input)

Parse this flag from the input.

Signature

parameter input Array(String)

The command-line arguments.

returns String | Symbol | Nil

The parsed value.

Implementation

def parse(input)
	if prefix?(input.first)
		# Whether we are expecting to parse a value from input:
		if @value
			# Get the actual value from input:
			flag, value = input.shift(2)
			return value
		else
			# Otherwise, we are just a boolean flag:
			input.shift
			return key
		end
	end
end