Class: Toys::Definition::Flag
- Inherits:
-
Object
- Object
- Toys::Definition::Flag
- Defined in:
- lib/toys/definition/flag.rb
Overview
Representation of a formal set of flags that set a particular context key. The flags within a single Flag definition are synonyms.
Constant Summary collapse
- DEFAULT_HANDLER =
The default handler replaces the previous value.
->(val, _prev) { val }
Instance Attribute Summary collapse
-
#accept ⇒ Object
readonly
Returns the acceptor, which may be
nil
. -
#default ⇒ Object
readonly
Returns the default value, which may be
nil
. -
#desc ⇒ Toys::Utils::WrappableString
Returns the short description string.
-
#flag_syntax ⇒ Array<FlagSyntax>
readonly
Returns an array of FlagSyntax for the flags.
-
#flag_type ⇒ :boolean, :value
readonly
The type of flag.
-
#handler ⇒ Proc
readonly
Returns the handler for setting/updating the value.
-
#key ⇒ Symbol
readonly
Returns the key.
-
#long_desc ⇒ Array<Toys::Utils::WrappableString>
Returns the long description strings as an array.
-
#value_delim ⇒ String?
readonly
The value delimiter, which may be
""
," "
, or"="
. -
#value_label ⇒ String?
readonly
The string label for the value as it should display in help, or
nil
if the flag type is not:value
. -
#value_type ⇒ :required, ...
readonly
The type of value.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Returns true if this flag is active.
-
#double_flag_syntax ⇒ Array<FlagSyntax>
Returns an array of FlagSyntax including only double-dash flags.
-
#effective_flags ⇒ Array<String>
Returns the list of effective flags used.
-
#optparser_info ⇒ Array
Returns a list suitable for passing to OptionParser.
-
#single_flag_syntax ⇒ Array<FlagSyntax>
Returns an array of FlagSyntax including only single-dash flags.
Instance Attribute Details
#accept ⇒ Object (readonly)
Returns the acceptor, which may be nil
.
154 155 156 |
# File 'lib/toys/definition/flag.rb', line 154 def accept @accept end |
#default ⇒ Object (readonly)
Returns the default value, which may be nil
.
160 161 162 |
# File 'lib/toys/definition/flag.rb', line 160 def default @default end |
#desc ⇒ Toys::Utils::WrappableString
Returns the short description string.
166 167 168 |
# File 'lib/toys/definition/flag.rb', line 166 def desc @desc end |
#flag_syntax ⇒ Array<FlagSyntax> (readonly)
Returns an array of FlagSyntax for the flags.
148 149 150 |
# File 'lib/toys/definition/flag.rb', line 148 def flag_syntax @flag_syntax end |
#flag_type ⇒ :boolean, :value (readonly)
The type of flag. Possible values are :boolean
for a simple boolean
switch, or :value
for a flag that sets a value.
185 186 187 |
# File 'lib/toys/definition/flag.rb', line 185 def flag_type @flag_type end |
#handler ⇒ Proc (readonly)
Returns the handler for setting/updating the value.
178 179 180 |
# File 'lib/toys/definition/flag.rb', line 178 def handler @handler end |
#key ⇒ Symbol (readonly)
Returns the key.
142 143 144 |
# File 'lib/toys/definition/flag.rb', line 142 def key @key end |
#long_desc ⇒ Array<Toys::Utils::WrappableString>
Returns the long description strings as an array.
172 173 174 |
# File 'lib/toys/definition/flag.rb', line 172 def long_desc @long_desc end |
#value_delim ⇒ String? (readonly)
The value delimiter, which may be ""
, " "
, or "="
. Set to nil
if the flag type is not :value
.
206 207 208 |
# File 'lib/toys/definition/flag.rb', line 206 def value_delim @value_delim end |
#value_label ⇒ String? (readonly)
The string label for the value as it should display in help, or nil
if the flag type is not :value
.
199 200 201 |
# File 'lib/toys/definition/flag.rb', line 199 def value_label @value_label end |
#value_type ⇒ :required, ... (readonly)
The type of value. Set to :required
or :optional
if the flag type
is :value
. Otherwise set to nil
.
192 193 194 |
# File 'lib/toys/definition/flag.rb', line 192 def value_type @value_type end |
Instance Method Details
#active? ⇒ Boolean
Returns true if this flag is active. That is, it has a nonempty flags list.
245 246 247 |
# File 'lib/toys/definition/flag.rb', line 245 def active? !effective_flags.empty? end |
#double_flag_syntax ⇒ Array<FlagSyntax>
Returns an array of FlagSyntax including only double-dash flags
220 221 222 |
# File 'lib/toys/definition/flag.rb', line 220 def double_flag_syntax @double_flag_syntax ||= flag_syntax.find_all { |ss| ss.flag_style == "--" } end |
#effective_flags ⇒ Array<String>
Returns the list of effective flags used.
228 229 230 |
# File 'lib/toys/definition/flag.rb', line 228 def effective_flags @effective_flags ||= flag_syntax.map(&:flags).flatten end |
#optparser_info ⇒ Array
Returns a list suitable for passing to OptionParser.
236 237 238 |
# File 'lib/toys/definition/flag.rb', line 236 def optparser_info @optparser_info ||= flag_syntax.map(&:canonical_str) + Array(accept) end |
#single_flag_syntax ⇒ Array<FlagSyntax>
Returns an array of FlagSyntax including only single-dash flags
212 213 214 |
# File 'lib/toys/definition/flag.rb', line 212 def single_flag_syntax @single_flag_syntax ||= flag_syntax.find_all { |ss| ss.flag_style == "-" } end |