add expected output for usage example

This commit is contained in:
Alex Flint 2019-05-03 13:07:12 -07:00
parent e2ce620ee4
commit 3392c173d7
2 changed files with 30 additions and 8 deletions

View File

@ -115,5 +115,23 @@ func Example_usageString() {
Dataset string `help:"dataset to use"`
Optimize int `arg:"-O,help:optimization level"`
}
// This is only necessary when running inside golang's runnable example harness
osExit = func(int) {}
MustParse(&args)
// output:
// Usage: example [--verbose] [--dataset DATASET] [--optimize OPTIMIZE] INPUT [OUTPUT [OUTPUT ...]]
//
// Positional arguments:
// INPUT
// OUTPUT
//
// Options:
// --verbose, -v verbosity level
// --dataset DATASET dataset to use
// --optimize OPTIMIZE, -O OPTIMIZE
// optimization level
// --help, -h display this help and exit
}

View File

@ -13,6 +13,9 @@ import (
scalar "github.com/alexflint/go-scalar"
)
// to enable monkey-patching during tests
var osExit = os.Exit
// spec represents a command line option
type spec struct {
dest reflect.Value
@ -38,20 +41,21 @@ func MustParse(dest ...interface{}) *Parser {
p, err := NewParser(Config{}, dest...)
if err != nil {
fmt.Println(err)
os.Exit(-1)
osExit(-1)
}
err = p.Parse(flags())
if err == ErrHelp {
switch {
case err == ErrHelp:
p.WriteHelp(os.Stdout)
os.Exit(0)
}
if err == ErrVersion {
osExit(0)
case err == ErrVersion:
fmt.Println(p.version)
os.Exit(0)
}
if err != nil {
osExit(0)
case err != nil:
p.Fail(err.Error())
}
return p
}