Class: Samovar::Flag

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

Overview

Represents a single command-line flag.

A flag can be a simple boolean flag or a flag that accepts a value.

Direct Known Subclasses

BooleanFlag, ValueFlag

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, prefix, alternatives = nil) ⇒ Flag

Initialize a new flag.



94
95
96
97
98
# File 'lib/samovar/flags.rb', line 94

def initialize(text, prefix, alternatives = nil)
  @text = text
  @prefix = prefix
  @alternatives = alternatives
end

Instance Attribute Details

#alternativesObject (readonly)

Alternative flag prefixes.



113
114
115
# File 'lib/samovar/flags.rb', line 113

def alternatives
  @alternatives
end

#prefixObject (readonly)

The primary flag prefix.



108
109
110
# File 'lib/samovar/flags.rb', line 108

def prefix
  @prefix
end

#textObject (readonly)

The full flag specification text.



103
104
105
# File 'lib/samovar/flags.rb', line 103

def text
  @text
end

Class Method Details

.parse(text) ⇒ Object

Parse a flag specification string into a flag instance.



79
80
81
82
83
84
85
86
87
# File 'lib/samovar/flags.rb', line 79

def self.parse(text)
  if text =~ /(.*?)\s(\<.*?\>)/
    ValueFlag.new(text, $1, $2)
  elsif text =~ /--\[no\]-(.*?)$/
    BooleanFlag.new(text, "--#{$1}")
  else
    ValueFlag.new(text, text, nil)
  end
end

Instance Method Details

#boolean?Boolean

Whether this is a boolean flag.

Returns:

  • (Boolean)


132
133
134
# File 'lib/samovar/flags.rb', line 132

def boolean?
  false
end

#keyObject

Generate a key name for this flag.



125
126
127
# File 'lib/samovar/flags.rb', line 125

def key
  @key ||= @prefix.sub(/^-*/, "").gsub("-", "_").to_sym
end

#to_sObject

Generate a string representation for usage output.



118
119
120
# File 'lib/samovar/flags.rb', line 118

def to_s
  @text
end