package pflagfork
import (
"fmt"
"reflect"
"strings"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
type mode int
const (
Default mode = iota
ShorthandOnly
NameAsShorthand
)
type Flag struct {
*pflag .Flag
ArgPrefix string
FlagPrefix rune
Args []string
}
func (f Flag ) Nargs () int {
if field := reflect .ValueOf (f .Flag ).Elem ().FieldByName ("Nargs" ); field .IsValid () && field .Kind () == reflect .Int {
return int (field .Int ())
}
return 0
}
const (
argStyleNext uint = 1 << iota
argStyleDelimited
argStyleAttached
)
func (f Flag ) AcceptsNext () bool {
return f .ArgumentStyle () == 0 || f .ArgumentStyle ()&argStyleNext != 0
}
func (f Flag ) AcceptsDelimited () bool {
return f .ArgumentStyle () == 0 || f .ArgumentStyle ()&argStyleDelimited != 0
}
func (f Flag ) AcceptsAttached () bool {
return f .ArgumentStyle () == 0 || f .ArgumentStyle ()&argStyleAttached != 0
}
func (f Flag ) OptargDelimiter () rune {
if field := reflect .ValueOf (f .Flag ).Elem ().FieldByName ("OptargDelimiter" ); field .IsValid () && field .Kind () == reflect .Int32 {
return (rune (field .Int ()))
}
return '='
}
func (f Flag ) DelimiterDisabled () bool {
return f .OptargDelimiter () < 0x20
}
func (f Flag ) ArgumentStyle () uint {
if field := reflect .ValueOf (f .Flag ).Elem ().FieldByName ("ArgumentStyle" ); field .IsValid () && field .Kind () == reflect .Uint {
return uint (field .Uint ())
}
return 0
}
func (f Flag ) GetMode () mode {
if field := reflect .ValueOf (f .Flag ).Elem ().FieldByName ("Mode" ); field .IsValid () && field .Kind () == reflect .Int {
return mode (field .Int ())
}
return Default
}
func (f Flag ) IsRepeatable () bool {
if strings .Contains (f .Value .Type (), "Slice" ) ||
strings .Contains (f .Value .Type (), "Array" ) ||
f .Value .Type () == "count" {
return true
}
return false
}
func (f Flag ) TakesValue () bool {
switch f .Value .Type () {
case "bool" , "boolSlice" , "count" :
return false
default :
return true
}
}
func (f Flag ) IsOptarg () bool {
return f .NoOptDefVal != ""
}
func (f Flag ) Style () string {
switch {
case !f .TakesValue ():
return style .Carapace .FlagNoArg
case f .IsOptarg ():
return style .Carapace .FlagOptArg
case f .Nargs () != 0 :
return style .Carapace .FlagMultiArg
default :
return style .Carapace .FlagArg
}
}
func (f Flag ) Required () bool {
if annotation := f .Annotations [cobra .BashCompOneRequiredFlag ]; len (annotation ) == 1 && annotation [0 ] == "true" {
return true
}
return false
}
func (f Flag ) Definition (prefix rune ) string {
p := string (prefix )
var definition string
switch f .GetMode () {
case ShorthandOnly :
definition = fmt .Sprintf ("%v%v" , p , f .Shorthand )
case NameAsShorthand :
definition = fmt .Sprintf ("%v%v, %v%v" , p , f .Shorthand , p , f .Name )
default :
switch f .Shorthand {
case "" :
definition = fmt .Sprintf ("%v%v%v" , p , p , f .Name )
default :
definition = fmt .Sprintf ("%v%v, %v%v%v" , p , f .Shorthand , p , p , f .Name )
}
}
if f .Hidden {
definition += "&"
}
if f .Required () {
definition += "!"
}
if f .IsRepeatable () {
definition += "*"
}
switch {
case f .IsOptarg ():
switch f .Value .Type () {
case "bool" , "boolSlice" , "count" :
default :
definition += "?"
}
case f .TakesValue ():
definition += "="
}
return definition
}
func (f Flag ) Consumes (arg string ) bool {
switch {
case f .Flag == nil :
return false
case !f .TakesValue ():
return false
case f .IsOptarg ():
return false
case len (f .Args ) == 0 :
return true
case f .Nargs () > 1 && len (f .Args ) < f .Nargs ():
return true
case f .Nargs () < 0 && !strings .HasPrefix (arg , string (f .FlagPrefix )):
return true
default :
return false
}
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .