func (cmd *Command) PrintHelp(w io.Writer) { wrap := wrapper.New() wrap.FirstIndent = " " wrap.DefaultIndent = wrap.FirstIndent // Command name with brief and synopsis pathStr := strings.Join(cmd.path, " ") fmt.Fprintf(w, "\n%s - %s\n\n", pathStr, cmd.Brief) fmt.Fprintf(w, "Usage: %s %s\n\n", pathStr, cmd.Synopsis) // Description descr := strings.Join(wrap.Wrap(cmd.Description), "\n") fmt.Fprintf(w, "Description:\n%s\n", descr) // Options hdrPrint := false tw := tabwriter.NewWriter(os.Stdout, 8, 0, 2, ' ', tabwriter.AlignRight) cmd.Flags.VisitAll(func(flag *flag.Flag) { if !hdrPrint { fmt.Fprintf(w, "\nOptions:\n") hdrPrint = true } def := "" if len(flag.DefValue) > 0 { def = fmt.Sprintf("(default %q)", flag.DefValue) } fmt.Fprintf(tw, "-%s\t%s%s\t\n", flag.Name, flag.Usage, def) }) tw.Flush() }
func (grp *Group) PrintHelp(w io.Writer) { wrap := wrapper.New() wrap.FirstIndent = " " wrap.DefaultIndent = wrap.FirstIndent // Brief, with an optional path to the group name if len(grp.path) > 0 { fmt.Fprintf(w, "\n%s - %s\n\n", strings.Join(grp.path, " "), grp.Brief) } else { fmt.Fprintf(w, "\n%s\n\n", grp.Brief) } // Description descr := strings.Join(wrap.Wrap(grp.Description), "\n") fmt.Fprintf(w, "Description:\n%s\n\n", descr) // Print available subgroups fmt.Fprintf(w, "Available subgroups:\n") for k, v := range grp.subgroup { fmt.Fprintf(w, " %-14s %s\n", k, v.Summary()) } }