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?, #key, parse, #to_s

Constructor Details

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

Initialize a new boolean flag.



204
205
206
207
208
209
210
211
# File 'lib/samovar/flags.rb', line 204

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.



225
226
227
228
229
230
231
232
233
# File 'lib/samovar/flags.rb', line 225

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)


217
218
219
# File 'lib/samovar/flags.rb', line 217

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