Skip to content

Commit

Permalink
removed Root() function
Browse files Browse the repository at this point in the history
- instead just add the `_carapace` subcommand to any command where Gen
is invoked
  • Loading branch information
rsteube committed Apr 17, 2021
1 parent d151174 commit 6c7e8f5
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 11 deletions.
7 changes: 1 addition & 6 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,12 @@ type Carapace struct {

// Gen initialized Carapace for given command
func Gen(cmd *cobra.Command) *Carapace {
addCompletionCommand(cmd)
return &Carapace{
cmd: cmd,
}
}

// Root marks the command as root and adds the hidden completion command (`_carapace`)
func (c Carapace) Root() {
// there is no PreExecC hook in cobra so this needs to be done explicitly
addCompletionCommand(c.cmd)
}

// PositionalCompletion defines completion for positional arguments using a list of Actions
func (c Carapace) PositionalCompletion(action ...Action) {
storage.get(c.cmd).positional = action
Expand Down
4 changes: 2 additions & 2 deletions docs/src/carapace/gen.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Gen

Calling [`Root()`](https://pkg.go.dev/github.com/rsteube/carapace#Carapace.Root) on the root command is sufficient to enable completion script generation using the [hidden subcommand](./gen/hiddenSubcommand.md).
Calling [`Gen`](https://pkg.go.dev/github.com/rsteube/carapace#Gen) on the root command is sufficient to enable completion script generation using the [hidden subcommand](./gen/hiddenSubcommand.md).

```go
import (
"github.com/rsteube/carapace"
)

carapace.Gen(myCmd).Root()
carapace.Gen(rootCmd)
```

Additionally invoke [`carapace.Test`](https://pkg.go.dev/github.com/rsteube/carapace#Test) in a [test](https://golang.org/doc/tutorial/add-a-test) to verify configuration during build time.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/carapace/gen/hiddenSubcommand.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Hidden Subcommand

When [`Root()`](https://pkg.go.dev/github.com/rsteube/carapace#Carapace.Root) is invoked a hidden command (`_carapace`) is added. This handles completion script generation and [callbacks](../action/actionCallback.md).
When [`Gen`](https://github.com/rsteube/carapace/carapace.go#Gen) is invoked a hidden subcommand (`_carapace`) is added. This handles completion script generation and [callbacks](../action/actionCallback.md).


## Completion
Expand Down
2 changes: 0 additions & 2 deletions example/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@ func init() {
carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"persistentFlag": carapace.ActionValues("p1", "p2", "p3"),
})

carapace.Gen(rootCmd).Root()
}

0 comments on commit 6c7e8f5

Please sign in to comment.