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.



729
730
731
732
733
734
735
# File 'lib/toys/flag.rb', line 729

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:



768
769
770
771
772
773
774
775
776
777
778
# File 'lib/toys/flag.rb', line 768

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)


749
750
751
# File 'lib/toys/flag.rb', line 749

def include_long?
  @include_long
end

#include_negative?Boolean

Whether to include negative long flags

Returns:

  • (Boolean)


757
758
759
# File 'lib/toys/flag.rb', line 757

def include_negative?
  @include_negative
end

#include_short?Boolean

Whether to include short flags

Returns:

  • (Boolean)


741
742
743
# File 'lib/toys/flag.rb', line 741

def include_short?
  @include_short
end