MCPcopy
hub / github.com/spf13/pflag / parseSingleShortArg

Method parseSingleShortArg

flag.go:1040–1114  ·  view source on GitHub ↗
(shorthands string, args []string, fn parseFunc)

Source from the content-addressed store, hash-verified

1038}
1039
1040func (f *FlagSet) parseSingleShortArg(shorthands string, args []string, fn parseFunc) (outShorts string, outArgs []string, err error) {
1041 outArgs = args
1042
1043 if isGotestShorthandFlag(shorthands) {
1044 return
1045 }
1046
1047 outShorts = shorthands[1:]
1048 c := shorthands[0]
1049
1050 flag, exists := f.shorthands[c]
1051 if !exists {
1052 switch {
1053 case c == 'h':
1054 f.usage()
1055 err = ErrHelp
1056 return
1057 case f.ParseErrorsWhitelist.UnknownFlags:
1058 fallthrough
1059 case f.ParseErrorsAllowlist.UnknownFlags:
1060 // '-f=arg arg ...'
1061 // we do not want to lose arg in this case
1062 if len(shorthands) > 2 && shorthands[1] == '=' {
1063 outShorts = ""
1064 return
1065 }
1066
1067 outArgs = stripUnknownFlagValue(outArgs)
1068 return
1069 default:
1070 err = f.fail(&NotExistError{
1071 name: string(c),
1072 specifiedShorthands: shorthands,
1073 messageType: flagUnknownShorthandFlagMessage,
1074 })
1075 return
1076 }
1077 }
1078
1079 var value string
1080 if len(shorthands) > 2 && shorthands[1] == '=' {
1081 // '-f=arg'
1082 value = shorthands[2:]
1083 outShorts = ""
1084 } else if flag.NoOptDefVal != "" {
1085 // '-f' (arg was optional)
1086 value = flag.NoOptDefVal
1087 } else if len(shorthands) > 1 {
1088 // '-farg'
1089 value = shorthands[1:]
1090 outShorts = ""
1091 } else if len(args) > 0 {
1092 // '-f arg'
1093 value = args[0]
1094 outArgs = args[1:]
1095 } else {
1096 // '-f' (arg was required)
1097 err = f.fail(&ValueRequiredError{

Callers 1

parseShortArgMethod · 0.95

Calls 5

usageMethod · 0.95
failMethod · 0.95
OutputMethod · 0.95
isGotestShorthandFlagFunction · 0.85
stripUnknownFlagValueFunction · 0.85

Tested by

no test coverage detected