Class: Toys::Flag::DefaultCompletion

Inherits:
Completion::Base show all
Defined in:
lib/toys/flag.rb

Overview

A Completion that returns all possible flags associated with a Toys::Flag.

Instance Method Summary collapse

Constructor Details

#initialize(flag:, include_short: true, include_long: true, include_negative: true) ⇒ DefaultCompletion

Create a completion given configuration options.

Parameters:

  • flag (Toys::Flag)

    The flag definition.

  • include_short (Boolean) (defaults to: true)

    Whether to include short flags.

  • include_long (Boolean) (defaults to: true)

    Whether to include long flags.

  • include_negative (Boolean) (defaults to: true)

    Whether to include --no-* forms.



300
301
302
303
304
305
306
# File 'lib/toys/flag.rb', line 300

def initialize(flag:, include_short: true, include_long: true, include_negative: true)
  super()
  @flag = flag
  @include_short = include_short
  @include_long = include_long
  @include_negative = include_negative
end

Instance Method Details

#call(context) ⇒ Array<Toys::Completion::Candidate>

Returns candidates for the current completion.

Parameters:

Returns:



339
340
341
342
343
344
345
346
347
348
349
# File 'lib/toys/flag.rb', line 339

def call(context)
  results =
    if @include_short && @include_long && @include_negative
      @flag.effective_flags
    else
      collect_results
    end
  fragment = context.fragment
  results.find_all { |val| val.start_with?(fragment) }
         .map { |str| Completion::Candidate.new(str) }
end

#include_long?Boolean

Whether to include long flags

Returns:

  • (Boolean)


320
321
322
# File 'lib/toys/flag.rb', line 320

def include_long?
  @include_long
end

#include_negative?Boolean

Whether to include negative long flags

Returns:

  • (Boolean)


328
329
330
# File 'lib/toys/flag.rb', line 328

def include_negative?
  @include_negative
end

#include_short?Boolean

Whether to include short flags

Returns:

  • (Boolean)


312
313
314
# File 'lib/toys/flag.rb', line 312

def include_short?
  @include_short
end