forked from tcnksm/gcli
/
flag_flag.go
69 lines (53 loc) · 1.39 KB
/
flag_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
package command
import (
"fmt"
"strings"
"github.com/tcnksm/gcli/skeleton"
)
const (
// defaultTypeString is default flag type
defaultTypeString = "string"
)
// FlagFlag implements the flag.Value interface and allows multiple
// calls to the same variable to append a list. It parses string and set them
// as skeleton.Flag.
type FlagFlag []*skeleton.Flag
// String
func (f *FlagFlag) String() string {
return ""
}
// Set parses input string and appends it on FlagdFlag.
// Input format must be NAME:TYPE:SYNOPSIS format.
func (f *FlagFlag) Set(v string) error {
flgStrs := strings.Split(v, ",")
for _, flgStr := range flgStrs {
parsedFlgStr := strings.Split(flgStr, ":")
if len(parsedFlgStr) > 3 {
return fmt.Errorf("flag must be specified by NAME:TYPE:DESCRIPTION format")
}
name := parsedFlgStr[0]
typeString := defaultTypeString
desc := ""
if len(parsedFlgStr) > 1 {
typeString = parsedFlgStr[1]
}
if len(parsedFlgStr) > 2 {
desc = parsedFlgStr[2]
// Delete unnessary characters
// TODO, this should not here..? or extract this as other function
desc = strings.Trim(desc, "\"")
desc = strings.Trim(desc, "'")
}
flag := &skeleton.Flag{
LongName: name,
TypeString: typeString,
Description: desc,
}
// Fix inputs string for using main processing
if err := flag.Fix(); err != nil {
return err
}
*f = append(*f, flag)
}
return nil
}