| Abhay Kumar | fe505f2 | 2025-11-10 14:16:31 +0000 | [diff] [blame^] | 1 | //go:build !windows || forceposix |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 2 | // +build !windows forceposix |
| 3 | |
| 4 | package flags |
| 5 | |
| 6 | import ( |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | defaultShortOptDelimiter = '-' |
| 12 | defaultLongOptDelimiter = "--" |
| 13 | defaultNameArgDelimiter = '=' |
| 14 | ) |
| 15 | |
| 16 | func argumentStartsOption(arg string) bool { |
| 17 | return len(arg) > 0 && arg[0] == '-' |
| 18 | } |
| 19 | |
| 20 | func argumentIsOption(arg string) bool { |
| 21 | if len(arg) > 1 && arg[0] == '-' && arg[1] != '-' { |
| 22 | return true |
| 23 | } |
| 24 | |
| 25 | if len(arg) > 2 && arg[0] == '-' && arg[1] == '-' && arg[2] != '-' { |
| 26 | return true |
| 27 | } |
| 28 | |
| 29 | return false |
| 30 | } |
| 31 | |
| 32 | // stripOptionPrefix returns the option without the prefix and whether or |
| 33 | // not the option is a long option or not. |
| 34 | func stripOptionPrefix(optname string) (prefix string, name string, islong bool) { |
| 35 | if strings.HasPrefix(optname, "--") { |
| 36 | return "--", optname[2:], true |
| 37 | } else if strings.HasPrefix(optname, "-") { |
| 38 | return "-", optname[1:], false |
| 39 | } |
| 40 | |
| 41 | return "", optname, false |
| 42 | } |
| 43 | |
| 44 | // splitOption attempts to split the passed option into a name and an argument. |
| 45 | // When there is no argument specified, nil will be returned for it. |
| 46 | func splitOption(prefix string, option string, islong bool) (string, string, *string) { |
| 47 | pos := strings.Index(option, "=") |
| 48 | |
| 49 | if (islong && pos >= 0) || (!islong && pos == 1) { |
| 50 | rest := option[pos+1:] |
| 51 | return option[:pos], "=", &rest |
| 52 | } |
| 53 | |
| 54 | return option, "", nil |
| 55 | } |
| 56 | |
| 57 | // addHelpGroup adds a new group that contains default help parameters. |
| 58 | func (c *Command) addHelpGroup(showHelp func() error) *Group { |
| 59 | var help struct { |
| 60 | ShowHelp func() error `short:"h" long:"help" description:"Show this help message"` |
| 61 | } |
| 62 | |
| 63 | help.ShowHelp = showHelp |
| 64 | ret, _ := c.AddGroup("Help Options", "", &help) |
| 65 | ret.isBuiltinHelp = true |
| 66 | |
| 67 | return ret |
| 68 | } |