-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflag.go
More file actions
57 lines (47 loc) · 1.63 KB
/
flag.go
File metadata and controls
57 lines (47 loc) · 1.63 KB
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
package cli
// Flag represents a command-line flag. It combines the FlagValuer interface
// with methods to access flag metadata (long name, short name, description).
type Flag interface {
// FlagValuer defines methods to get and set flag's value.
FlagValuer
// LongName returns the long name of the flag (e.g., "--verbose").
LongName() string
// ShortName returns the short name of the flag (e.g., "-v"). May be empty.
ShortName() string
// Description returns a description of the flag's purpose.
Description() string
}
// NewFlag creates a new Flag instance.
//
// longName is the long flag name, like --longname ; cannot be empty.
// shortName is the short flag name ; usually 1 character, like -s ; can be empty.
// valuer provide the way to set value to the destination.
// description is a short text explaining the flag ; can be empty.
//
// It panics if invalid inputs are provided.
func NewFlag(longName, shortName string, valuer FlagValuer, description string) Flag {
if longName == "" && shortName == "" {
panic("longName and/or shortName must be non-empty")
}
if shortName != "" && len(shortName) > 1 {
panic("shortName must be one character long")
}
if valuer == nil {
panic("a non-nil valuer is required")
}
return &flagValue{
FlagValuer: valuer,
longName: longName,
shortName: shortName,
description: description,
}
}
type flagValue struct {
FlagValuer
longName string
shortName string
description string
}
func (f flagValue) LongName() string { return f.longName }
func (f flagValue) ShortName() string { return f.shortName }
func (f flagValue) Description() string { return f.description }