Skip to content

Commit

Permalink
feat: example builder utility (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankithans committed Aug 11, 2021
1 parent 8363053 commit 8b438c1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/utils/example_builder/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package examplebuilder

import (
"fmt"
"strings"

"github.com/spf13/cobra"
)

// NewCmdExample adds example with description to the cobra.Command
func NewCmdExample(cmd *cobra.Command, description string, flags []string) {
cmd.Example += fmt.Sprintf("\n\n# %s \n%s %s", description, cmd.CommandPath(), strings.Join(flags, ", "))
}
25 changes: 25 additions & 0 deletions docs/src/charmil_utils.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Charmil Utils
slug: /charmil_utils
---

## Example Builder
Example Builder provides Example string builder which generates full example string of the cobra command and append to existing examples.

NewCmdExample function accepts a cobra command in which example needs to be generated, description of the command and a slice of flags(if any)

```go
examplebuilder.NewCmdExample(cmd, "List all artifacts for the default artifacts group", []string{"list --page=4"})
examplebuilder.NewCmdExample(cmd, "List all artifacts", []string{"list"})
```

Output
```bash
Examples:

# List all artifacts for the default artifacts group
kafka list --page=4

# List all artifacts
kafka list
```

0 comments on commit 8b438c1

Please sign in to comment.