File: cobra.go

package info (click to toggle)
golang-github-shenwei356-util 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: makefile: 2
file content (160 lines) | stat: -rw-r--r-- 3,781 bytes parent folder | download | duplicates (3)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package cliutil

import (
	"fmt"
	"os"
	"strconv"

	"github.com/shenwei356/util/stringutil"
	"github.com/spf13/cobra"
)

func isStdin(file string) bool {
	return file == "-"
}

func CheckError(err error) {
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(-1)
	}
}

func GetFlagInt(cmd *cobra.Command, flag string) int {
	value, err := cmd.Flags().GetInt(flag)
	CheckError(err)
	return value
}

func GetFlagUint8(cmd *cobra.Command, flag string) uint8 {
	value, err := cmd.Flags().GetUint8(flag)
	CheckError(err)
	return value
}

func GetFlagUint32(cmd *cobra.Command, flag string) uint32 {
	value, err := cmd.Flags().GetUint32(flag)
	CheckError(err)
	return value
}

func GetFlagUint64(cmd *cobra.Command, flag string) uint64 {
	value, err := cmd.Flags().GetUint64(flag)
	CheckError(err)
	return value
}

func GetFlagPositiveInt(cmd *cobra.Command, flag string) int {
	value, err := cmd.Flags().GetInt(flag)
	CheckError(err)
	if value <= 0 {
		CheckError(fmt.Errorf("value of flag --%s should be greater than 0", flag))
	}
	return value
}

func GetFlagPositiveFloat64(cmd *cobra.Command, flag string) float64 {
	value, err := cmd.Flags().GetFloat64(flag)
	CheckError(err)
	if value <= 0 {
		CheckError(fmt.Errorf("value of flag --%s should be greater than 0", flag))
	}
	return value
}

func GetFlagNonNegativeInt(cmd *cobra.Command, flag string) int {
	value, err := cmd.Flags().GetInt(flag)
	CheckError(err)
	if value < 0 {
		CheckError(fmt.Errorf("value of flag --%s should be greater than or equal to 0", flag))
	}
	return value
}

func GetFlagNonNegativeFloat64(cmd *cobra.Command, flag string) float64 {
	value, err := cmd.Flags().GetFloat64(flag)
	CheckError(err)
	if value < 0 {
		CheckError(fmt.Errorf("value of flag --%s should be greater than or equal to ", flag))
	}
	return value
}

func GetFlagBool(cmd *cobra.Command, flag string) bool {
	value, err := cmd.Flags().GetBool(flag)
	CheckError(err)
	return value
}

func GetFlagString(cmd *cobra.Command, flag string) string {
	value, err := cmd.Flags().GetString(flag)
	CheckError(err)
	return value
}

func GetFlagNonEmptyString(cmd *cobra.Command, flag string) string {
	value, err := cmd.Flags().GetString(flag)
	CheckError(err)
	if value == "" {
		CheckError(fmt.Errorf("value of flag --%s should not be empty", flag))

	}
	return value
}

func GetFlagCommaSeparatedStrings(cmd *cobra.Command, flag string) []string {
	value, err := cmd.Flags().GetString(flag)
	CheckError(err)
	return stringutil.Split(value, ",")
}

func GetFlagSemicolonSeparatedStrings(cmd *cobra.Command, flag string) []string {
	value, err := cmd.Flags().GetString(flag)
	CheckError(err)
	return stringutil.Split(value, ";")
}

func GetFlagCommaSeparatedInts(cmd *cobra.Command, flag string) []int {
	filedsStrList := GetFlagCommaSeparatedStrings(cmd, flag)
	fields := make([]int, len(filedsStrList))
	for i, value := range filedsStrList {
		v, err := strconv.Atoi(value)
		if err != nil {
			CheckError(fmt.Errorf("value of flag --%s should be comma separated integers", flag))
		}
		fields[i] = v
	}
	return fields
}

func GetFlagRune(cmd *cobra.Command, flag string) rune {
	value, err := cmd.Flags().GetString(flag)
	CheckError(err)
	if len(value) > 1 {
		CheckError(fmt.Errorf("value of flag --%s should has length of 1", flag))
	}
	var v rune
	for _, r := range value {
		v = r
		break
	}
	return v
}

func GetFlagFloat64(cmd *cobra.Command, flag string) float64 {
	value, err := cmd.Flags().GetFloat64(flag)
	CheckError(err)
	return value
}

func GetFlagInt64(cmd *cobra.Command, flag string) int64 {
	value, err := cmd.Flags().GetInt64(flag)
	CheckError(err)
	return value
}

func GetFlagStringSlice(cmd *cobra.Command, flag string) []string {
	value, err := cmd.Flags().GetStringSlice(flag)
	CheckError(err)
	return value
}