Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Nov 27, 2022
1 parent d0d6120 commit 8118c23
Showing 1 changed file with 12 additions and 43 deletions.
55 changes: 12 additions & 43 deletions docs/src/carapace/defaultActions/actionExecute.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,31 @@
# ActionExecute

[`ActionExecute`] invokes an internal command and parses it's output using [`ActionImport`].
It does so by updating the commands args so that [Export] is called with the current context as arguments.

```go
var executeCmd = &cobra.Command{
Use: "execute",
Short: "execute example",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
rootCmd.AddCommand(executeCmd)
> Cobra commands can only be executed **once** so be sure each invocation uses a new instance.
```go
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
cmd := &cobra.Command{
Use: "embedded",
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
Run: func(cmd *cobra.Command, args []string) {},
}

cmd.Flags().Bool("test", false, "")
cmd.Flags().Bool("embedded-flag", false, "embedded flag")

carapace.Gen(cmd).PositionalCompletion(
carapace.ActionValues("one", "two"),
carapace.ActionValues("three", "four"),
carapace.ActionValues("embeddedPositional1", "embeddedP1"),
carapace.ActionValues("embeddedPositional2", "embeddedP2"),
)

carapace.Gen(executeCmd).PositionalAnyCompletion(
carapace.ActionExecute(cmd),
)
}
```
return carapace.ActionExecute(cmd)
})
````

E.g. in [`gh repo clone`] which allows additional git flags after dash.

```sh
gh repo clone [<repository>] [-- <gitflags>...] [flags]
```

Here, the context is updated so that only flags are completed and the `--branch` flag can list remote branches.
```go
carapace.Gen(repo_cloneCmd).DashAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo := ""
if args := repo_cloneCmd.Flags().Args(); len(args) > 0 {
repo = fmt.Sprintf("https://github.com/%v.git", args[0])
}
c.Args = append([]string{"clone", repo, ""}, c.Args...)
return git.ActionExecute().Invoke(c).ToA()
}),
)
```

[![asciicast](https://asciinema.org/a/468206.svg)](https://asciinema.org/a/468206)
![](./actionExecute.cast)

[`ActionExecute`]:https://pkg.go.dev/github.com/rsteube/carapace#ActionExecute
[`ActionImport`]:../action/actionImport.md
[Export]:../export.md
[`gh repo clone`]:https://github.com/rsteube/carapace-bin/blob/84717177317a9c9b1aa0d150d25d1b5c12cf9422/completers/gh_completer/cmd/repo_clone.go
[`ActionImport`]:../action/actionImport.md

0 comments on commit 8118c23

Please sign in to comment.