Class: Samovar::ValueFlag

Inherits:
Flag
  • Object
show all
Defined in:
lib/samovar/flags.rb

Overview

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

Instance Attribute Summary collapse

Attributes inherited from Flag

#prefix, #text

Instance Method Summary collapse

Methods inherited from Flag

#key, parse, #to_s

Constructor Details

#initialize(text, prefix, value) ⇒ ValueFlag

Initialize a new value flag.



144
145
146
147
148
149
150
# File 'lib/samovar/flags.rb', line 144

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

Instance Attribute Details

#alternativesObject (readonly)

Alternative flag prefixes.



155
156
157
# File 'lib/samovar/flags.rb', line 155

def alternatives
  @alternatives
end

#valueObject (readonly)

The value placeholder.



160
161
162
# File 'lib/samovar/flags.rb', line 160

def value
  @value
end

Instance Method Details

#boolean?Boolean

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

Returns:

  • (Boolean)


165
166
167
# File 'lib/samovar/flags.rb', line 165

def boolean?
	@value.nil?
end

#parse(input) ⇒ Object

Parse this flag from the input.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/samovar/flags.rb', line 181

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

#prefix?(token) ⇒ Boolean

Check if the token matches this flag.

Returns:

  • (Boolean)


173
174
175
# File 'lib/samovar/flags.rb', line 173

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