-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflags.go
147 lines (138 loc) · 5.25 KB
/
flags.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
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
package kouch
import (
"github.com/go-kivik/couchdb/chttp"
"github.com/go-kivik/kouch/internal/errors"
"github.com/spf13/pflag"
)
// Common command line flags
const (
// Curl-equivalent flags
FlagVerbose = "verbose"
FlagOutputFile = "output"
FlagData = "data"
FlagHead = "head"
FlagDumpHeader = "dump-header"
FlagUser = "user"
FlagCreateDirs = "create-dirs"
// Custom flags
FlagClobber = "force"
FlagConfigFile = "kouchconfig"
FlagServerRoot = "root"
FlagDataJSON = "data-json"
FlagDataYAML = "data-yaml"
FlagOutputFormat = "output-format"
FlagFilename = "filename"
FlagDocument = "id"
FlagDatabase = "database"
FlagFullCommit = "full-commit"
FlagIfNoneMatch = "if-none-match"
FlagRev = "rev"
FlagAutoRev = "auto-rev"
FlagShards = "shards"
FlagPassword = "password"
FlagContext = "context"
FlagConflicts = "conflicts"
FlagDescending = "descending"
FlagEndKey = "endkey"
FlagEndKeyDocID = "endkey-docid"
FlagGroup = "group"
FlagGroupLevel = "group-level"
FlagIncludeDocs = "include-docs"
FlagIncludeAttachments = "attachments"
FlagIncludeAttEncoding = "att-encoding-info"
FlagInclusiveEnd = "inclusive-end"
FlagKey = "key"
FlagKeys = "keys"
FlagLimit = "limit"
FlagReduce = "reduce"
FlagSkip = "skip"
FlagSorted = "sorted"
FlagStable = "stable"
FlagStale = "stale"
FlagStartKey = "startkey"
FlagStartKeyDocID = "startkey-docid"
FlagUpdate = "update"
FlagUpdateSeq = "update-seq"
FlagTemplate = "template"
FlagTemplateFile = "template-file"
FlagJSONPrefix = "json-prefix"
FlagJSONIndent = "json-indent"
FlagJSONEscapeHTML = "json-escape-html"
FlagAttsSince = "atts-since"
FlagIncludeDeletedConflicts = "deleted-conflicts"
FlagForceLatest = "latest"
FlagIncludeLocalSeq = "local-seq"
FlagMeta = "meta"
FlagOpenRevs = "open-revs"
FlagRevs = "revs"
FlagRevsInfo = "revs-info"
FlagBatch = "batch"
FlagNewEdits = "new-edits"
// Curl-equivalent short flags
FlagShortVerbose = "v"
FlagShortOutputFile = "o"
FlagShortData = "d"
FlagShortHead = "I"
FlagShortDumpHeader = "D"
FlagShortUser = "u"
// Short versions, custom
FlagShortServerRoot = "S"
FlagShortOutputFormat = "F"
FlagShortRev = "r"
FlagShortAutoRev = "R"
FlagShortShards = "q"
FlagShortPassword = "p"
)
type paramParser func(flags *pflag.FlagSet, flagName string) ([]string, error)
var flagParsers = map[string]paramParser{
FlagEndKey: parseParamString,
FlagEndKeyDocID: parseParamString,
FlagKey: parseParamString,
FlagStale: parseParamString,
FlagStartKey: parseParamString,
FlagStartKeyDocID: parseParamString,
FlagUpdate: parseParamString,
FlagRev: parseParamString,
FlagKeys: parseParamStringArray,
FlagGroupLevel: parseParamInt,
FlagLimit: parseParamInt,
FlagSkip: parseParamInt,
FlagShards: parseParamInt,
FlagConflicts: parseParamBool,
FlagDescending: parseParamBool,
FlagGroup: parseParamBool,
FlagIncludeDocs: parseParamBool,
FlagIncludeAttachments: parseParamBool,
FlagIncludeAttEncoding: parseParamBool,
FlagInclusiveEnd: parseParamBool,
FlagReduce: parseParamBool,
FlagSorted: parseParamBool,
FlagStable: parseParamBool,
FlagUpdateSeq: parseParamBool,
FlagIncludeDeletedConflicts: parseParamBool,
FlagAttsSince: parseParamStringSlice,
FlagOpenRevs: parseParamStringSlice,
FlagForceLatest: parseParamBool,
FlagIncludeLocalSeq: parseParamBool,
FlagMeta: parseParamBool,
FlagRevs: parseParamBool,
FlagRevsInfo: parseParamBool,
FlagNewEdits: parseParamBool,
}
type paramValidator func(flag string, value []string) error
var flagValidators = map[string]paramValidator{
FlagStale: func(flag string, v []string) error {
switch v[0] {
case "ok", "update_after", "false":
return nil
}
return errors.NewExitError(chttp.ExitFailedToInitialize, "Invalid value for --%s. Supported options: `ok`, `update_after`, `false`", flag)
},
FlagUpdate: func(flag string, v []string) error {
switch v[0] {
case "true", "false", "lazy":
return nil
}
return errors.NewExitError(chttp.ExitFailedToInitialize, "Invalid value for --%s. Supported options: `true`, `false`, `lazy`", flag)
},
}