Skip to content

Commit

Permalink
Merge pull request #848 from rsteube/doc-prerun
Browse files Browse the repository at this point in the history
doc: PreRun
  • Loading branch information
rsteube committed Jul 27, 2023
2 parents ba5e94e + 1948f25 commit 63bcd15
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 99 deletions.
219 changes: 121 additions & 98 deletions docs/src/carapace/gen/preInvoke.cast

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/src/carapace/gen/preInvoke.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```go
carapace.Gen(rootCmd).PreInvoke(func(cmd *cobra.Command, flag *pflag.Flag, action carapace.Action) carapace.Action {
return action.Chdir(rootCmd.Flag("C").Value.String())
return action.Chdir(rootCmd.Flag("chdir").Value.String())
})
```

Expand Down
41 changes: 41 additions & 0 deletions docs/src/carapace/gen/preRun.cast
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{"version": 2, "width": 108, "height": 24, "timestamp": 1690471863, "env": {"SHELL": "elvish", "TERM": "tmux-256color"}}
[0.082769, "o", "\u001b[?7h\u001b[7m⏎\u001b[m \r \r\u001b[?7l\u001b[?2004h"]
[0.083393, "o", "\u001b[?25l\r???> ???> \r\u001b[5C\u001b[?25h\u001b[?25l\r\u001b[5C\u001b[K\r\u001b[5C\u001b[?25h"]
[0.09568, "o", "\u001b[?25l\r\r\u001b[5C\u001b[?25h"]
[0.095794, "o", "\u001b[?25l\r\u001b[K\r\n\u001b[0;1;36mcarapace/example\u001b[0;m on \u001b[0;1;35m doc-preinvoke\u001b[0;m \u001b[0;1;31m[$!?]\u001b[0;m via \u001b[0;1;36m🐹 v1.20.6 \r\n\u001b[0;1;37mesh\u001b[0;m \u001b[0;1;32m❯\u001b[0;m \r\u001b[6C\u001b[?25h"]
[0.095847, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[6C\u001b[?25h"]
[0.412505, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[0;31me\u001b[0;m\r\u001b[7C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"]
[0.413421, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"]
[0.430073, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"]
[0.430152, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"]
[0.604681, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[7C\u001b[0;31mx\u001b[0;m\r\u001b[8C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[8C\u001b[?25h"]
[0.725813, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;32mexa\u001b[0;m\r\u001b[9C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[9C\u001b[?25h"]
[0.836668, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;31mexam\u001b[0;m\r\u001b[10C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[10C\u001b[?25h"]
[0.889595, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[10C\u001b[0;31mp\u001b[0;m\r\u001b[11C\u001b[?25h"]
[0.889894, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[11C\u001b[?25h"]
[0.891132, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[11C\u001b[?25h"]
[0.891192, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[11C\u001b[?25h"]
[1.015919, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[11C\u001b[0;31ml\u001b[0;m\r\u001b[12C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[12C\u001b[?25h"]
[1.06554, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;32mexample\u001b[0;m\r\u001b[13C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[13C\u001b[?25h"]
[1.122077, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[13C \r\u001b[14C\u001b[?25h"]
[1.295771, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[14C\u001b[0;4maction \r\n\u001b[0;1;37;45m COMPLETING argument \u001b[0;m \r\n\u001b[0;7;34maction\u001b[0;2;7m (action example) \r\n\u001b[0;34malias\u001b[0;2m (action example) \r\n\u001b[0;mchain\u001b[0;2m (shorthand chain) \r\n\u001b[0;mcompletion\u001b[0;2m (Generate the autocompletion script for the specified shell)\r\n\u001b[0;34mflag\u001b[0;2m (flag example) \r\n\u001b[0;mgroup\u001b[0;2m (group example) \r\n\u001b[0;mhelp\u001b[0;2m (Help about any command) \r\n\u001b[0;35minjection\u001b[0;2m (just trying to break things) \r\n\u001b[0;minterspersed\u001b[0;2m (interspersed example) \r\n\u001b[0;33mmodifier\u001b[0;2m (modifier example) \r\n\u001b[0;mmultiparts\u001b[0;2m (multiparts example) \r\n\u001b[0;36mplugin\u001b[0;2m (dynamic plugin command) \r\n\u001b[0;mspecial \u001b[13A\r\u001b[22C\u001b[?25h"]
[1.763322, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\u001b[22Cp\r\n\r\n\r\n\u001b[1C\u001b[Kompletion\u001b[0;2m (Generate the autocompletion script for the specified shell)\r\n\u001b[0;m\u001b[K\u001b[0;34mflag\u001b[0;2m (flag example) \r\n\u001b[0;m\u001b[Kgroup\u001b[0;2m (group example) \r\n\u001b[0;m\u001b[Khelp\u001b[0;2m (Help about any command) \r\n\u001b[0;m\u001b[Kinterspersed\u001b[0;2m (interspersed example) \r\n\u001b[0;m\u001b[K\u001b[0;33mmodifier\u001b[0;2m (modifier example) \r\n\u001b[0;m\u001b[Kmultiparts\u001b[0;2m (multiparts example) \r\n\u001b[0;m\u001b[K\u001b[0;36mplugin\u001b[0;2m (dynamic plugin command) \r\n\u001b[0;m\u001b[Kspecial \r\n\u001b[J\u001b[A\u001b[11A\r\u001b[23C\u001b[?25h"]
[1.924854, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\u001b[23Cl\r\n\r\n\r\n\r\n\r\n\r\n\u001b[Kinterspersed\u001b[0;2m (interspersed example) \r\n\u001b[0;m\u001b[K\u001b[0;33mmodifier\u001b[0;2m (modifier example) \r\n\u001b[0;m\u001b[Kmultiparts\u001b[0;2m (multiparts example) \r\n\u001b[0;m\u001b[K\u001b[0;36mplugin\u001b[0;2m (dynamic plugin command) \u001b[0;m\r\n\u001b[J\u001b[A\u001b[9A\r\u001b[24C\u001b[?25h"]
[2.034708, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[14C\u001b[K\u001b[0;4mplugin \r\n\u001b[24C\u001b[0;mu\r\n\u001b[K\u001b[0;7;36mplugin\u001b[0;2;7m (dynamic plugin command)\u001b[0;m\r\n\u001b[J\u001b[A\u001b[1A\r\u001b[25C\u001b[?25h"]
[2.034794, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\r\n\r\n\u001b[1A\r\u001b[25C\u001b[?25h"]
[2.863313, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[14C\u001b[Kplugin \r\n\u001b[J\u001b[A\r\u001b[21C\u001b[?25h"]
[3.296292, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[21Cpl\r\u001b[23C\u001b[?25h"]
[3.778875, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[21C\u001b[K\u001b[0;4mpl1 \r\n\u001b[0;1;37;45m COMPLETING argument \u001b[0;m \r\n\u001b[0;7mpl1\u001b[0;m pluginArg1\u001b[1A\r\u001b[22C\u001b[?25h"]
[4.517865, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[23C\u001b[K\u001b[0;4muginArg1 \r\n\r\n\u001b[0;m\u001b[Kpl1 \u001b[0;7mpluginArg1\u001b[0;m\u001b[1A\r\u001b[22C\u001b[?25h"]
[5.479275, "o", "\u001b[?25l\u001b[3A\r\r\n\r\n\u001b[21C\u001b[KpluginArg1 \r\n\u001b[J\u001b[A\r\u001b[32C\u001b[?25h"]
[5.479367, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[32C\u001b[?25h"]
[5.47977, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[32C\u001b[?25h"]
[6.804825, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\r\u001b[6C\u001b[?25h"]
[6.806102, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[6C\u001b[?25h"]
[6.827759, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[6C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[6C\u001b[?25h"]
[7.105936, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[0;31me\u001b[0;m\r\u001b[7C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[7C\u001b[?25h"]
[7.289151, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[7C\u001b[0;31mx\u001b[0;m\r\u001b[8C\u001b[?25h\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[8C\u001b[?25h"]
[7.387644, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[8C\u001b[0;31mi\u001b[0;m\r\u001b[9C\u001b[?25h"]
[7.387975, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\u001b[9C\u001b[?25h"]
[7.497308, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\u001b[6C\u001b[K\u001b[0;32mexit\u001b[0;m\r\u001b[10C\u001b[?25h"]
[7.598899, "o", "\u001b[?25l\u001b[2A\r\r\n\r\n\r\n\r\u001b[?25h\u001b[?7h\u001b[?2004l\r"]
23 changes: 23 additions & 0 deletions docs/src/carapace/gen/preRun.md
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# PreRun

[`PreRun`] is called before arguments are parsed for the current command and allows modification of its structure.

```go
carapace.Gen(rootCmd).PreRun(func(cmd *cobra.Command, args []string) {
pluginCmd := &cobra.Command{
Use: "plugin",
Short: "dynamic plugin command",
GroupID: "plugin",
Run: func(cmd *cobra.Command, args []string) {},
}

carapace.Gen(pluginCmd).PositionalCompletion(
carapace.ActionValues("pl1", "pluginArg1"),
)

cmd.AddCommand(pluginCmd)
})
```

![](./preRun.cast)

[`PreRun`]:https://pkg.go.dev/github.com/rsteube/carapace#Carapace.PreRun
23 changes: 23 additions & 0 deletions example/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var rootCmd = &cobra.Command{
Expand All @@ -17,6 +18,7 @@ func Execute() error {
}

func init() {
rootCmd.Flags().StringP("chdir", "C", "", "change work directory")
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().StringP("persistentFlag", "p", "", "Help message for persistentFlag")
rootCmd.PersistentFlags().String("persistentFlag2", "", "Help message for persistentFlag2")
Expand All @@ -25,6 +27,7 @@ func init() {
rootCmd.Flags().StringArrayP("array", "a", []string{}, "multiflag")

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"chdir": carapace.ActionDirectories(),
"persistentFlag": carapace.ActionValues("p1", "p2", "p3"),
"persistentFlag2": carapace.ActionValues("p4", "p5", "p6"),
})
Expand All @@ -33,5 +36,25 @@ func init() {
&cobra.Group{ID: "main", Title: "Main Commands"},
&cobra.Group{ID: "modifier", Title: "Modifier Commands"},
&cobra.Group{ID: "test", Title: "Test Commands"},
&cobra.Group{ID: "plugin", Title: "Plugin Commands"},
)

carapace.Gen(rootCmd).PreRun(func(cmd *cobra.Command, args []string) {
pluginCmd := &cobra.Command{
Use: "plugin",
Short: "dynamic plugin command",
GroupID: "plugin",
Run: func(cmd *cobra.Command, args []string) {},
}

carapace.Gen(pluginCmd).PositionalCompletion(
carapace.ActionValues("pl1", "pluginArg1"),
)

cmd.AddCommand(pluginCmd)
})

carapace.Gen(rootCmd).PreInvoke(func(cmd *cobra.Command, flag *pflag.Flag, action carapace.Action) carapace.Action {
return action.Chdir(rootCmd.Flag("chdir").Value.String())
})
}
6 changes: 6 additions & 0 deletions example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func TestRoot(t *testing.T) {
"multiparts", "multiparts example",
"special", "",
).Tag("other commands"),
carapace.ActionValuesDescribed(
"plugin", "dynamic plugin command",
).Style(style.Cyan).Tag("plugin commands"),
).ToA())

s.Run("a").
Expand All @@ -95,6 +98,8 @@ func TestRoot(t *testing.T) {

s.Run("-").
Expect(carapace.ActionStyledValuesDescribed(
"--chdir", "change work directory", style.Blue,
"-C", "change work directory", style.Blue,
"--array", "multiflag", style.Blue,
"-a", "multiflag", style.Blue,
"-h", "help for example", style.Default,
Expand All @@ -111,6 +116,7 @@ func TestRoot(t *testing.T) {
s.Run("--").
Expect(carapace.ActionStyledValuesDescribed(
"--array", "multiflag", style.Blue,
"--chdir", "change work directory", style.Blue,
"--help", "help for example", style.Default,
"--persistentFlag", "Help message for persistentFlag", style.Yellow,
"--persistentFlag2", "Help message for persistentFlag2", style.Blue,
Expand Down

0 comments on commit 63bcd15

Please sign in to comment.