Skip to content

Commit

Permalink
Add new command to list flags for documentation
Browse files Browse the repository at this point in the history
Signed-off-by: Omer Aplatony <[email protected]>
  • Loading branch information
omerap12 committed Jul 5, 2024
1 parent 5e8ef90 commit 03d8450
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
48 changes: 48 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var generateReadmeFlags = map[string]bool{
"print": true,
}

func newListFlagsCommand(rootCmd *cobra.Command) *cobra.Command {
return &cobra.Command{
Use: "list-flags",
Short: "Lists all flags for documentation",
RunE: func(cmd *cobra.Command, args []string) error {
printCommandFlags(rootCmd, "")
return nil
},
}
}

func printCommandFlags(cmd *cobra.Command, indent string) {
_, ok := generateReadmeFlags[cmd.Use]
if ok {
fmt.Printf("%s### `%s` command\n\n", indent, cmd.Use)

fmt.Printf("%s| Flag | Default Value | Required | Description |\n", indent)
fmt.Printf("%s| ----------------------------- | ------------------------ | -------- | ------------------------------------------------------------ |\n", indent)

cmd.Flags().VisitAll(func(flag *pflag.Flag) {
fmt.Printf("%s| `%-27s` | %-24s | %-8v | %s |\n",
indent,
flag.Name,
flag.DefValue,
flag.NoOptDefVal == "",
flag.Usage,
)
})
fmt.Println()
}

for _, subCmd := range cmd.Commands() {
printCommandFlags(subCmd, indent+" ")
}
}
3 changes: 1 addition & 2 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ func newPrintCommand() *cobra.Command {
`If present, the namespace scope for this CLI request.`)

cmd.Flags().BoolVarP(&pr.allNamespaces, "all-namespaces", "A", false,
`If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even
if specified with --namespace.`)
`If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.`)

cmd.Flags().StringSliceVar(&pr.providers, "providers", []string{},
fmt.Sprintf("If present, the tool will try to convert only resources related to the specified providers, supported values are %v.", i2gw.GetSupportedProviders()))
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func getKubeconfig() {
func Execute() {
rootCmd := newRootCmd()
rootCmd.AddCommand(newPrintCommand())
rootCmd.AddCommand(newListFlagsCommand(rootCmd))
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand Down

0 comments on commit 03d8450

Please sign in to comment.