Class: Samovar::Flag
- Inherits:
-
Object
- Object
- Samovar::Flag
- 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
Instance Attribute Summary collapse
-
#alternatives ⇒ Object
readonly
Alternative flag prefixes.
-
#prefix ⇒ Object
readonly
The primary flag prefix.
-
#text ⇒ Object
readonly
The full flag specification text.
Class Method Summary collapse
-
.parse(text) ⇒ Object
Parse a flag specification string into a flag instance.
Instance Method Summary collapse
-
#boolean? ⇒ Boolean
Whether this is a boolean flag.
-
#initialize(text, prefix, alternatives = nil) ⇒ Flag
constructor
Initialize a new flag.
-
#key ⇒ Object
Generate a key name for this flag.
-
#to_s ⇒ Object
Generate a string representation for usage output.
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
#alternatives ⇒ Object (readonly)
Alternative flag prefixes.
113 114 115 |
# File 'lib/samovar/flags.rb', line 113 def alternatives @alternatives end |
#prefix ⇒ Object (readonly)
The primary flag prefix.
108 109 110 |
# File 'lib/samovar/flags.rb', line 108 def prefix @prefix end |
#text ⇒ Object (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.
132 133 134 |
# File 'lib/samovar/flags.rb', line 132 def boolean? false end |
#key ⇒ Object
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_s ⇒ Object
Generate a string representation for usage output.
118 119 120 |
# File 'lib/samovar/flags.rb', line 118 def to_s @text end |