Skip to content

Commit abafb99

Browse files
committedFeb 2, 2018
feature: flag to control what to output
This commit adds --print flag that allows users to specify what to output. Flag accepts a list of comma-separated values. Allowed values are: intro (short: i), progress (p), result (r). Examples: --print=i,p,r # outputs everything --print=intro,progress,output # same as above --print=i,r # intro & result only Closes #25, updates #24.
1 parent 5f661ed commit abafb99

8 files changed

+418
-79
lines changed
 

‎args_parser.go

+57
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package main
22

33
import (
4+
"fmt"
45
"runtime"
56
"strconv"
7+
"strings"
68
"time"
79

810
"github.com/alecthomas/kingpin"
@@ -32,6 +34,8 @@ type kingpinParser struct {
3234
keyPath string
3335
rate *nullableUint64
3436
clientType clientTyp
37+
38+
printSpec *nullableString
3539
}
3640

3741
func newKingpinParser() argsParser {
@@ -52,6 +56,7 @@ func newKingpinParser() argsParser {
5256
url: "",
5357
rate: new(nullableUint64),
5458
clientType: fhttp,
59+
printSpec: new(nullableString),
5560
}
5661

5762
app := kingpin.New("", "Fast cross-platform HTTP benchmarking tool").
@@ -133,6 +138,18 @@ func newKingpinParser() argsParser {
133138
}).
134139
Bool()
135140

141+
app.Flag(
142+
"print", "Specifies what to output. Comma-separated list of values"+
143+
" 'intro' (short: 'i'), 'progress' (short: 'p'),"+
144+
" 'result' (short: 'r'). Examples:"+
145+
"\n\t* i,p,r (prints everything)"+
146+
"\n\t* intro,result (intro & result)"+
147+
"\n\t* r (result only)"+
148+
"\n\t* result (same as above)").
149+
PlaceHolder("<spec>").
150+
Short('p').
151+
SetValue(kparser.printSpec)
152+
136153
app.Arg("url", "Target's URL").Required().
137154
StringVar(&kparser.url)
138155

@@ -146,6 +163,13 @@ func (k *kingpinParser) parse(args []string) (config, error) {
146163
if err != nil {
147164
return emptyConf, err
148165
}
166+
pi, pp, pr := true, true, true
167+
if k.printSpec.val != nil {
168+
pi, pp, pr, err = parsePrintSpec(*k.printSpec.val)
169+
if err != nil {
170+
return emptyConf, err
171+
}
172+
}
149173
return config{
150174
numConns: k.numConns,
151175
numReqs: k.numReqs.val,
@@ -163,5 +187,38 @@ func (k *kingpinParser) parse(args []string) (config, error) {
163187
insecure: k.insecure,
164188
rate: k.rate.val,
165189
clientType: k.clientType,
190+
printIntro: pi,
191+
printProgress: pp,
192+
printResult: pr,
166193
}, nil
167194
}
195+
196+
func parsePrintSpec(spec string) (bool, bool, bool, error) {
197+
pi, pp, pr := false, false, false
198+
if spec == "" {
199+
return false, false, false, errEmptyPrintSpec
200+
}
201+
parts := strings.Split(spec, ",")
202+
partsCount := 0
203+
for _, p := range parts {
204+
switch p {
205+
case "i", "intro":
206+
pi = true
207+
case "p", "progress":
208+
pp = true
209+
case "r", "result":
210+
pr = true
211+
default:
212+
return false, false, false,
213+
fmt.Errorf("%q is not a valid part of print spec", p)
214+
}
215+
partsCount++
216+
}
217+
if partsCount < 1 || partsCount > 3 {
218+
return false, false, false,
219+
fmt.Errorf(
220+
"Spec %q has too many parts, at most 3 are allowed", spec,
221+
)
222+
}
223+
return pi, pp, pr, nil
224+
}

0 commit comments

Comments
 (0)
Please sign in to comment.