Debian Codegraph @

optstyle_windows.go

raw symbols SCIP
1 package flags
2
3 import (
4 "strings"
5 )
6
7 // Windows uses a front slash for both short and long options. Also it uses
8 // a colon for name/argument delimter.
9 const (
10 defaultShortOptDelimiter = '/'
11 defaultLongOptDelimiter = "/"
12 defaultNameArgDelimiter = ':'
13 )
14
15 func argumentIsOption(arg string) bool {
16 // Windows-style options allow front slash for the option
17 // delimiter.
18 return len(arg) > 0 && (arg[0] == '-' || arg[0] == '/')
19 }
20
21 // stripOptionPrefix returns the option without the prefix and whether or
22 // not the option is a long option or not.
23 func stripOptionPrefix(optname string) (string, bool) {
24 // Determine if the argument is a long option or not. Windows
25 // typically supports both long and short options with a single
26 // front slash as the option delimiter, so handle this situation
27 // nicely.
28 if strings.HasPrefix(optname, "--") {
29 return optname[2:], true
30 } else if strings.HasPrefix(optname, "-") {
31 return optname[1:], false
32 } else if strings.HasPrefix(optname, "/") {
33 optname = optname[1:]
34 if len(optname) > 1 {
35 return optname, true
36 }
37 return optname, 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.
45 func splitOption(option string) (string, *string) {
46 if len(option) == 0 {
47 return option, nil
48 }
49
50 // Windows typically uses a colon for the option name and argument
51 // delimiter while POSIX typically uses an equals. Support both styles,
52 // but don't allow the two to be mixed. That is to say /foo:bar and
53 // --foo=bar are acceptable, but /foo=bar and --foo:bar are not.
54 var pos int
55 if option[0] == '/' {
56 pos = strings.Index(option, ":")
57 } else if option[0] == '-' {
58 pos = strings.Index(option, "=")
59 }
60
61 if pos >= 0 {
62 rest := option[pos+1:]
63 return option[:pos], &rest
64 }
65
66 return option, nil
67 }
68
69 // newHelpGroup returns a new group that contains default help parameters.
70 func newHelpGroup(showHelp func() error) *Group {
71 // Windows CLI applications typically use /? for help, so make both
72 // that available as well as the POSIX style h and help.
73 var help struct {
74 ShowHelp func() error `short:"?" description:"Show this help message"`
75 ShowHelp2 func() error `short:"h" long:"help" description:"Show this help message"`
76 }
77 help.ShowHelp = showHelp
78 help.ShowHelp2 = showHelp
79 return NewGroup("Help Options", &help)
80 }