Giter Club home page Giter Club logo

pycomplete's Introduction

pycomplete

A Python library to generate static completion scripts for your CLI app

Tests PyPI PyPI - Python Version Supported Shells - bash|zsh|fish|powershell

Installation

pycomplete requires Python 3.6 or higher, you can install it via PyPI:

$ pip install pycomplete

Usage

With pycomplete, one can generate a completion script for CLI application that is compatible with a given shell. The script outputs the result onto stdout, allowing one to re-direct the output to the file of their choosing.

pycomplete accepts different types of objects depending on which CLI framework you are using. For argparse, argparse.ArgumentParser is expected while for click, either click.Command or click.Context is OK. pycomplete knows what to do smartly.

Where you place the file will depend on which shell, and which operating system you are using. Your particular configuration may also determine where these scripts need to be placed.

Note that pycomplete needs to be installed in the same environment as the target CLI app to work properly.

Here are some common set ups for the three supported shells under Unix and similar operating systems (such as GNU/Linux).

BASH

Completion files are commonly stored in /etc/bash_completion.d/. Run command:

$ pycomplete "myscript:parser" bash > /etc/bash_completion.d/_myscript

You may have to log out and log back in to your shell session for the changes to take effect.

FISH

Fish completion files are commonly stored in$HOME/.config/fish/completions/. Run command:

$ pycomplete "myscript:parser" fish > $HOME/.config/fish/completions/myscript.fish

You may have to log out and log back in to your shell session for the changes to take effect.

ZSH

ZSH completions are commonly stored in any directory listed in your $fpath variable. To use these completions, you must either add the generated script to one of those directories, or add your own to this list.

Adding a custom directory is often the safest best if you're unsure of which directory to use. First create the directory, for this example we'll create a hidden directory inside our $HOME directory

$ mkdir ~/.zfunc

Then add the following lines to your .zshrc just before compinit

$ fpath+=~/.zfunc

Run command:

$ pycomplete "myscript:parser" zsh > ~/.zfunc/_myscript

You must then either log out and log back in, or simply run

$ exec zsh

For the new completions to take affect.

Powershell

There is no default location for completion scripts on Powershell. One may need to execute the scripts in their profile:

PS > mkdir $PROFILE\..\Completions
PS > echo @'
Get-ChildItem "$PROFILE\..\Completions\" | ForEach-Object {
    . $_.FullName
}
'@ | Out-File -Append -Encoding utf8 $PROFILE

Make sure you set the proper Execution Policy:

PS > Set-ExecutionPolicy Unrestricted -Scope CurrentUser

Run command to generate script:

PS > pycomplete "myscript:parser" powershell | Out-File -Encoding utf8 $PROFILE\..\Completions\myscript_completion.ps1

You may have to log out and log back in to your shell session for the changes to take effect.

CUSTOM LOCATIONS

Alternatively, you could save these files to the place of your choosing, such as a custom directory inside your $HOME. Doing so will require you to add the proper directives, such as sourceing inside your login script. Consult your shells documentation for how to add such directives.

Integrate with existing CLI apps

pycomplete can be also used as a Python library, allowing one to integrate with existing CLI apps.

from pycomplete import Completer
from mypackage.cli import parser

completer = Completer(parser)
print(completer.render())

See examples/ folder for full examples of working apps.

How does it differ from argcomplete?

argcomplete, together with click-completion, can also generate scripts for shell completion. However, they work in a different way that commands and options are retrieved on the fly when they are requested by a matching token. This brings a performance shrinkage when it is expensive to import the CLI app. In the other side, pycomplete produces static and fixed scripts which contain all required information within themselves. Plus, argcomplete and click-completion both work for specific framework. One may notice the disadvantage of static completion is also obvious -- users must regenerate the script when the commands and/or options are updated. Fortunately, it shouldn't be a problem in most package managers like homebrew, where completion scripts are part of the package and are bundled with it.

Limitations

Only options and subcommands are autocompleted, positional arguments are not completed since user usually expects the path sugguestion to work in this case.

Supported CLI Frameworks

  • argparse.ArgumentParser
  • click.Command, click.Context
  • More to be added

pycomplete's People

Contributors

frostming avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pycomplete's Issues

pycomplete==0.3.1 fails to build due to invalid pyproject.toml

When running pip install --no-binary :all: pycomplete==0.3.1, it fails due to pdm not finding a project section in the pyproject.toml:

#  pip install --no-binary :all: pycomplete==0.3.1
Collecting pycomplete==0.3.1
  Downloading pycomplete-0.3.1.tar.gz (12 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... error
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python /usr/local/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /tmp/tmp9irzz09f
         cwd: /tmp/pip-install-98nsfzk2/pycomplete_11b6d2ce9c5a47eaa52971e042fae911
    Complete output (25 lines):
    Traceback (most recent call last):
      File "/tmp/pip-build-env-05xf7dm2/overlay/lib/python3.9/site-packages/pdm/pep517/metadata.py", line 58, in __init__
        self._metadata = self._read_pyproject(self.filepath)
      File "/tmp/pip-build-env-05xf7dm2/overlay/lib/python3.9/site-packages/pdm/pep517/metadata.py", line 65, in _read_pyproject
        return data["project"]
    KeyError: 'project'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/usr/local/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 280, in <module>
        main()
      File "/usr/local/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 263, in main
        json_out['return_val'] = hook(**hook_input['kwargs'])
      File "/usr/local/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py", line 133, in prepare_metadata_for_build_wheel
        return hook(metadata_directory, config_settings)
      File "/tmp/pip-build-env-05xf7dm2/overlay/lib/python3.9/site-packages/pdm/pep517/api.py", line 29, in prepare_metadata_for_build_wheel
        dist_info = Path(metadata_directory, builder.dist_info_name)
      File "/tmp/pip-build-env-05xf7dm2/overlay/lib/python3.9/site-packages/pdm/pep517/wheel.py", line 104, in dist_info_name
        name = to_filename(self.meta.project_name)
      File "/tmp/pip-build-env-05xf7dm2/overlay/lib/python3.9/site-packages/pdm/pep517/base.py", line 119, in meta
        self._meta = Metadata(self.location / "pyproject.toml")
      File "/tmp/pip-build-env-05xf7dm2/overlay/lib/python3.9/site-packages/pdm/pep517/metadata.py", line 60, in __init__
        raise ProjectError("The project's pyproject.toml is not valid.")
    pdm.pep517.metadata.ProjectError: The project's pyproject.toml is not valid.
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python /usr/local/lib/python3.9/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /tmp/tmp9irzz09f Check the logs for full command output.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.