Class: Toys::Definition::Flag

Inherits:
Object
  • Object
show all
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.

Returns:

  • (Proc)
->(val, _prev) { val }

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#acceptObject (readonly)

Returns the acceptor, which may be nil.

Returns:

  • (Object)


154
155
156
# File 'lib/toys/definition/flag.rb', line 154

def accept
  @accept
end

#defaultObject (readonly)

Returns the default value, which may be nil.

Returns:

  • (Object)


160
161
162
# File 'lib/toys/definition/flag.rb', line 160

def default
  @default
end

#descToys::Utils::WrappableString

Returns the short description string.



166
167
168
# File 'lib/toys/definition/flag.rb', line 166

def desc
  @desc
end

#flag_syntaxArray<FlagSyntax> (readonly)

Returns an array of FlagSyntax for the flags.

Returns:



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.

Returns:

  • (:boolean, :value)


185
186
187
# File 'lib/toys/definition/flag.rb', line 185

def flag_type
  @flag_type
end

#handlerProc (readonly)

Returns the handler for setting/updating the value.

Returns:

  • (Proc)


178
179
180
# File 'lib/toys/definition/flag.rb', line 178

def handler
  @handler
end

#keySymbol (readonly)

Returns the key.

Returns:

  • (Symbol)


142
143
144
# File 'lib/toys/definition/flag.rb', line 142

def key
  @key
end

#long_descArray<Toys::Utils::WrappableString>

Returns the long description strings as an array.

Returns:



172
173
174
# File 'lib/toys/definition/flag.rb', line 172

def long_desc
  @long_desc
end

#value_delimString? (readonly)

The value delimiter, which may be "", " ", or "=". Set to nil if the flag type is not :value.

Returns:

  • (String, nil)


206
207
208
# File 'lib/toys/definition/flag.rb', line 206

def value_delim
  @value_delim
end

#value_labelString? (readonly)

The string label for the value as it should display in help, or nil if the flag type is not :value.

Returns:

  • (String, nil)


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.

Returns:

  • (:required, :optional, 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.

Returns:

  • (Boolean)


245
246
247
# File 'lib/toys/definition/flag.rb', line 245

def active?
  !effective_flags.empty?
end

#double_flag_syntaxArray<FlagSyntax>

Returns an array of FlagSyntax including only double-dash flags

Returns:



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_flagsArray<String>

Returns the list of effective flags used.

Returns:

  • (Array<String>)


228
229
230
# File 'lib/toys/definition/flag.rb', line 228

def effective_flags
  @effective_flags ||= flag_syntax.map(&:flags).flatten
end

#optparser_infoArray

Returns a list suitable for passing to OptionParser.

Returns:

  • (Array)


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_syntaxArray<FlagSyntax>

Returns an array of FlagSyntax including only single-dash flags

Returns:



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