Package: pycomplete
pycompleteA Python library to generate static completion scripts for your CLI appInstallationpycompleterequires Python 3.6 or higher, you can install it via PyPI:$pipinstallpycompleteUsageWithpycomplete, one can generate a completion script for CLI application that is compatible with a given shell.
The script outputs the result ontostdout, allowing one to re-direct the output to the file of their choosing.pycompleteaccepts different types of objects depending on which CLI framework you are using.
Forargparse,argparse.ArgumentParseris expected while forclick, eitherclick.Commandorclick.Contextis OK.pycompleteknows 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 thatpycompleteneeds 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).BASHCompletion files are commonly stored in/etc/bash_completion.d/. Run command:$pycomplete"myscript:parser"bash>/etc/bash_completion.d/_myscriptYou may have to log out and log back in to your shell session for the changes to take effect.FISHFish completion files are commonly stored in$HOME/.config/fish/completions/. Run command:$pycomplete"myscript:parser"fish>$HOME/.config/fish/completions/myscript.fishYou may have to log out and log back in to your shell session for the changes to take effect.ZSHZSH completions are commonly stored in any directory listed in your$fpathvariable. 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$HOMEdirectory$mkdir~/.zfuncThen add the following lines to your.zshrcjust beforecompinit$fpath+=~/.zfuncRun command:$pycomplete"myscript:parser"zsh>~/.zfunc/_myscriptYou must then either log out and log back in, or simply run$execzshFor the new completions to take affect.PowershellThere is no default location for completion scripts on Powershell. One may need to execute the scripts in their profile:PS>mkdir$PROFILE\..\CompletionsPS>echo@'Get-ChildItem "$PROFILE\..\Completions\" | ForEach-Object {. $_.FullName}'@|Out-File-Append-Encodingutf8$PROFILEMake sure you set the properExecution Policy:PS>Set-ExecutionPolicyUnrestricted-ScopeCurrentUserRun command to generate script:PS>pycomplete"myscript:parser"powershell|Out-File-Encodingutf8$PROFILE\..\Completions\myscript_completion.ps1You may have to log out and log back in to your shell session for the changes to take effect.CUSTOM LOCATIONSAlternatively, 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 assourceing inside your login script. Consult your shells documentation for how to
add such directives.Integrate with existing CLI appspycompletecan be also used as a Python library, allowing one to integrate with existing CLI apps.frompycompleteimportCompleterfrommypackage.cliimportparsercompleter=Completer(parser)print(completer.render())Seeexamples/folder for full examples of working apps.How does it differ fromargcomplete?argcomplete, together withclick-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,pycompleteproducesstatic and fixedscripts which contain all required information
within themselves. Plus,argcompleteandclick-completionboth 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 likehomebrew, where completion scripts are part of the package and are bundled with it.LimitationsOnly options and subcommands are autocompleted, positional arguments are not completed since user usually expects the path sugguestion to work
in this case.Supported CLI Frameworksargparse.ArgumentParserclick.Command,click.ContextMore to be added