forked from aliyun/aliyun-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_field.go
70 lines (60 loc) · 1.28 KB
/
flag_field.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
/*
* Copyright (C) 2017-2018 Alibaba Group Holding Limited
*/
package cli
import (
"github.com/aliyun/aliyun-cli/i18n"
"fmt"
)
type Field struct {
//
// appear in `--flag key1=value1, key2=value2`
// if Key assigned with "", it can used with `--flag value1 value2`
Key string
//
// if Required is true, this field must be assigned
Required bool
//
// if Repeatable is true, this field can appear multiply times, eg: "--flag key1=value1 key2=value2"
Repeatable bool
//
// if field not appear, use this value, not used with Required
DefaultValue string
//
// Message show
Short *i18n.Text
assigned bool
value string
values []string
}
func (f *Field) assign(v string) {
f.assigned = true
f.value = v
f.values = append(f.values, v)
}
func (f *Field) getValue() (string, bool) {
if f.assigned {
return f.value, true
} else if f.DefaultValue != "" {
return f.DefaultValue, false
} else {
return "", false
}
}
func (f *Field) check() error {
if f.Required && !f.assigned {
if f.Key != "" {
return fmt.Errorf("%s= required", f.Key)
} else {
return fmt.Errorf("value required")
}
}
if !f.Repeatable && len(f.values) > 1 {
if f.Key != "" {
return fmt.Errorf("%s= duplicated", f.Key)
} else {
return fmt.Errorf("value duplicated")
}
}
return nil
}