I was reading the clang source code and happened upon the command-line autocompletion support that was implemented a few years ago. This blog post by its implementor gives a good introduction to the functionality.
Put simply, clang gained the ability to perform autocompletion on arguments passed to it by the shell, and the existing bash-autocompletion script was updated to delegate its completion to clang itself. Handling autocompletion in clang itself is advantageous as it has much more information about the arguments such as their type and the context in which they should be used.
I was wondering if the community sees any value in similar functionality being added to GOptionContext.
I am envisioning an enhancement to meson’s gnome module which could be used to generate and install a skeleton shell completion script for a given target to complete any functionality added to GLib.
Would this kind of functionality be useful to anybody?
Many thanks for all your hard work on GNOME over the years. I have benefited greatly from your work and I hope to contribute back what I can, if you find it useful.
Yes, this would be useful. If you’d like to implement it and submit a merge request, that would be fantastic. The contribution guidelines are worth a read; in particular please note that all new features need to come with unit tests — but it’s probably a good idea to put together a rough implementation of the API and functionality first and get that reviewed before adding the unit tests.
I would expect the API would end up looking something like:
Add a g_option_context_set_completion_enabled() method (like g_option_context_set_help_enabled()). For backwards compatibility this would unfortunately have to default to off (as projects may already be using a --completion option). Projects would use this to opt-in to completion support.
Provide a way for third party code to provide its own completion functions for GOptionContexts which use custom parsing (such as G_OPTION_ARG_CALLBACK, g_option_context_set_ignore_unknown_options() or G_OPTION_REMAINING). That’s probably the crux of the API design for this feature.
Thank you for your feedback and for the extra context you have provided. I will work on this and push up an MR when it’s getting ready. Concurrently I will prototype the meson side of things on meson upstream.
If anybody knows of any other implementations of this kind of auto-complete in a command line interface I would interested to hear about it, particularly those using GLib. Clang’s implementation is the only prior art I had in mind but there’s bound to be loads more.