feat: improve docs output

License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
pull/612/head
Henrique Dias 2019-01-06 18:36:47 +00:00
parent a2d32438f1
commit ebaed3c390
1 changed files with 16 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/spf13/pflag"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -110,20 +111,29 @@ func generateMarkdown(cmd *cobra.Command, w io.Writer) {
checkErr(err) checkErr(err)
} }
func generateFlagsTable(fs *pflag.FlagSet, buf *bytes.Buffer) {
buf.WriteString("| Name | Shorthand | Usage |\n")
buf.WriteString("|------|-----------|-------|\n")
fs.VisitAll(func(f *pflag.Flag) {
buf.WriteString("|" + f.Name + "|" + f.Shorthand + "|" + f.Usage + "|\n")
})
}
func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) { func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) {
flags := cmd.NonInheritedFlags() flags := cmd.NonInheritedFlags()
flags.SetOutput(buf) flags.SetOutput(buf)
if flags.HasAvailableFlags() { if flags.HasAvailableFlags() {
buf.WriteString("## Options\n\n```\n") buf.WriteString("## Options\n\n")
flags.PrintDefaults() generateFlagsTable(flags, buf)
buf.WriteString("```\n\n") buf.WriteString("\n")
} }
parentFlags := cmd.InheritedFlags() parentFlags := cmd.InheritedFlags()
parentFlags.SetOutput(buf) parentFlags.SetOutput(buf)
if parentFlags.HasAvailableFlags() { if parentFlags.HasAvailableFlags() {
buf.WriteString("### Inherited\n\n```\n") buf.WriteString("### Inherited\n\n")
parentFlags.PrintDefaults() generateFlagsTable(parentFlags, buf)
buf.WriteString("```\n") buf.WriteString("\n")
} }
} }