Skip to content

Commit

Permalink
Merge pull request #2085 from rsteube/rename-macro-cmd
Browse files Browse the repository at this point in the history
carapace: renamed macro flag/subcommand
  • Loading branch information
rsteube authored Dec 23, 2023
2 parents 85c2c05 + 1ef32ee commit c600b9d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/carapace/cmd/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

var codegenCmd = &cobra.Command{
Use: "--scrape [spec]",
Use: "--codegen [spec]",
Short: "",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down
14 changes: 7 additions & 7 deletions cmd/carapace/cmd/macros.go → cmd/carapace/cmd/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/spf13/cobra"
)

var macrosCmd = &cobra.Command{
Use: "--macros [macro] ...",
var macroCmd = &cobra.Command{
Use: "--macro [macro] ...",
Short: "",
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -30,14 +30,14 @@ var macrosCmd = &cobra.Command{
}

func init() {
carapace.Gen(macrosCmd).Standalone()
macrosCmd.Flags().SetInterspersed(false)
carapace.Gen(macroCmd).Standalone()
macroCmd.Flags().SetInterspersed(false)

carapace.Gen(macrosCmd).PositionalCompletion(
carapace.Gen(macroCmd).PositionalCompletion(
carapace.ActionMultiPartsN("(", 2, func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return carapace.ActionExecCommand("carapace", "--macros")(func(output []byte) carapace.Action {
return carapace.ActionExecCommand("carapace", "--macro")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

vals := make([]string, 0)
Expand All @@ -59,7 +59,7 @@ func init() {
}),
)

carapace.Gen(macrosCmd).PositionalAnyCompletion(
carapace.Gen(macroCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return spec.ActionMacro("$carapace." + c.Args[0]).Shift(1) // TODO macro prefix
}),
Expand Down
4 changes: 2 additions & 2 deletions cmd/carapace/cmd/macros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

func TestMacros(t *testing.T) {
sandbox.Package(t, "github.com/rsteube/carapace-bin/cmd/carapace")(func(s *sandbox.Sandbox) {
s.Run("--macros", "color.XtermColorNames", "G").
s.Run("--macro", "color.XtermColorNames", "G").
Expect(carapace.ActionStyledValues(
"Green", "color2",
"Grey", "color8",
).Usage("--macros [macro] ..."))
).Usage("--macro [macro] ..."))
})
}
28 changes: 14 additions & 14 deletions cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// since flag parsing is disabled do this manually
switch args[0] {
case "--macros":
macrosCmd.SetArgs(args[1:])
macrosCmd.Execute()
case "--macro":
macroCmd.SetArgs(args[1:])
macroCmd.Execute()
case "-h", "--help":
cmd.Help()
case "-v", "--version":
Expand Down Expand Up @@ -238,20 +238,20 @@ func Execute(version string) error {
}

func init() {
rootCmd.Flags().Bool("codegen", false, "generate code from spec file")
rootCmd.Flags().Bool("list", false, "list completers")
rootCmd.Flags().Bool("macros", false, "list spec macros")
rootCmd.Flags().Bool("codegen", false, "generate code for spec file")
rootCmd.Flags().BoolP("help", "h", false, "help for carapace")
rootCmd.Flags().BoolP("version", "v", false, "version for carapace")
rootCmd.Flags().Bool("list", false, "list completers")
rootCmd.Flags().Bool("macro", false, "list or execute macros")
rootCmd.Flags().Bool("run", false, "run spec")
rootCmd.Flags().Bool("schema", false, "json schema for spec files")
rootCmd.Flags().Bool("style", false, "set style")
rootCmd.Flags().BoolP("version", "v", false, "version for carapace")

rootCmd.MarkFlagsMutuallyExclusive(
"codegen",
"help",
"list",
"macros",
"macro",
"run",
"schema",
"style",
Expand All @@ -275,22 +275,22 @@ func init() {
return carapace.ActionExecute(invokeCmd)
}
switch c.Args[0] {
case "--macros":
return carapace.ActionExecute(macrosCmd).Shift(1)
case "--codegen":
return carapace.ActionExecute(codegenCmd).Shift(1)
case "--help":
return carapace.ActionValues()
case "-v", "--version":
return carapace.ActionValues()
case "--list":
return carapace.ActionExecute(listCmd).Shift(1).Usage("list")
case "--macro":
return carapace.ActionExecute(macroCmd).Shift(1)
case "--run":
return carapace.ActionExecute(runCmd).Shift(1)
case "--schema":
return carapace.ActionExecute(schemaCmd).Shift(1)
case "--scrape":
return carapace.ActionExecute(codegenCmd).Shift(1)
case "--style":
return carapace.ActionExecute(styleCmd).Shift(1)
case "-v", "--version":
return carapace.ActionValues()
default:
return carapace.ActionValues()
}
Expand Down
3 changes: 2 additions & 1 deletion docs/src/release_notes/v0.29.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

![](./v0.29/banner.png)

One step further to exchange completions _loosely coupled_ between commands.
One step further to exchange completions between commands.

## Root Command

Restructured the `carapace` root command which was (and still is) a bit of a mess.
Some "flags" (subcommands) were renamed/changed for consistency.

- `--list=json` -> `--list --format json`
- `--macros` -> `--macro`
- `--scrape [spec]` -> `--codegen [spec]`

## Macro
Expand Down
6 changes: 3 additions & 3 deletions docs/src/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ commands:
Carapace provides a range of [custom macros](./spec/macros.md):
```sh
carapace --macros # list macros
carapace --macros color.HexColors # show macro details
carapace --macros color.HexColors <TAB> # test macro
carapace --macro # list macros
carapace --macro color.HexColors # show macro details
carapace --macro color.HexColors <TAB> # test macro
```

![](./spec-macros.cast)
Expand Down

0 comments on commit c600b9d

Please sign in to comment.