Class: Toys::Flag

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/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.

Defined Under Namespace

Classes: DefaultCompletion, Resolution, Syntax

Constant Summary collapse

SET_HANDLER =

The set handler replaces the previous value.

Returns:

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

The push handler pushes the given value using the << operator.

Returns:

  • (Proc)
->(val, prev) { prev.nil? ? [val] : prev << val }
DEFAULT_HANDLER =

The default handler is the set handler, replacing the previous value.

Returns:

  • (Proc)
SET_HANDLER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#acceptorToys::Acceptor::Base (readonly)

Returns the effective acceptor.



127
128
129
# File 'lib/toys/flag.rb', line 127

def acceptor
  @acceptor
end

#defaultObject (readonly)

Returns the default value, which may be nil.

Returns:

  • (Object)


133
134
135
# File 'lib/toys/flag.rb', line 133

def default
  @default
end

#descToys::WrappableString

The short description string.

When reading, this is always returned as a WrappableString.

When setting, the description may be provided as any of the following:

  • A WrappableString.
  • A normal String, which will be transformed into a WrappableString using spaces as word delimiters.
  • An Array of String, which will be transformed into a WrappableString where each array element represents an individual word for wrapping.


150
151
152
# File 'lib/toys/flag.rb', line 150

def desc
  @desc
end

#display_nameString (readonly)

The display name of this flag.

Returns:

  • (String)


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

def display_name
  @display_name
end

#flag_completionProc, Toys::Completion::Base (readonly)

The proc that determines shell completions for the flag.

Returns:



181
182
183
# File 'lib/toys/flag.rb', line 181

def flag_completion
  @flag_completion
end

#flag_syntaxArray<Toys::Flag::Syntax> (readonly)

Returns an array of Flag::Syntax for the flags.

Returns:



121
122
123
# File 'lib/toys/flag.rb', line 121

def flag_syntax
  @flag_syntax
end

#flag_type:boolean, :value (readonly)

The type of flag.

Returns:

  • (:boolean)

    if the flag is a simple boolean switch

  • (:value)

    if the flag sets a value



195
196
197
# File 'lib/toys/flag.rb', line 195

def flag_type
  @flag_type
end

#groupToys::FlagGroup (readonly)

Returns the flag group containing this flag

Returns:



109
110
111
# File 'lib/toys/flag.rb', line 109

def group
  @group
end

#handlerProc (readonly)

The handler for setting/updating the value.

Returns:

  • (Proc)


175
176
177
# File 'lib/toys/flag.rb', line 175

def handler
  @handler
end

#keySymbol (readonly)

Returns the key.

Returns:

  • (Symbol)


115
116
117
# File 'lib/toys/flag.rb', line 115

def key
  @key
end

#long_descArray<Toys::WrappableString>

The long description strings.

When reading, this is returned as an Array of WrappableString representing the lines in the description.

When setting, the description must be provided as an Array where each element may be any of the following:

  • A WrappableString representing one line.
  • A normal String representing a line. This will be transformed into a WrappableString using spaces as word delimiters.
  • An Array of String representing a line. This will be transformed into a WrappableString where each array element represents an individual word for wrapping.

Returns:



169
170
171
# File 'lib/toys/flag.rb', line 169

def long_desc
  @long_desc
end

#sort_strString (readonly)

A string that can be used to sort this flag

Returns:

  • (String)


233
234
235
# File 'lib/toys/flag.rb', line 233

def sort_str
  @sort_str
end

#value_completionProc, Toys::Completion::Base (readonly)

The proc that determines shell completions for the value.

Returns:



187
188
189
# File 'lib/toys/flag.rb', line 187

def value_completion
  @value_completion
end

#value_delimString? (readonly)

The value delimiter, which may be "", " ", or "=".

Returns:

  • (String)

    The delimiter

  • (nil)

    if the flag type is not :value.



221
222
223
# File 'lib/toys/flag.rb', line 221

def value_delim
  @value_delim
end

#value_labelString? (readonly)

The string label for the value as it should display in help.

Returns:

  • (String)

    The label

  • (nil)

    if the flag type is not :value.



213
214
215
# File 'lib/toys/flag.rb', line 213

def value_label
  @value_label
end

#value_type:required, ... (readonly)

The type of value.

Returns:

  • (:required)

    if the flag type is :value and the value is required.

  • (:optional)

    if the flag type is :value and the value is optional.

  • (nil)

    if the flag type is not :value.



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

def value_type
  @value_type
end

Class Method Details

.create(key, flags = [], used_flags: nil, report_collisions: true, accept: nil, handler: nil, default: nil, complete_flags: nil, complete_values: nil, display_name: nil, desc: nil, long_desc: nil, group: nil) ⇒ Object

Create a flag definition.

