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