Class: Dry::CLI::Completion::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/cli/completion/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(registry, program_name: nil) ⇒ Generator

Returns a new instance of Generator.



11
12
13
14
# File 'lib/dry/cli/completion/generator.rb', line 11

def initialize(registry, program_name: nil)
  @registry = registry
  @program_name = program_name || Dry::CLI::ProgramName.call
end

Instance Method Details

#call(shell:, include_aliases: false, out: StringIO.new) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dry/cli/completion/generator.rb', line 16

def call(shell:, include_aliases: false, out: StringIO.new)
  raise ArgumentError, "Unknown shell" unless SUPPORTED_SHELLS.include?(shell)
  if shell == ZSH
    out.puts "# enable bash completion support, see https://github.com/dannyben/completely#completions-in-zsh"
    out.puts "autoload -Uz +X compinit && compinit"
    out.puts "autoload -Uz +X bashcompinit && bashcompinit"
  end

  out.puts Completely::Completions.new(
    Input.new(@registry, @program_name).call(include_aliases: include_aliases)
  ).script
  out.string
end