class Toys::Completion::Context
The context in which to determine completion candidates.
Attributes
The CLI
being run. @return [Toys::CLI]
The current string fragment to complete @return [String]
A non-completed prefix for the current fragment. @return [String]
All previous words. @return [Array<String>]
Public Class Methods
Create a completion context
@param cli [Toys::CLI] The CLI
being run. Required. @param previous_words
[Array<String>] Array of complete strings that
appeared prior to the fragment to complete.
@param fragment_prefix
[String] A prefix in the fragment that does not
participate in completion. (e.g. "key=")
@param fragment [String] The string fragment to complete. @param params [Hash] Miscellaneous context data
# File lib/toys/completion.rb, line 30 def initialize(cli:, previous_words: [], fragment_prefix: "", fragment: "", **params) @cli = cli @previous_words = previous_words @fragment_prefix = fragment_prefix @fragment = fragment extra_params = { cli: cli, previous_words: previous_words, fragment_prefix: fragment_prefix, fragment: fragment } @params = params.merge(extra_params) @tool = nil @args = nil @arg_parser = nil end
Public Instance Methods
Get data for arbitrary key. @param [Symbol] key @return [Object]
# File lib/toys/completion.rb, line 84 def [](key) @params[key] end
Current ArgParser
indicating the status of argument parsing up to this point.
@return [Toys::ArgParser]
# File lib/toys/completion.rb, line 114 def arg_parser lookup_tool @arg_parser ||= ArgParser.new(@cli, @tool).parse(@args) end
An array of complete arguments passed to the tool, prior to the fragment to complete. @return [Array<String>]
# File lib/toys/completion.rb, line 103 def args lookup_tool @args end
@private
# File lib/toys/completion.rb, line 120 def inspect "<Toys::Completion::Context previous=#{previous_words.inspect}" \ " prefix=#{fragment_prefix.inspect} fragment=#{fragment.inspect}>" end
The tool being invoked, which should control the completion. @return [Toys::ToolDefinition]
# File lib/toys/completion.rb, line 93 def tool lookup_tool @tool end
Create a new completion context with the given modifications.
@param delta_params [Hash] Replace context data. @return [Toys::Completion::Context]
# File lib/toys/completion.rb, line 51 def with(**delta_params) Context.new(**@params.merge(delta_params)) end
Private Instance Methods
# File lib/toys/completion.rb, line 127 def lookup_tool @tool, @args = @cli.loader.lookup(@previous_words) unless @tool end