tag: add passthrough for positional arguments

This new tag tells the parser to stop processing flags after the
positional argument is encountered.

This is particularly useful for subcommands that forward their arguments
to an external program, and makes it possible to implement commands
like `kubectl exec` or `docker run`.

Fixes #80.
This commit is contained in:
Franklin "Snaipe" Mathieu
2021-04-04 19:30:00 +02:00
committed by Alec Thomas
parent 49417fe966
commit d4dd709445
6 changed files with 57 additions and 12 deletions
+6
View File
@@ -34,6 +34,7 @@ type Tag struct {
Embed bool
Aliases []string
Negatable bool
Passthrough bool
// Storage for all tag keys for arbitrary lookups.
items map[string][]string
@@ -184,6 +185,11 @@ func parseTag(fv reflect.Value, ft reflect.StructField) *Tag {
t.PlaceHolder = strings.ToUpper(dashedString(fv.Type().Name()))
}
t.Enum = t.Get("enum")
passthrough := t.Has("passthrough")
if passthrough && !t.Arg {
fail("passthrough only makes sense for positional arguments")
}
t.Passthrough = passthrough
return t
}