Parameters:

  • key (String, Symbol)

    The key to use to retrieve the value from the execution context.

  • flags (Array<String>) (defaults to: [])

    The flags in OptionParser format. If empty, a flag will be inferred from the key.

  • accept (Object) (defaults to: nil)

    An acceptor that validates and/or converts the value. See Acceptor.create for recognized formats. Optional. If not specified, defaults to Acceptor::DEFAULT.

  • default (Object) (defaults to: nil)

    The default value. This is the value that will be set in the context if this flag is not provided on the command line. Defaults to nil.

  • handler (Proc, nil, :set, :push) (defaults to: nil)

    An optional handler for setting/updating the value. A handler is a proc taking two arguments, the given value and the previous value, returning the new value that should be set. You may also specify a predefined named handler. The :set handler (the default) replaces the previous value (effectively -> (val, _prev) { val }). The :push handler expects the previous value to be an array and pushes the given value onto it; it should be combined with setting default: [] and is intended for "multi-valued" flags.

  • complete_flags (Object) (defaults to: nil)

    A specifier for shell tab completion for flag names associated with this flag. By default, a DefaultCompletion is used, which provides the flag's names as completion candidates. To customize completion, set this to a hash of options to pass to the constructor for DefaultCompletion, or pass any other spec recognized by Completion.create.

  • complete_values (Object) (defaults to: nil)

    A specifier for shell tab completion for flag values associated with this flag. Pass any spec recognized by Completion.create.

  • report_collisions (Boolean) (defaults to: true)

    Raise an exception if a flag is requested that is already in use or marked as disabled. Default is true.

  • group (Toys::FlagGroup) (defaults to: nil)

    Group containing this flag.

  • desc (String, Array<String>, Toys::WrappableString) (defaults to: nil)

    Short description for the flag. See ToolDefinition#desc for a description of allowed formats. Defaults to the empty string.

  • long_desc (Array<String,Array<String>,Toys::WrappableString>) (defaults to: nil)

    Long description for the flag. See ToolDefinition#long_desc for a description of allowed formats. Defaults to the empty array.

  • display_name (String) (defaults to: nil)

    A display name for this flag, used in help text and error messages.

  • used_flags (Array<String>) (defaults to: nil)

    An array of flags already in use.



97
98
99
100
101
102
103
# File 'lib/toys/flag.rb', line 97

def self.create(key, flags = [],
                used_flags: nil, report_collisions: true, accept: nil, handler: nil,
                default: nil, complete_flags: nil, complete_values: nil, display_name: nil,
                desc: nil, long_desc: nil, group: nil)
  new(key, flags, used_flags, report_collisions, accept, handler, default, complete_flags,
      complete_values, desc, long_desc, display_name, group)
end

Instance Method Details

#active?Boolean

Whether this flag is active--that is, it has a nonempty flags list.

Returns:

  • (Boolean)


297
298
299
# File 'lib/toys/flag.rb', line 297

def active?
  !effective_flags.empty?
end

#append_long_desc(long_desc) ⇒ self

Append long description strings.

You must pass an array of lines in the long description. See #long_desc for details on how each line may be represented.

Parameters:

Returns:

  • (self)


332
333
334
335
# File 'lib/toys/flag.rb', line 332

def append_long_desc(long_desc)
  @long_desc.concat(WrappableString.make_array(long_desc))
  self
end

#canonical_syntax_stringsArray<String>

A list of canonical flag syntax strings.

Returns:

  • (Array<String>)


288
289
290
# File 'lib/toys/flag.rb', line 288

def canonical_syntax_strings
  @canonical_syntax_strings ||= flag_syntax.map(&:canonical_str)
end

#effective_flagsArray<String>

The list of all effective flags used.

Returns:

  • (Array<String>)


255
256
257
# File 'lib/toys/flag.rb', line 255

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

#long_flag_syntaxArray<Flag::Syntax>

An array of Flag::Syntax including only long (double-dash) flags.

Returns:



247
248
249
# File 'lib/toys/flag.rb', line 247

def long_flag_syntax
  @long_flag_syntax ||= flag_syntax.find_all { |ss| ss.flag_style == :long }
end

#resolve(str) ⇒ Toys::Flag::Resolution

Look up the flag by string. Returns an object that indicates whether the given string matched this flag, whether the match was unique, and other pertinent information.

Parameters:

  • str (String)

    Flag string to look up

Returns:



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/toys/flag.rb', line 267

def resolve(str)
  resolution = Resolution.new(str)
  flag_syntax.each do |fs|
    if fs.positive_flag == str
      resolution.add!(self, fs, false, true)
    elsif fs.negative_flag == str
      resolution.add!(self, fs, true, true)
    elsif fs.positive_flag.start_with?(str)
      resolution.add!(self, fs, false, false)
    elsif fs.negative_flag.to_s.start_with?(str)
      resolution.add!(self, fs, true, false)
    end
  end
  resolution
end

#short_flag_syntaxArray<Flag::Syntax>

An array of Flag::Syntax including only short (single dash) flags.

Returns:



239
240
241
# File 'lib/toys/flag.rb', line 239

def short_flag_syntax
  @short_flag_syntax ||= flag_syntax.find_all { |ss| ss.flag_style == :short }
end