-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
12 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |