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