-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringflag.go
67 lines (60 loc) · 1.34 KB
/
stringflag.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
package argumentative
import (
"fmt"
)
// struct for a single configured flag
type StringFlag struct {
Longflag string
Shortflag string
Description string
Required bool
Default string
Value *string
}
// Factory to generate a new flag
func NewStringFlag(longflag string, shortflag string, required bool, defaultvalue string, description string) StringFlag {
flag := StringFlag{
Longflag: longflag,
Shortflag: shortflag,
Description: description,
Required: required,
Default: defaultvalue,
Value: new(string),
}
if defaultvalue != "" {
*flag.Value = defaultvalue
}
return flag
}
// Generate the string for the long description
func (f *StringFlag) GetLongDescription() string {
flagnames := ""
if f.Shortflag != "" {
flagnames += "-" + f.Shortflag + ", "
}
flagnames += "--" + f.Longflag
output := fmt.Sprintf("%-25s", flagnames)
if f.Description != "" {
output += f.Description
}
if f.Default != "" {
output += " (Default: " + f.Default + ")"
}
return output
}
// Generate the string for a short description in the 'Usage:' line
func (f *StringFlag) GetShortDescription() string {
output := " "
if !f.Required {
output += "["
}
if f.Shortflag != "" {
output += "-" + f.Shortflag
} else {
output += "--" + f.Longflag
}
if !f.Required {
output += "]"
}
return output
}