forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_helper.go
61 lines (47 loc) · 1.45 KB
/
flag_helper.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
package app
import (
"fmt"
"strings"
"unicode/utf8"
"github.com/codegangsta/cli"
)
func NewIntFlag(name, usage string) IntFlagWithNoDefault {
return IntFlagWithNoDefault{cli.IntFlag{Name: name, Usage: usage}}
}
func NewIntFlagWithValue(name, usage string, value int) IntFlagWithNoDefault {
return IntFlagWithNoDefault{cli.IntFlag{Name: name, Value: value, Usage: usage}}
}
func NewStringFlag(name, usage string) StringFlagWithNoDefault {
return StringFlagWithNoDefault{cli.StringFlag{Name: name, Usage: usage}}
}
func NewStringSliceFlag(name, usage string) StringSliceFlagWithNoDefault {
return StringSliceFlagWithNoDefault{cli.StringSliceFlag{Name: name, Usage: usage, Value: &cli.StringSlice{}}}
}
type IntFlagWithNoDefault struct {
cli.IntFlag
}
type StringFlagWithNoDefault struct {
cli.StringFlag
}
type StringSliceFlagWithNoDefault struct {
cli.StringSliceFlag
}
func (f IntFlagWithNoDefault) String() string {
defaultVal := fmt.Sprintf("'%v'", f.Value)
return strings.Replace(f.IntFlag.String(), defaultVal, "", 1)
}
func (f StringFlagWithNoDefault) String() string {
defaultVal := fmt.Sprintf("'%v'", f.Value)
return strings.Replace(f.StringFlag.String(), defaultVal, "", 1)
}
func (f StringSliceFlagWithNoDefault) String() string {
return fmt.Sprintf("%s%s \t%s", prefixFor(f.Name), f.Name, f.Usage)
}
func prefixFor(name string) (prefix string) {
if utf8.RuneCountInString(name) == 1 {
prefix = "-"
} else {
prefix = "--"
}
return
}