Towards a more flexible help output #139
Replies: 4 comments 4 replies
-
(moved the issue to Discussions, which I had forgotten to enable) Kingpin uses the text/template approach, and this is actually also why Kong explicitly has a public abstract data model - so that it could be used for such a purpose. In fact, Kingpin uses this system for writing completion templates, among other things. It doesn't have one yet because I wanted to keep Kong's core lean while I was building it out, but now that it is mostly stable it could be a good time to add it. If you'd like to take a crack at this, it would be most welcome. A couple of things to think about:
|
Beta Was this translation helpful? Give feedback.
-
A possible interface would be to provide a function something like this: // TemplatedHelpPrinter uses text/template to provide help formatting.
//
// The root of the template context is kong.Context.
func TemplatedHelpPrinter(template string, extraCtx interface{}, funcs template.FuncMap) HelpPrinter You might also want to use functional options for this instead of the above, just to allow future expansion if necessary. |
Beta Was this translation helpful? Give feedback.
-
I've had a bit of a crack at this and it reminded me why I didn't replicate template-based help originally. It's really not trivial to make templates + template functions that support wrapping, columns, and so on. I think perhaps a better approach might be to just expose more of the help-based helper functions that already exist as private functions in Kong. |
Beta Was this translation helpful? Give feedback.
-
I'm leaning towards making no significant change here beyond maybe just exposing some helper functions to make writing help a bit simpler for users. Thoughts? |
Beta Was this translation helpful? Give feedback.
-
Not an issue per se, but I'm opening a discussion to gather ideas on how we could work towards a more flexible help output from Kong.
While Kong's default help output is elegant and fits most use cases, there's a need for more customization. The most recent example being
HelpOptions.FlagsLast
. Besides minor (but increasing maintenance burden) tweaks like this one, I can think of other changes which would be quite disruptive:hg --help
An app is free to use
Help(HelpFunc)
to fully customize the output, but that means forgoing all the heavy lifting the default help printer provides.Let's explore some possibilities.
Internationalization
Enabling i18n in Kong would be useful to cater for an international audience, but it should also enable overriding the default English strings such as:
Commands:
Run "app <command> --help" for more information on a command.
Show context-sensitive help.
I don't have much experience in this area in Go, but it looks like the standard
x/text/message
package provides the tooling for this.Template
A great way to provide a lot of flexibility would be to use templates instead of hard-coding the help structure in the help printer. This is also the solution Cobra opted for by offering a
SetHelpTemplate()
API.Go has a native text/template package for this use case.
Discussing a potential template structure could be its own issue. But here are a few examples enabled by templating:
writeTwoColumns()
function could be exposed as acolumns
template function cutting the input text at a particular delimiter (\t
being a good candidate) and ignoring empty lines:
suffixes, etc.)Different levels of details
Sometimes we need more compact help output:
-h
versus--help
UsageOnError
could benefit from displaying much less contentUsing templates would help with this, by providing one template per details level, without modifying the printer logic.
Shipped as another package?
If these new dependencies are unwanted for the basic config, all of this could be shipped as a "plug-in" package which would expose a
HelpFunc
implementation to replace the default one.Beta Was this translation helpful? Give feedback.
All reactions