Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Nov 17, 2022
1 parent 1ddeea0 commit ab7bf88
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
13 changes: 13 additions & 0 deletions internal/pflagfork/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ func (f flag) Style() style {
return Default
}

func (f flag) Matches(arg string) bool {
switch f.Style() {
case Default:
return false
case ShorthandOnly:
return false
case NameAsShorthand:
return false
default:
return false
}
}

func Flag(f *pflag.Flag) *flag {
return &flag{Flag: f}
}
9 changes: 9 additions & 0 deletions internal/pflagfork/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ func (f flagSet) IsPosix() bool {
return true
}

func (f flagSet) LookupArg(arg string) (result *flag) {
f.IsPosix()
f.FlagSet.VisitAll(func(f *pflag.Flag) {
Flag(f).Matches(arg)
})
// TODO shorthand and variants
return
}

func FlagSet(f *pflag.FlagSet) *flagSet {
return &flagSet{FlagSet: f}
}
9 changes: 2 additions & 7 deletions traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func actionTraverse(c *cobra.Command, args []string) (Action, Context) {
// Not a subcommand and likely a flag
case strings.HasPrefix(arg, "-"):
inFlag = &InFlag{
Flag: findFlag(c, arg),
Flag: pflagfork.FlagSet(c.Flags()).LookupArg(arg).Flag,
Args: []string{},
}
inArgs = append(inArgs, arg)
Expand Down Expand Up @@ -87,9 +87,4 @@ func actionTraverse(c *cobra.Command, args []string) (Action, Context) {
func subcommand(cmd *cobra.Command, arg string) (subcommand *cobra.Command) {
subcommand, _, _ = cmd.Find([]string{arg})
return
}

func findFlag(cmd *cobra.Command, arg string) *pflag.Flag {
// TODO shorthand and variants
return cmd.Flags().Lookup(arg[1:])
}
}

0 comments on commit ab7bf88

Please sign in to comment.