Class: Samovar::BooleanFlag

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

Overview

Represents a boolean flag with ‘–flag` and `–no-flag` variants.

Instance Attribute Summary

Attributes inherited from Flag

#alternatives, #prefix, #text

Instance Method Summary collapse

Methods inherited from Flag

#boolean?, #completions, #key, parse, #to_s

Constructor Details

#initialize(text, prefix, value = nil) ⇒ BooleanFlag

Initialize a new boolean flag.



236
237
238
239
240
241
242
243
# File 'lib/samovar/flags.rb', line 236

def initialize(text, prefix, value = nil)
	super(text, prefix)
	
	@value = value
	
	@negated = @prefix.sub(/^--/, "--no-")
	@alternatives = [@negated]
end

Instance Method Details

#parse(input) ⇒ Object

Parse this flag from the input.



257
258
259
260
261
262
263
264
265
# File 'lib/samovar/flags.rb', line 257

def parse(input)
	if input.first == @prefix
		input.shift
		return true
	elsif input.first == @negated
		input.shift
		return false
	end
end

#prefix?(token) ⇒ Boolean

Check if the token matches this flag.

Returns:

  • (Boolean)


249
250
251
# File 'lib/samovar/flags.rb', line 249

def prefix?(token)
	@prefix == token or @negated == token
end