Skip to content

Commit 8628179

Browse files
committed
add following methods:
- Filler.FindFiller - Filler.GetChildFiller - Filler.GetNounCompleteFunc - Filler.GetNounStructField
1 parent 01aa1c1 commit 8628179

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

myflags.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,42 @@ func IsOwnAction(cmd *cobra.Command, completionCMDName, helpCMDName string, skip
806806

807807
}
808808

809+
// FindFiller return corresponding filler that handles given args
810+
func (filler *Filler) FindFiller(args []string) *Filler {
811+
cmd, _, _ := filler.Find(args)
812+
813+
path := "/" + strings.Join(strings.Fields(cmd.CommandPath())[1:], "/")
814+
815+
return filler.GetChildFiller(path)
816+
}
817+
818+
// GetChildFiller return a child Filler specified by child path,
819+
// which is a string of list of command names separated by "/", start with "/" which represents calling filler's command
820+
// e.g. /act1/act12/act121; return nil if not found or childpath is not valid
821+
func (filler *Filler) GetChildFiller(childpath string) *Filler {
822+
if childpath[0] != '/' {
823+
return nil
824+
}
825+
if childpath == "/" {
826+
return filler
827+
}
828+
curCMD := filler
829+
for _, p := range strings.FieldsFunc(childpath, func(c rune) bool { return c == '/' }) {
830+
found := false
831+
for _, child := range curCMD.fsMap {
832+
if child.Command.Name() == p {
833+
found = true
834+
curCMD = child
835+
break
836+
}
837+
}
838+
if !found {
839+
return nil
840+
}
841+
}
842+
return curCMD
843+
}
844+
809845
// GetChildCommand return a child command specified by child path,
810846
// which is a string of list of command names separated by "/", start with "/" which represents calling filler's command
811847
// e.g. /act1/act12/act121; return nil if not found or childpath is not valid

noun.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,23 @@ func rpad(s string, padding int) string {
235235
func trimRightSpace(s string) string {
236236
return strings.TrimRightFunc(s, unicode.IsSpace)
237237
}
238+
239+
// GetNounCompleteFunc returns complete function for the noun specified by the index, which start from 1;
240+
// return nil if not found
241+
func (filler *Filler) GetNounCompleteFunc(index uint) cobra.CompletionFunc {
242+
if filler.nounCompleters != nil {
243+
if f, ok := filler.nounCompleters[index]; ok {
244+
return f
245+
}
246+
}
247+
return nil
248+
}
249+
250+
// GetNounStructField returns struct field for the noun specified by the index, which start from 1;
251+
// return nil if not found
252+
func (filler *Filler) GetNounStructField(index uint) *reflect.StructField {
253+
if f, ok := filler.nounFields[index]; ok {
254+
return &f
255+
}
256+
return nil
257+
}

0 commit comments

Comments
 (0)