Crates.io | gen-completions |
lib.rs | gen-completions |
version | 0.6.0 |
source | src |
created_at | 2023-12-25 00:33:06.884673 |
updated_at | 2024-03-20 00:14:19.014093 |
description | Generate Bash/Zsh completions from manpages or JSON/KDL files |
homepage | https://github.com/ysthakur/gen-completions |
repository | https://github.com/ysthakur/gen-completions |
max_upload_size | |
id | 1079974 |
size | 760,540 |
[!warning] This project is a work in progress so it's extremely unstable and mostly broken.
This is a crate for parsing manpages to generate shell completions either by parsing manpages or from KDL/JSON files. There's both a library and a binary, and if you're looking for documentation on the library, see https://docs.rs/gen-completions/. But you're probably here for the binary, and if you want information on that, read on.
Currently, it generates Bash, Zsh, and Nushell completions, although I've only tested out Zsh and Nushell properly. If you're using another shell, it also generates Carapace specs. In addition to that, it generates KDL and JSON files so you can process the command information to generate completions yourself or something else.
The manpage parsing has been mainly ported from Fish's completions script, although this crate doesn't yet support every kind of manpage that the Fish script supports. In particular, MacOS man pages cannot yet be parsed. Any help with that would be greatly appreciated.
cargo install gen-completions
github:ysthakur/gen-completions
nix shell github:ysthakur/gen-completions
git clone git@github.com:ysthakur/gen-completions.git
)cd gen-completions && cargo build --release
You can periodically run gen-completions
to generate completions for any commands you want.
For example, if you have a directory ~/generated-completions
for all your generated
completions, and you want to generate Zsh completions from the ncdu
manpage, you can use:
gen-completions man zsh ~/generated-completions --cmds="ncdu"
If you have a config file to generate completions from, you can use:
gen-completions for zsh ncdu-completions.kdl ~/generated-completions
The CLI uses env_logger
as the backend for logging,
so to configure that, set the RUST_LOG
environment variable (the link has instructions).
See below for specific flags and whatnot.
Usage: gen-completions man [OPTIONS] <SHELL> <PATH>
Arguments:
<SHELL>
Shell(s) to generate completions for
Possible values:
- zsh: Generate completions for Zsh
- bash: Generate completions for Bash
- nu: Generate completions for Nushell
- kdl: Output parsed options as KDL
- json: Output parsed options as JSON
- carapace: Output Carapace spec
<PATH>
Directory to output completions to
Options:
-d, --dirs <PATH,...>
Directories to search for man pages in, e.g. `--dirs=/usr/share/man/man1,/usr/share/man/man6` Note that `--dirs` will search directly inside the given directories, not inside `<dir>/man1`, `<dir>/man2`, etc. If you want to search for man pages in a specific set of directories, set `$MANPATH` before running this command
-c, --cmds <REGEX>
Commands to generate completions for. If omitted, generates completions for all found commands. To match the whole name, use "^...$"
-C, --exclude-cmds <REGEX>
Commands to exclude (regex). To match the whole name, use "^...$"
--not-subcmds <COMMAND-NAME,...>
Commands that should not be treated as subcommands, to help deal with false positives when detecting subcommands
--subcmds <man-page=sub cmd,...>
Explicitly list which man pages are for which subcommands. e.g. `git-commit=git commit,foobar=foo bar`
-h, --help
Print help (see a summary with '-h')
Usage: gen-completions for <SHELL> <CONF> [OUT]
Arguments:
<SHELL>
Shell(s) to generate completions for
Possible values:
- zsh: Generate completions for Zsh
- bash: Generate completions for Bash
- nu: Generate completions for Nushell
- kdl: Output parsed options as KDL
- json: Output parsed options as JSON
- carapace: Output Carapace spec
<CONF>
File to generate completions from
[OUT]
File to generate completions to. Outputted to stdout if not given
Options:
-h, --help
Print help (see a summary with '-h')
You can either generate completions to a directory that's already in $fpath
, where
Zsh looks for functions, or you can make a new directory. If you choose to do the latter,
make sure to add it to your $fpath
in your ~/.zshrc
:
fpath=(path/to/my/directory $fpath)
[!note]
fpath
must be updated beforecompinit
is called.
After this, if your chosen directory is ~/generated-completions
, you can run
gen-completions man zsh ~/generated-completions --cmds="^ncdu"
and when you try ncdu <TAB>
, you should see completions for all of ncdu's flags.
TODO
TODO
Any and all contributions are welcome. See CONTRIBUTING.md for information on what needs to be worked on.
For examples of the kinds of files this generates, look at the
expected
folder inside the tests
folder.
For some example man pages, look at the samples
folder.
It has very basic subcommand detection. If a manpage is named git-commit-tree
,
it will look for the text git commit tree
, git-commit tree
, and git commit-tree
in
the file. When it finds the text git commit-tree
in the man page, it will
assume that commit-tree
is a subcommand of git
. I'm not sure how the Fish
script generates subcommands--I've been too lazy to do anything but skim over it--but
I will eventually get around to porting Fish's subcommand detection.