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

#completions, #key, parse, #to_s

Constructor Details

#initialize(text, prefix, value) ⇒ ValueFlag

Initialize a new value flag.



176
177
178
179
180
181
182
# File 'lib/samovar/flags.rb', line 176

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

Instance Attribute Details

#alternativesObject (readonly)

Alternative flag prefixes.



187
188
189
# File 'lib/samovar/flags.rb', line 187

def alternatives
  @alternatives
end

#valueObject (readonly)

The value placeholder.



192
193
194
# File 'lib/samovar/flags.rb', line 192

def value
  @value
end

Instance Method Details

#boolean?Boolean

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

Returns:

  • (Boolean)


197
198
199
# File 'lib/samovar/flags.rb', line 197

def boolean?
	@value.nil?
end

#parse(input) ⇒ Object

Parse this flag from the input.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/samovar/flags.rb', line 213

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)


205
206
207
# File 'lib/samovar/flags.rb', line 205

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