本文整理汇总了Golang中github.com/spf13/cobra.Command.HasParent方法的典型用法代码示例。如果您正苦于以下问题:Golang Command.HasParent方法的具体用法?Golang Command.HasParent怎么用?Golang Command.HasParent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/spf13/cobra.Command
的用法示例。
在下文中一共展示了Command.HasParent方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Namespace
// Namespace enables namespacing for a sub-commmand and its immediated children. It returns an error if the command does not have a parent.
func (n *CobraNamespace) Namespace(cmd *cobra.Command) error {
if !cmd.HasParent() {
return errors.New("cmdns: command requires a parent")
}
// Do not bind if there are not available sub commands
if !cmd.HasAvailableSubCommands() {
return nil
}
if n.OverrideUsageFunc {
cmd.SetUsageFunc(n.UsageFunc())
}
for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() {
continue
}
// copy the command add it to the root command with a prefix of its parent.
nc := *c
nc.Use = cmd.Name() + DefaultNamespaceSeparator + c.Use
// add this command to the root and hide it so it does not show in available commands list
c.Parent().Parent().AddCommand(&nc)
c.Hidden = true
n.commands = append(n.commands, &nc)
}
n.cmd = cmd
return nil
}
示例2: rootCmd
func (t *templater) rootCmd(c *cobra.Command) *cobra.Command {
if c != nil && !c.HasParent() {
return c
}
if t.RootCmd == nil {
panic("nil root cmd")
}
return t.RootCmd
}
示例3: GenMarkdownCustom
func GenMarkdownCustom(cmd *cobra.Command, out io.Writer, linkHandler func(string) string) {
name := cmd.CommandPath()
short := cmd.Short
long := cmd.Long
if len(long) == 0 {
long = short
}
fmt.Fprintf(out, "## %s\n\n", name)
fmt.Fprintf(out, "%s\n\n", short)
fmt.Fprintf(out, "### Synopsis\n\n")
fmt.Fprintf(out, "\n%s\n\n", long)
if cmd.Runnable() {
fmt.Fprintf(out, "```\n%s\n```\n\n", cmd.UseLine())
}
if len(cmd.Example) > 0 {
fmt.Fprintf(out, "### Examples\n\n")
fmt.Fprintf(out, "```\n%s\n```\n\n", cmd.Example)
}
printOptions(out, cmd, name)
if hasSeeAlso(cmd) {
fmt.Fprintf(out, "### SEE ALSO\n")
if cmd.HasParent() {
parent := cmd.Parent()
pname := parent.CommandPath()
link := pname + ".md"
link = strings.Replace(link, " ", "_", -1)
fmt.Fprintf(out, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short)
cmd.VisitParents(func(c *cobra.Command) {
if c.DisableAutoGenTag {
cmd.DisableAutoGenTag = c.DisableAutoGenTag
}
})
}
children := cmd.Commands()
sort.Sort(byName(children))
for _, child := range children {
if !child.IsAvailableCommand() || child.IsHelpCommand() {
continue
}
cname := name + " " + child.Name()
link := cname + ".md"
link = strings.Replace(link, " ", "_", -1)
fmt.Fprintf(out, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short)
}
fmt.Fprintf(out, "\n")
}
if !cmd.DisableAutoGenTag {
fmt.Fprintf(out, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006"))
}
}
示例4: findChildOfRootCommand
func findChildOfRootCommand(command *cobra.Command) (*cobra.Command, bool) {
for command.HasParent() {
if !command.Parent().HasParent() {
return command, true
}
command = command.Parent()
}
return nil, false
}
示例5: hasSeeAlso
// Test to see if we have a reason to print See Also information in docs
// Basically this is a test for a parent commend or a subcommand which is
// both not deprecated and not the autogenerated help command.
func hasSeeAlso(cmd *cobra.Command) bool {
if cmd.HasParent() {
return true
}
for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() || c.IsHelpCommand() {
continue
}
return true
}
return false
}
示例6: globalFlags
func (c *CLI) globalFlags(cmd *cobra.Command) *flag.FlagSet {
fs := &flag.FlagSet{}
if cmd.HasParent() {
fs.AddFlagSet(cmd.InheritedFlags())
if fs.Lookup("help") == nil && cmd.Flag("help") != nil {
fs.AddFlag(cmd.Flag("help"))
}
} else {
fs.AddFlagSet(cmd.PersistentFlags())
}
return c.sansDriverFlags(c.sansAdditionalFlags(fs))
}
示例7: genMan
func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
// something like `rootcmd subcmd1 subcmd2`
commandName := cmd.CommandPath()
// something like `rootcmd-subcmd1-subcmd2`
dashCommandName := strings.Replace(commandName, " ", "-", -1)
fillHeader(header, commandName)
buf := new(bytes.Buffer)
short := cmd.Short
long := cmd.Long
if len(long) == 0 {
long = short
}
manPreamble(buf, header, commandName, short, long)
manPrintOptions(buf, cmd)
if len(cmd.Example) > 0 {
fmt.Fprintf(buf, "# EXAMPLE\n")
fmt.Fprintf(buf, "```\n%s\n```\n", cmd.Example)
}
if hasSeeAlso(cmd) {
fmt.Fprintf(buf, "# SEE ALSO\n")
seealsos := make([]string, 0)
if cmd.HasParent() {
parentPath := cmd.Parent().CommandPath()
dashParentPath := strings.Replace(parentPath, " ", "-", -1)
seealso := fmt.Sprintf("**%s(%s)**", dashParentPath, header.Section)
seealsos = append(seealsos, seealso)
cmd.VisitParents(func(c *cobra.Command) {
if c.DisableAutoGenTag {
cmd.DisableAutoGenTag = c.DisableAutoGenTag
}
})
}
children := cmd.Commands()
sort.Sort(byName(children))
for _, c := range children {
if !c.IsAvailableCommand() || c.IsHelpCommand() {
continue
}
seealso := fmt.Sprintf("**%s-%s(%s)**", dashCommandName, c.Name(), header.Section)
seealsos = append(seealsos, seealso)
}
fmt.Fprintf(buf, "%s\n", strings.Join(seealsos, ", "))
}
if !cmd.DisableAutoGenTag {
fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006"))
}
return buf.Bytes()
}
示例8: commands
func commands(cmd *cobra.Command) []*cobra.Command {
if cmd.HasParent() {
return cmd.Commands()
}
cArr := []*cobra.Command{}
for _, c := range cmd.Commands() {
if m, _ := rx.MatchString("((re)?start)|stop|status|((un)?install)", c.Name()); !m {
cArr = append(cArr, c)
}
}
return cArr
}
示例9: localFlags
func localFlags(cmd *cobra.Command) *flag.FlagSet {
fs := &flag.FlagSet{}
if cmd.HasParent() {
cmd.LocalFlags().VisitAll(func(f *flag.Flag) {
if f.Name != "help" {
fs.AddFlag(f)
}
})
} else {
cmd.LocalFlags().VisitAll(func(f *flag.Flag) {
if cmd.PersistentFlags().Lookup(f.Name) == nil {
fs.AddFlag(f)
}
})
}
return sansAdditionalFlags(fs)
}
示例10: GenerateSingle
func GenerateSingle(cmd *cobra.Command, out *bytes.Buffer, linkHandler func(string) string, specs []string, render_dir string) {
name := cmd.CommandPath()
short := cmd.Short
long := cmd.Long
if len(long) == 0 {
long = short
}
fmt.Fprintf(out, "# %s\n\n", name)
fmt.Fprintf(out, "%s\n\n", short)
fmt.Fprintf(out, "## Synopsis\n")
fmt.Fprintf(out, "\n%s\n\n", long)
if cmd.Runnable() {
fmt.Fprintf(out, "```bash\n%s\n```\n\n", cmd.UseLine())
}
if len(cmd.Example) > 0 {
fmt.Fprintf(out, "## Examples\n\n")
fmt.Fprintf(out, "```bash\n%s\n```\n\n", cmd.Example)
}
flags := cmd.NonInheritedFlags()
flags.SetOutput(out)
if flags.HasFlags() {
fmt.Fprintf(out, "## Options\n\n```\n")
flags.PrintDefaults()
fmt.Fprintf(out, "```\n\n")
}
parentFlags := cmd.InheritedFlags()
parentFlags.SetOutput(out)
if parentFlags.HasFlags() {
fmt.Fprintf(out, "## Options inherited from parent commands\n\n```\n")
parentFlags.PrintDefaults()
fmt.Fprintf(out, "```\n\n")
}
if len(cmd.Commands()) > 0 {
fmt.Fprintf(out, "## Subcommands\n\n")
children := cmd.Commands()
sort.Sort(byName(children))
for _, child := range children {
if len(child.Deprecated) > 0 {
continue
}
cname := name + " " + child.Name()
link := cname + ".md"
link = strings.Replace(link, " ", "_", -1)
fmt.Fprintf(out, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short)
}
}
if len(cmd.Commands()) > 0 && cmd.HasParent() {
fmt.Fprintf(out, "\n")
}
if cmd.HasParent() {
fmt.Fprintf(out, "## See Also\n\n")
parent := cmd.Parent()
pname := parent.CommandPath()
link := pname + ".md"
link = strings.Replace(link, " ", "_", -1)
fmt.Fprintf(out, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short)
}
fmt.Fprintf(out, "\n## Specifications\n\n")
for _, spec := range specs {
spec = strings.Replace(spec, render_dir, "", 1)
title := strings.Replace(spec, "_", " ", -1)
title = strings.Replace(title, ".md", "", 1)
// title = strings.Replace(title, "spec", "specification", 1)
title = strings.Title(title)
fmt.Fprintf(out, "* [%s](%s)\n", title, linkHandler(spec))
}
fmt.Fprintf(out, "\n")
}