Skip to content

Commit

Permalink
shell completion --format: add help to function with args
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Luap99 authored and cdoern committed May 27, 2022
1 parent f8157de commit 754bad3
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"reflect"
"strconv"
"strings"

"github.com/containers/common/libnetwork/types"
Expand Down Expand Up @@ -1145,6 +1146,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
Expand Down

0 comments on commit 754bad3

Please sign in to comment.