-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynflags.go
92 lines (78 loc) · 2.61 KB
/
dynflags.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
85
86
87
88
89
90
91
92
package dynflags
import (
"fmt"
"io"
"os"
)
// ParseBehavior defines how the parser handles errors
type ParseBehavior int
const (
// Continue parsing on error
ContinueOnError ParseBehavior = iota
// Exit on error
ExitOnError
)
// DynFlags manages configuration and parsed values
type DynFlags struct {
configGroups map[string]*ConfigGroup // Static parent groups
groupOrder []string // Order of group names
SortGroups bool // Sort groups in help message
SortFlags bool // Sort flags in help message
parsedGroups map[string][]*ParsedGroup // Parsed child groups organized by parent group
parseBehavior ParseBehavior // Parsing behavior
unparsedArgs []string // Arguments that couldn't be parsed
output io.Writer // Output for usage/help
usage func() // Customizable usage function
title string // Title in the help message
description string // Description after the title in the help message
epilog string // Epilog in the help message
}
// New initializes a new DynFlags instance
func New(behavior ParseBehavior) *DynFlags {
df := &DynFlags{
configGroups: make(map[string]*ConfigGroup),
parsedGroups: make(map[string][]*ParsedGroup),
parseBehavior: behavior,
output: os.Stdout,
}
df.usage = func() { df.Usage() }
return df
}
// Title adds a title to the help message
func (df *DynFlags) Title(title string) {
df.title = title
}
// Description adds a descripton after the Title
func (df *DynFlags) Description(description string) {
df.description = description
}
// Epilog adds an epilog after the description of the dynamic flags to the help message
func (df *DynFlags) Epilog(epilog string) {
df.epilog = epilog
}
// Group defines a new group or retrieves an existing one
func (df *DynFlags) Group(name string) *ConfigGroup {
if _, exists := df.configGroups[name]; exists {
return df.configGroups[name]
}
df.groupOrder = append(df.groupOrder, name)
group := &ConfigGroup{
Name: name,
Flags: make(map[string]*Flag),
}
df.configGroups[name] = group
return group
}
// UnknownArgs returns the list of unparseable arguments.
func (df *DynFlags) UnknownArgs() []string {
return df.unparsedArgs
}
// DefaultUsage provides the default usage output
func (df *DynFlags) Usage() {
fmt.Fprintf(df.output, "Usage: [--<group>.<identifier>.<flag> value]\n\n")
df.PrintDefaults()
}
// SetOutput sets the output writer
func (df *DynFlags) SetOutput(buf io.Writer) {
df.output = buf
}