class Toys::Flag::DefaultCompletion

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

Public Class Methods

new(flag:, include_short: true, include_long: true, include_negative: true) click to toggle source

Create a completion given configuration options.

@param flag [Toys::Flag] The flag definition. @param include_short [Boolean] Whether to include short flags. @param include_long [Boolean] Whether to include long flags. @param include_negative [Boolean] Whether to include `–no-*` forms.

Calls superclass method
# 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

Public Instance Methods

call(context) click to toggle source

Returns candidates for the current completion.

@param context [Toys::Completion::Context] the current completion

context including the string fragment.

@return [Array<Toys::Completion::Candidate>] an array of candidates

# 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?() click to toggle source

Whether to include long flags @return [Boolean]

# File lib/toys/flag.rb, line 749
def include_long?
  @include_long
end
include_negative?() click to toggle source

Whether to include negative long flags @return [Boolean]

# File lib/toys/flag.rb, line 757
def include_negative?
  @include_negative
end
include_short?() click to toggle source

Whether to include short flags @return [Boolean]

# File lib/toys/flag.rb, line 741
def include_short?
  @include_short
end

Private Instance Methods

collect_results() click to toggle source
# File lib/toys/flag.rb, line 782
def collect_results
  results = []
  if @include_short
    results += @flag.short_flag_syntax.map(&:positive_flag)
  end
  if @include_long
    results +=
      if @include_negative
        @flag.long_flag_syntax.flat_map(&:flags)
      else
        @flag.long_flag_syntax.map(&:positive_flag)
      end
  end
  results
end