-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_line.go
84 lines (71 loc) · 2.85 KB
/
command_line.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package flago
import (
"flag"
"os"
)
// CommandLine is a default FlagSet that is used by the package functions.
// It's a wrapper around flag.CommandLine to follow the same pattern as in the stdlib.
var CommandLine = Wrap(flag.CommandLine)
func init() {
// Override generic FlagSet default Usage with call to global Usage.
// Note: This is not CommandLine.Usage = Usage,
// because we want any eventual call to use any updated value of Usage,
// not the value it has when this line is run.
CommandLine.Usage = commandLineUsage
}
func commandLineUsage() {
Usage()
}
// StructVar registers the given struct with the default FlagSet
// `ignoredFields` is a slice of pointers to fields that should be ignored and not registered as flags
// See FlagSet.StructVar
func StructVar(p any, ignoredFields ...any) error {
return CommandLine.StructVar(p, ignoredFields...)
}
// StructVarWithPrefix registers the given struct with the default FlagSet using
// the given prefix for flag names.
// `ignoredFields` is a slice of pointers to fields that should be ignored and not registered as flags
// See FlagSet.StructVarWithPrefix
func StructVarWithPrefix(p any, flagsPrefix string, ignoredFields ...any) error {
return CommandLine.StructVarWithPrefix(p, flagsPrefix, ignoredFields...)
}
// SetAllowParsingMultipleAliases sets the behavior of Parse() when multiple tag names
// assigned to same field are passed.
// If `true`, it will be ignored and only the last value will be used.
// If `false`, Parse() will return an error.
// Default value is `false`.
func SetAllowParsingMultipleAliases(allow bool) {
CommandLine.SetAllowParsingMultipleAliases(allow)
}
// SetIgnoreUnknown sets the behavior of Parse() when unknown flags are passed.
// If `true`, they will be ignored.
// If `false`, Parse() will return an error.
// Default value is `false`.
func SetIgnoreUnknown(ignore bool) {
CommandLine.SetIgnoreUnknown(ignore)
}
// SetIgnoreUnknownAmbiguousAsBoolFlags sets the behavior of Parse() when unknown flags are passed,
// and they are ambiguous with known bool flags.
// If `true`, they will be treated as bool flags.
// If `false`, Parse() will return an error.
// Default value is `false`.
func SetIgnoreUnknownAmbiguousAsBoolFlags(treatAsBool bool) {
CommandLine.SetIgnoreUnknownAmbiguousAsBoolFlags(treatAsBool)
}
// GetIgnoredArgs returns a slice of arguments that were ignored during the last call to Parse()
// because of SetIgnoreUnknown(true), nil otherwise
func GetIgnoredArgs() []string {
return CommandLine.GetIgnoredArgs()
}
// Parse parses the command-line flags using the default FlagSet
func Parse() error {
return CommandLine.Parse(os.Args[1:])
}
// PrintDefaults prints the default FlagSet usage to stdout grouping alternative flag names
func PrintDefaults() {
CommandLine.PrintDefaults()
}
var Usage = func() {
printUsageTitle(CommandLine.FlagSet, os.Args[0])
PrintDefaults()
}