-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathflag.go
89 lines (72 loc) · 1.92 KB
/
flag.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
package skeleton
import (
"fmt"
"strings"
)
// TypeString represents type as string
const (
TypeStringInt = "int"
TypeStringBool = "bool"
TypeStringString = "string"
)
// Flag stores flag meta informations
type Flag struct {
// Name is flag name, this is used for flag variable name in generated code.
// Name is equal to titled LongName.
Name string
// LongName is long form of the flag name.
// This must be provided by user
LongName string
// ShortName is short form of flag name.
// This is generated automatically from LongName
ShortName string
// VariableName is variable name which is usded for
// holding a flag value in generating source code
VariableName string
// TypeString is flag type. This must be provided by user
TypeString string
// Default is default value.
// This is automatically generated from TypeString
Default interface{}
// Description is help message of the flag.
Description string
}
// Fix fixed user input for templating.
func (f *Flag) Fix() error {
// Fix Typestring
if err := f.fixTypeString(); err != nil {
return err
}
f.LongName = strings.ToLower(f.LongName)
// Name is same as LongName by default
f.Name = f.LongName
f.VariableName = camelCase(f.LongName)
// ShortName is first character of LongName
// TODO, when same first character is provided.
f.ShortName = string(f.LongName[0])
return nil
}
// FixTypeString fixes Type string which is provided
// by user and set Default variable.
func (f *Flag) fixTypeString() error {
switch strings.ToLower(f.TypeString) {
case "bool", "b":
f.TypeString = TypeStringBool
if f.Default == nil {
f.Default = false
}
case "int", "i":
f.TypeString = TypeStringInt
if f.Default == nil {
f.Default = 0
}
case "string", "str", "s":
f.TypeString = TypeStringString
if f.Default == nil {
f.Default = ""
}
default:
return fmt.Errorf("unexpected type is provided: %s", f.TypeString)
}
return nil
}