Skip to content

Commit

Permalink
traverse: collect context information about arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Oct 1, 2023
1 parent 0e0f241 commit 4cdd9c0
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ import (
"github.com/rsteube/carapace/internal/pflagfork"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

type inArgsX []inArgX
type inArgX struct {
Value string `yaml:",omitempty"`
Name string `yaml:",omitempty"`
Description string `yaml:",omitempty"`
Type string `yaml:",omitempty"`
OptArg *inArgX `yaml:",omitempty"`
}

func traverse(c *cobra.Command, args []string) (Action, Context) {
LOG.Printf("traverse called for %#v with args %#v\n", c.Name(), args)
storage.preRun(c, args)
Expand All @@ -19,6 +29,7 @@ func traverse(c *cobra.Command, args []string) (Action, Context) {
c.FParseErrWhitelist.UnknownFlags = true
}

inArgsX := make(inArgsX, 0)
inArgs := []string{} // args consumed by current command
inPositionals := []string{} // positionals consumed by current command
var inFlag *pflagfork.Flag // last encountered flag that still expects arguments
Expand All @@ -32,6 +43,10 @@ loop:
// flag argument
case inFlag != nil && inFlag.Consumes(arg):
LOG.Printf("arg %#v is a flag argument\n", arg)
inArgsX = append(inArgsX, inArgX{
Value: arg,
Type: "flag argument",
})
inArgs = append(inArgs, arg)
inFlag.Args = append(inFlag.Args, arg)

Expand All @@ -43,6 +58,10 @@ loop:
// dash
case arg == "--":
LOG.Printf("arg %#v is dash\n", arg)
inArgsX = append(inArgsX, inArgX{
Type: "dash",
Value: arg,
})
inArgs = append(inArgs, context.Args[i:]...)
break loop

Expand All @@ -54,6 +73,17 @@ loop:

if inFlag == nil {
LOG.Printf("flag %#v is unknown", arg)
inArgsX = append(inArgsX, inArgX{
Type: "unknown flag",
Value: arg,
})
} else {
inArgsX = append(inArgsX, inArgX{
Name: inFlag.Name,
Description: inFlag.Usage,
Type: "flag", // TODO flagtype
Value: arg,
})
}
continue

Expand All @@ -73,16 +103,30 @@ loop:
context.Args = c.Flags().Args()
}

return traverse(subcommand(c, arg), args[i+1:])
subCmd := subcommand(c, arg)
inArgsX = append(inArgsX, inArgX{

Check failure on line 107 in traverse.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to inArgsX (ineffassign)
Name: subCmd.Name(),
Description: subCmd.Short,
Type: "subcommand",
Value: arg,
})
return traverse(subCmd, args[i+1:])

// positional
default:
LOG.Printf("arg %#v is a positional\n", arg)
inArgsX = append(inArgsX, inArgX{
Type: "positional argument",
Value: arg,
})
inArgs = append(inArgs, arg)
inPositionals = append(inPositionals, arg)
}
}

m, _ := yaml.Marshal(inArgsX)
LOG.Println(string(m))

toParse := inArgs
if inFlag != nil && len(inFlag.Args) == 0 && inFlag.Consumes("") {
LOG.Printf("removing arg %#v since it is a flag missing its argument\n", toParse[len(toParse)-1])
Expand Down

0 comments on commit 4cdd9c0

Please sign in to comment.