Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

traverse: collect context information about arguments #923

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 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,33 @@ loop:
context.Args = c.Flags().Args()
}

return traverse(subcommand(c, arg), args[i+1:])
subCmd := subcommand(c, arg)
inArgsX = append(inArgsX, inArgX{
Name: subCmd.Name(),
Description: subCmd.Short,
Type: "subcommand",
Value: arg,
})
m, _ := yaml.Marshal(inArgsX) // TODO pass on to next traverse invocation
LOG.Println(string(m))

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
Loading