From ebaed3c390d43341f5d5ebbce9b9202c66f337f6 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 6 Jan 2019 18:36:47 +0000 Subject: [PATCH] feat: improve docs output License: MIT Signed-off-by: Henrique Dias --- cmd/docs.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/docs.go b/cmd/docs.go index 711d25d7..0a86f5ab 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -9,6 +9,7 @@ import ( "sort" "strings" + "github.com/spf13/pflag" "github.com/spf13/cobra" ) @@ -110,20 +111,29 @@ func generateMarkdown(cmd *cobra.Command, w io.Writer) { 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) { flags := cmd.NonInheritedFlags() flags.SetOutput(buf) if flags.HasAvailableFlags() { - buf.WriteString("## Options\n\n```\n") - flags.PrintDefaults() - buf.WriteString("```\n\n") + buf.WriteString("## Options\n\n") + generateFlagsTable(flags, buf) + buf.WriteString("\n") } parentFlags := cmd.InheritedFlags() parentFlags.SetOutput(buf) if parentFlags.HasAvailableFlags() { - buf.WriteString("### Inherited\n\n```\n") - parentFlags.PrintDefaults() - buf.WriteString("```\n") + buf.WriteString("### Inherited\n\n") + generateFlagsTable(parentFlags, buf) + buf.WriteString("\n") } }