Class: Dry::CLI::Autocomplete::Command

Inherits:
Command
  • Object
show all
Defined in:
lib/dry/cli/autocomplete/command.rb

Overview

The command a host registers to expose mycli completion <shell>.

This file defines the command class and nothing else. It loads no spec builder and no emitter, and it must stay that way: a host pays for whatever this require pulls on every invocation, while the command itself runs about once per shell. The generator is required inside #call, where the cost is actually incurred. See SPECIFICATION.md §1.3 and §2.4, and the spec that pins it.

Constant Summary collapse

SHELLS =
%w[bash zsh].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.program_nameObject (readonly)

Returns the value of attribute program_name.



33
34
35
# File 'lib/dry/cli/autocomplete/command.rb', line 33

def program_name
  @program_name
end

.registryObject (readonly)

Returns the value of attribute registry.



33
34
35
# File 'lib/dry/cli/autocomplete/command.rb', line 33

def registry
  @registry
end

Class Method Details

.[](registry, program_name: nil) ⇒ Object

Binds the command to a registry, and optionally to the name the host is installed as. Without one, the program name is taken from $PROGRAM_NAME at call time rather than at registration, since a gem may be required long before anyone knows how it was invoked.

register "completion", Dry::CLI::Autocomplete::Command[MyCLI]


25
26
27
28
29
30
# File 'lib/dry/cli/autocomplete/command.rb', line 25

def self.[](registry, program_name: nil)
  Class.new(self) do
    @registry = registry
    @program_name = program_name
  end
end

Instance Method Details

#call(shell:) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/dry/cli/autocomplete/command.rb', line 46

def call(shell:, **)
  require_relative "spec_builder"
  require_relative "emitters/#{shell}"

  spec = SpecBuilder.call(registry, program_name: program_name)
  out.puts emitter_for(shell).call(spec)
end