blob: f84b69746d3b7523e9946c598c0a9c64f3d82ba6 [file] [log] [blame]
Abhay Kumarfe505f22025-11-10 14:16:31 +00001//go:build !windows || forceposix
Naveen Sampath04696f72022-06-13 15:19:14 +05302// +build !windows forceposix
3
4package flags
5
6import (
7 "strings"
8)
9
10const (
11 defaultShortOptDelimiter = '-'
12 defaultLongOptDelimiter = "--"
13 defaultNameArgDelimiter = '='
14)
15
16func argumentStartsOption(arg string) bool {
17 return len(arg) > 0 && arg[0] == '-'
18}
19
20func 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.
34func 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.
46func 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.
58func (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}