Skip to content

pyparam.completer

module

pyparam.completer

Classes for completions

The idea is inspired from https://github.com/pallets/click/pull/1622

Some of the code is borrowing there, under following LICENSE:

Copyright 2014 Pallets

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Classes
Functions
  • split_arg_string(string) (list of str) Given an argument string this attempts to split it into small parts.</>
function

pyparam.completer.split_arg_string(string)

Given an argument string this attempts to split it into small parts.

Borrowed from https://github.com/pallets/click/blob/3984f9efce5a0d15f058e1abe1ea808c6abd243a/src/click/parser.py#L106

Parameters
  • string (str) The string to be split
Returns (list of str)

List of split pieces

class

pyparam.completer.Completer()

Main completion handler

Attributes
  • comp_curr (str) The current word for completion
  • comp_prev (str) The previous word matched
  • comp_shell (str) The shell where the completion will be conducted One of ['', 'bash', 'fish', 'zsh'] Obtained from environment
  • comp_words (list of str) The words have been entered before completion
  • progvar (str) Get the program name that can be used as a variable</>
  • uid (str) Get the uid based on the raw program name
    This is used as the prefix or suffix of some shell function names</>
Methods
  • complete() (str) Yields the completions</>
  • shellcode(shell, python, module) (str) Generate the shell code to be integrated</>
method

shellcode(shell, python=None, module=False) → str

Generate the shell code to be integrated

For bash, it should be appended to ~/.profile For zsh, it should be appended to ~/.zprofile For fish, it should be appended to ~/.config/fish/completions/%(prog)s.fish If python is provided, this should go to python.fish rather than the %(prog)s.fish

Parameters
  • shell (str) The shell to generate the code for.
  • python (str, optional) The python name or path to invoke completion.
  • module (bool, optional) Whether do completion for python -m <prog>
Raises
  • ValueError if shell is not one of bash, zsh and fish
generator

complete()

Yields the completions

Yields (str)

The strings as completion candidates

class

pyparam.completer.CompleterParam()

Class for a parameter dealing with completion

Methods
  • complete_name(current) (iterator of (str, str)) Give the completion name candidates</>
  • complete_value(current, prefix) (Union(str, iterator of (str, ...))) Give the completion candidates</>
method

complete_value(current, prefix='')

Give the completion candidates

Parameters
  • current (str) Current prefix
Returns (Union(str, iterator of (str, ...)))

None when there are no candidates, nor should we have next paramters/commands as candidates (requiring a value). An empty string if we should put next parameters/commands as candidates. Otherwise yields The candidates should be either 1-, 2-, or 3-element tuple. If 1-element, type plain and no description implied. If 2-element, type plain and 2nd element should be description. If 3-element, 2nd element the type, 3rd the description.

generator

complete_name(current)

Give the completion name candidates

Parameters
  • current (str) The current prefix or word under cursor
Returns (iterator of (str, str))

An iterator of a tuple including the prefixed name and description.