diff --git a/carapace.go b/carapace.go index 12a360930..2a65b3613 100644 --- a/carapace.go +++ b/carapace.go @@ -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 diff --git a/docs/src/carapace/gen.md b/docs/src/carapace/gen.md index d8db40f14..bcb50d8e7 100644 --- a/docs/src/carapace/gen.md +++ b/docs/src/carapace/gen.md @@ -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. diff --git a/docs/src/carapace/gen/hiddenSubcommand.md b/docs/src/carapace/gen/hiddenSubcommand.md index 0191517b3..753c3fa86 100644 --- a/docs/src/carapace/gen/hiddenSubcommand.md +++ b/docs/src/carapace/gen/hiddenSubcommand.md @@ -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 diff --git a/example/cmd/root.go b/example/cmd/root.go index d4ddd12c1..3bbe35719 100644 --- a/example/cmd/root.go +++ b/example/cmd/root.go @@ -25,6 +25,4 @@ func init() { carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{ "persistentFlag": carapace.ActionValues("p1", "p2", "p3"), }) - - carapace.Gen(rootCmd).Root() }