Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 19, 2023
1 parent 5b6d0ce commit 7c700b3
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ package cmd
import (
"fmt"
"os"
"strings"
"time"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/cmd/carapace/cmd/action"
"github.com/rsteube/carapace-bin/cmd/carapace/cmd/completers"
"github.com/rsteube/carapace-bin/cmd/carapace/cmd/lazyinit"
"github.com/rsteube/carapace-bin/cmd/carapace/cmd/shim"
Expand Down Expand Up @@ -52,32 +54,33 @@ var rootCmd = &cobra.Command{
Config is written to [%v/carapace].
Specs are loaded from [%v/carapace/specs].
`, suppressErr(xdg.UserCacheDir), suppressErr(xdg.UserConfigDir), suppressErr(xdg.UserConfigDir)),
Args: cobra.ArbitraryArgs,
Args: cobra.MinimumNArgs(1),
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
// since flag parsing is disabled do this manually
// TODO switch to cobra flag parsing with interspersed=false (redirect completion from completers/carapace_completer to here)
switch { // TODO use flags
case cmd.Flag("macros").Changed:
macrosCmd.SetArgs(args)
switch args[0] { // TODO use flags
case "--macros":
macrosCmd.SetArgs(args[1:])
macrosCmd.Execute()
case cmd.Flag("help").Changed:
case "-h", "--help":
cmd.Help()
case cmd.Flag("version").Changed:
case "-v", "--version":
println(cmd.Version)
case cmd.Flag("list").Changed:
listCmd.SetArgs(args)
case "--list":
listCmd.SetArgs(args[1:])
listCmd.Execute()
case cmd.Flag("run").Changed:
runCmd.SetArgs(args)
case "--run":
runCmd.SetArgs(args[1:])
runCmd.Execute()
case cmd.Flag("schema").Changed:
schemaCmd.SetArgs(args)
case "--schema":
schemaCmd.SetArgs(args[1:])
schemaCmd.Execute()
case cmd.Flag("scrape").Changed:
scrapeCmd.SetArgs(args)
case "--scrape":
scrapeCmd.SetArgs(args[1:])
scrapeCmd.Execute()
case cmd.Flag("style").Changed:
styleCmd.SetArgs(args)
case "--style":
styleCmd.SetArgs(args[1:])
styleCmd.Execute()
default:
invokeCmd.SetArgs(args)
Expand Down Expand Up @@ -239,8 +242,6 @@ func Execute(version string) error {
}

func init() {
rootCmd.Flags().SetInterspersed(false)

rootCmd.Flags().BoolP("help", "h", false, "help for carapace")
rootCmd.Flags().Bool("list", false, "list completers")
rootCmd.Flags().Bool("macros", false, "list spec macros")
Expand Down Expand Up @@ -268,26 +269,34 @@ func init() {
// })

carapace.Gen(rootCmd).PositionalCompletion(
action.ActionCompleters(),
)

carapace.Gen(rootCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
switch {
case rootCmd.Flag("macros").Changed:
if !strings.HasPrefix(c.Value, "-") {
return carapace.ActionExecute(invokeCmd)
}

switch c.Args[0] {
case "--macros":
return carapace.ActionExecute(macrosCmd)
case rootCmd.Flag("help").Changed:
case "--help":
return carapace.ActionValues()
case rootCmd.Flag("version").Changed:
case "-v", "--version":
return carapace.ActionValues()
case rootCmd.Flag("list").Changed:
case "--list":
return carapace.ActionExecute(listCmd)
case rootCmd.Flag("run").Changed:
case "--run":
return carapace.ActionExecute(runCmd)
case rootCmd.Flag("schema").Changed:
case "--schema":
return carapace.ActionExecute(schemaCmd)
case rootCmd.Flag("scrape").Changed:
case "--scrape":
return carapace.ActionExecute(scrapeCmd)
case rootCmd.Flag("style").Changed:
case "--style":
return carapace.ActionExecute(styleCmd)
default:
return carapace.ActionExecute(invokeCmd)
return carapace.ActionValues()
}
}),
)
Expand Down

0 comments on commit 7c700b3

Please sign in to comment.