mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Add Global Normalization Function
This commit is contained in:
parent
bba56042cf
commit
72d5846913
2 changed files with 52 additions and 0 deletions
|
@ -4,9 +4,12 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
var _ = fmt.Println
|
||||
|
@ -886,3 +889,28 @@ func TestPeristentPreRunPropagation(t *testing.T) {
|
|||
t.Error("RootCmd PersistentPreRun not called but should have been")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlobalNormFuncPropagation(t *testing.T) {
|
||||
normFunc := func(f *pflag.FlagSet, name string) pflag.NormalizedName {
|
||||
return pflag.NormalizedName(name)
|
||||
}
|
||||
|
||||
rootCmd := initialize()
|
||||
rootCmd.SetGlobalNormalizationFunc(normFunc)
|
||||
if reflect.ValueOf(normFunc) != reflect.ValueOf(rootCmd.GlobalNormalizationFunc()) {
|
||||
t.Error("rootCmd seems to have a wrong normalization function")
|
||||
}
|
||||
|
||||
// First add the cmdEchoSub to cmdPrint
|
||||
cmdPrint.AddCommand(cmdEchoSub)
|
||||
if cmdPrint.GlobalNormalizationFunc() != nil && cmdEchoSub.GlobalNormalizationFunc() != nil {
|
||||
t.Error("cmdPrint and cmdEchoSub should had no normalization functions")
|
||||
}
|
||||
|
||||
// Now add cmdPrint to rootCmd
|
||||
rootCmd.AddCommand(cmdPrint)
|
||||
if reflect.ValueOf(cmdPrint.GlobalNormalizationFunc()).Pointer() != reflect.ValueOf(rootCmd.GlobalNormalizationFunc()).Pointer() ||
|
||||
reflect.ValueOf(cmdEchoSub.GlobalNormalizationFunc()).Pointer() != reflect.ValueOf(rootCmd.GlobalNormalizationFunc()).Pointer() {
|
||||
t.Error("cmdPrint and cmdEchoSub should had the normalization function of rootCmd")
|
||||
}
|
||||
}
|
||||
|
|
24
command.go
24
command.go
|
@ -93,6 +93,8 @@ type Command struct {
|
|||
helpFunc func(*Command, []string) // Help can be defined by application
|
||||
helpCommand *Command // The help command
|
||||
helpFlagVal bool
|
||||
// The global normalization function that we can use on every pFlag set and children commands
|
||||
globNormFunc func(f *flag.FlagSet, name string) flag.NormalizedName
|
||||
}
|
||||
|
||||
// os.Args[1:] by default, if desired, can be overridden
|
||||
|
@ -151,6 +153,19 @@ func (c *Command) SetHelpTemplate(s string) {
|
|||
c.helpTemplate = s
|
||||
}
|
||||
|
||||
// SetGlobalNormalizationFunc sets a normalization function to all flag sets and also to child commands.
|
||||
// The user should not have a cyclic dependency on commands.
|
||||
func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
|
||||
c.Flags().SetNormalizeFunc(n)
|
||||
c.PersistentFlags().SetNormalizeFunc(n)
|
||||
c.LocalFlags().SetNormalizeFunc(n)
|
||||
c.globNormFunc = n
|
||||
|
||||
for _, command := range c.commands {
|
||||
command.SetGlobalNormalizationFunc(n)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Command) UsageFunc() (f func(*Command) error) {
|
||||
if c.usageFunc != nil {
|
||||
return c.usageFunc
|
||||
|
@ -606,6 +621,10 @@ func (c *Command) AddCommand(cmds ...*Command) {
|
|||
if nameLen > c.commandsMaxNameLen {
|
||||
c.commandsMaxNameLen = nameLen
|
||||
}
|
||||
// If glabal normalization function exists, update all children
|
||||
if c.globNormFunc != nil {
|
||||
x.SetGlobalNormalizationFunc(c.globNormFunc)
|
||||
}
|
||||
c.commands = append(c.commands, x)
|
||||
}
|
||||
}
|
||||
|
@ -830,6 +849,11 @@ func (c *Command) HasParent() bool {
|
|||
return c.parent != nil
|
||||
}
|
||||
|
||||
// GlobalNormalizationFunc returns the global normalization function or nil if doesn't exists
|
||||
func (c *Command) GlobalNormalizationFunc() func(f *flag.FlagSet, name string) flag.NormalizedName {
|
||||
return c.globNormFunc
|
||||
}
|
||||
|
||||
// Get the complete FlagSet that applies to this command (local and persistent declared here and by all parents)
|
||||
func (c *Command) Flags() *flag.FlagSet {
|
||||
if c.flags == nil {
|
||||
|
|
Loading…
Reference in a new issue