From 3b0844f990c6b7de708eeec1f8dfdc7e7d4611bd Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 16 May 2022 21:01:52 +0200 Subject: shell completion --format: add help to function with args From a template users POV it is not importent when they use a struct field or method. They only notice the difference when the function requires arguments. So lets be nice and let the user know that this method requires arguments via the help text. This is how it now looks like when the completion descriptions are enabled on bash: ``` $ bin/podman ps --format {{.Created.A {{.Created.AddDate (This is a function and requires 3 arguments) {{.Created.After (This is a function and requires 1 argument) {{.Created.Add (This is a function and requires 1 argument) {{.Created.AppendFormat (This is a function and requires 2 arguments) ``` Signed-off-by: Paul Holzinger --- cmd/podman/common/completion.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'cmd/podman/common/completion.go') diff --git a/cmd/podman/common/completion.go b/cmd/podman/common/completion.go index 32dd896c0..ce26c1678 100644 --- a/cmd/podman/common/completion.go +++ b/cmd/podman/common/completion.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "reflect" + "strconv" "strings" "github.com/containers/common/libnetwork/types" @@ -1142,6 +1143,20 @@ func getMethodNames(f reflect.Value, prefix string) []formatSuggestion { if kind == reflect.Struct || kind == reflect.Map { suffix = "." } + // From a template users POV it is not importent when the use a struct field or method. + // They only notice the difference when the function requires arguments. + // So lets be nice and let the user know that this method requires arguments via the help text. + // Note since this is actually a method on a type the first argument is always fix so we should skip it. + num := method.Func.Type().NumIn() - 1 + if num > 0 { + // everything after tab will the completion scripts show as help when enabled + // overwrite the suffix because it expects the args + suffix = "\tThis is a function and requires " + strconv.Itoa(num) + " argument" + if num > 1 { + // add plural s + suffix += "s" + } + } fname := method.Name if strings.HasPrefix(fname, prefix) { // add method name with closing braces -- cgit v1.2.3-54-g00ecf