blob: f3f28aeeff4b3d598f672c99c8fe8707f3c9acad [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001// +build !forceposix
2
3package flags
4
5import (
6 "strings"
7)
8
9// Windows uses a front slash for both short and long options. Also it uses
10// a colon for name/argument delimter.
11const (
12 defaultShortOptDelimiter = '/'
13 defaultLongOptDelimiter = "/"
14 defaultNameArgDelimiter = ':'
15)
16
17func argumentStartsOption(arg string) bool {
18 return len(arg) > 0 && (arg[0] == '-' || arg[0] == '/')
19}
20
21func argumentIsOption(arg string) bool {
22 // Windows-style options allow front slash for the option
23 // delimiter.
24 if len(arg) > 1 && arg[0] == '/' {
25 return true
26 }
27
28 if len(arg) > 1 && arg[0] == '-' && arg[1] != '-' {
29 return true
30 }
31
32 if len(arg) > 2 && arg[0] == '-' && arg[1] == '-' && arg[2] != '-' {
33 return true
34 }
35
36 return false
37}
38
39// stripOptionPrefix returns the option without the prefix and whether or
40// not the option is a long option or not.
41func stripOptionPrefix(optname string) (prefix string, name string, islong bool) {
42 // Determine if the argument is a long option or not. Windows
43 // typically supports both long and short options with a single
44 // front slash as the option delimiter, so handle this situation
45 // nicely.
46 possplit := 0
47
48 if strings.HasPrefix(optname, "--") {
49 possplit = 2
50 islong = true
51 } else if strings.HasPrefix(optname, "-") {
52 possplit = 1
53 islong = false
54 } else if strings.HasPrefix(optname, "/") {
55 possplit = 1
56 islong = len(optname) > 2
57 }
58
59 return optname[:possplit], optname[possplit:], islong
60}
61
62// splitOption attempts to split the passed option into a name and an argument.
63// When there is no argument specified, nil will be returned for it.
64func splitOption(prefix string, option string, islong bool) (string, string, *string) {
65 if len(option) == 0 {
66 return option, "", nil
67 }
68
69 // Windows typically uses a colon for the option name and argument
70 // delimiter while POSIX typically uses an equals. Support both styles,
71 // but don't allow the two to be mixed. That is to say /foo:bar and
72 // --foo=bar are acceptable, but /foo=bar and --foo:bar are not.
73 var pos int
74 var sp string
75
76 if prefix == "/" {
77 sp = ":"
78 pos = strings.Index(option, sp)
79 } else if len(prefix) > 0 {
80 sp = "="
81 pos = strings.Index(option, sp)
82 }
83
84 if (islong && pos >= 0) || (!islong && pos == 1) {
85 rest := option[pos+1:]
86 return option[:pos], sp, &rest
87 }
88
89 return option, "", nil
90}
91
92// addHelpGroup adds a new group that contains default help parameters.
93func (c *Command) addHelpGroup(showHelp func() error) *Group {
94 // Windows CLI applications typically use /? for help, so make both
95 // that available as well as the POSIX style h and help.
96 var help struct {
97 ShowHelpWindows func() error `short:"?" description:"Show this help message"`
98 ShowHelpPosix func() error `short:"h" long:"help" description:"Show this help message"`
99 }
100
101 help.ShowHelpWindows = showHelp
102 help.ShowHelpPosix = showHelp
103
104 ret, _ := c.AddGroup("Help Options", "", &help)
105 ret.isBuiltinHelp = true
106
107 return ret
108}