Open technicianted opened 2 years ago
I like this feature. Any updates?
I like this feature. Any updates?
You can temporarily use my fork technicianted/pflag and add the following to your go.mod:
replace github.com/spf13/pflag v1.0.5 => github.com/technicianted/pflag v1.0.6-0.20211216182845-d6307205cfab
Thanks @technicianted, have you used this in any project? Is there an example?
Thanks @technicianted, have you used this in any project? Is there an example?
If you're using spf13/cobra, you can use this usage template like this:
import (
_ "embed"
)
var (
//go:embed usage.txt
usageTemplate string
)
func init() {
myCMD.SetUsageTemplate(usageTemplate)
}
usage.txt
is in Go template format that I had adapted from the original cobra usage template. You can tweak it any way you want.
This PR adds support for flag grouping.
Group
can be set either directly onFlag
or usingSetGroup()
function. A new functionGroups()
can be used to obtain a list of defined groups.By default,
Group
is empty andFlagUsages()
will work as expected.Although this work can be done using custom usage functions, having a built-in way of doing it seems more intuitive as the package already handles bulk flag formatting using
FlagUsages()
method.With this change, things like spf13/cobra can update its template to provide consistent and backward compatible output. For example,
Flags:
output section can be replaced with:and output would look like this: