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.



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

def initialize(flag:, include_short: true, include_long: true, include_negative: true)
  @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:



792
793
794
795
796
797
798
799
800
801
802
# File 'lib/toys/flag.rb', line 792

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)


773
774
775
# File 'lib/toys/flag.rb', line 773

def include_long?
  @include_long
end

#include_negative?Boolean

Whether to include negative long flags

Returns:

  • (Boolean)


781
782
783
# File 'lib/toys/flag.rb', line 781

def include_negative?
  @include_negative
end

#include_short?Boolean

Whether to include short flags

Returns:

  • (Boolean)


765
766
767
# File 'lib/toys/flag.rb', line 765

def include_short?
  @include_short
end