I am thinking of creating a simple babashka script to generate system notifications, nothing complicated. Now I'm thinking of adding the command line completion for ease of use. After reading the README of babashka.cli do I understand correctly that I have to setup these completions for every unique cli program?
If so, I'm wondering if this autocompletion feature makes sense for infrequently used scripts. Let's say I distribute a script via bbin can I install these completions automatically somehow?
completions are "keyed" on the binary name, this is why bb now has its own completions thing. every bb.cli has a standard subcommand when you use dispatch to enable the completions
you need to source the output of that subcommand
So I would have to add it my .zshrc to have it always on. I can do that of course but for this scenario maybe a bit of an overkill and cluttering of my .zshrc
yes, .zshrc
or you can just use bb tasks + exec-fn
Btw is this a typo? https://github.com/babashka/cli#completions:~:text=help%22%5D%0A%20%20%7B-,%3Aprog%20%22myctl%22,-%3Ahelp%20true%7D)
It is the only time myctl is being used over mycli
lol, yes, typo
> or you can just use bb tasks + exec-fn Yeah for most projects I would probably do that. In this case I was thinking of distributing a script for people outside of our user group
some tools auto-install their completions
perhaps there's a non-intrusive way to do that
Yeah I really don't know if it is worth that. I just misunderstood how this thing worked. So i thought you would get it always for free, but a bit naive
like not editing a config file, but emitting some piece of config in a separate file. I wonder how other tools in brew do this
ah maybe by convention. Maybe some tools look for certain dirs and if the usr opts in you can register your completion there? I have no idea
What does brew do here?
ChatGPT says this:
Homebrew laat een package zijn completion-bestanden in conventionele directories installeren, bijvoorbeeld:
zsh: $(brew --prefix)/share/zsh/site-functions/_mycli
bash: $(brew --prefix)/etc/bash_completion.d/mycli
fish: $(brew --prefix)/share/fish/vendor_completions.d/mycli.fish
De gebruiker hoeft dus niet per programma iets aan .zshrc toe te voegen. Eén algemene configuratie zorgt dat zsh alle completion-bestanden in Homebrew's directory vindt. Tegenwoordig voegt brew shellenv die directory aan FPATH toe; daarna moet compinit worden uitgevoerd.sorry for the Dutch ;)
haha dutch is ok
ah yeah ok so it would have to be part of bbin or something similar
I guess we could enhance bbin to do this..
or maybe bbin should have its own completion directory that people add one time to their zsh config
yeah you mean having a completion directory instead of inserting it to .zshr for every command for instance? And then you need the one time config
yeah or maybe doing it like brew could be good, those seem to be the canonical places
perhaps people can add a flag in their bb.edn / deps.edn bbin settings to install the cli completions via a command which then invokes the org.babashka/completions thing
yeah probably you can reuse brew's dir instead of having to manage another dir and configure it. at least on osx brew seems to be the standard
these aren't brew specific dirs
I just wondered how brew did it, but these dirs are more general
ah but brew has the prefix brew --prefix which points to /opt/homebrew here?
But even better if there is a standard
oh yes you're right
the --prefix IS brew specific, hmmm ok
so yeah then it would make sense if bbin had this kind of directory too and people would add it to their zsh once
you can deploy your CLI through brew too btw, it's just a bit more work
I'm wondering how these homebrew completions are loaded though. Don't see it in my .zshrc
I see completion things for other tools, but I would not do this effort for basic tools
> you can deploy your CLI through brew too btw, it's just a bit more work Yeah can do that for this case at least. Probably also makes more sense for the outsiders
Ah homebrew does it via brew shellenv which i have disabled for some reason. So i guess i don't get the completions from homebrew, although for git i get the completions
Like this doesn't show me anything
print -rl -- $fpath | grep -Fx "$(brew --prefix)/share/zsh/site-functions"
Ok good to know it works like this
I also want centralized completions, so I'm very happy to see this discussed! I imagined something like
$ bb completions add mycli
to add completions and
# in ~/.zshrc
source $(bb completions print all zsh)
to activate them.
Assumption: mycli has implemented completions with baabshka/cli. Could probably be extended to something more generic by adding more explicit CLI arguments to bb completions add mycli ,,,.
———
Any script could provide this experience, really, replace bb completions with bb-completions and go.
Letting bbin manage completions is probably better, then the script author could configure completions.
{:bbin/bin {neil {:main-opts ["-f" "neil"]
:completions ,,,}}}No, I think this should live in bbin. bbin would just execute mycli org.babashka/cli completions and spit it out to the centralized location, that's it
I'm still struggling with understanding what should complete and what not. I've copied the example from the https://github.com/babashka/cli#single-word-commands and things seem to work, but no completions. Should it complete on bb -m try-cmds<TAB> for instance?
You need to install the completions via the subcommand for that script name and then call the script, not via bb -m
Completions trigger by a certain command name, like mycli
That's just how shell completion works
If you want bb <TAB to work, you need to register that thing as an :exec-fn in bb.edn tasks
here are a few tips how to get a "standalone CLI" with thanks to @lee https://github.com/babashka/cli#creating-a-standalone-cli
Ah cool. Thanks for clarifying