add expected output for usage example
This commit is contained in:
parent
e2ce620ee4
commit
3392c173d7
|
@ -115,5 +115,23 @@ func Example_usageString() {
|
||||||
Dataset string `help:"dataset to use"`
|
Dataset string `help:"dataset to use"`
|
||||||
Optimize int `arg:"-O,help:optimization level"`
|
Optimize int `arg:"-O,help:optimization level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is only necessary when running inside golang's runnable example harness
|
||||||
|
osExit = func(int) {}
|
||||||
|
|
||||||
MustParse(&args)
|
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
|
||||||
}
|
}
|
||||||
|
|
20
parse.go
20
parse.go
|
@ -13,6 +13,9 @@ import (
|
||||||
scalar "github.com/alexflint/go-scalar"
|
scalar "github.com/alexflint/go-scalar"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// to enable monkey-patching during tests
|
||||||
|
var osExit = os.Exit
|
||||||
|
|
||||||
// spec represents a command line option
|
// spec represents a command line option
|
||||||
type spec struct {
|
type spec struct {
|
||||||
dest reflect.Value
|
dest reflect.Value
|
||||||
|
@ -38,20 +41,21 @@ func MustParse(dest ...interface{}) *Parser {
|
||||||
p, err := NewParser(Config{}, dest...)
|
p, err := NewParser(Config{}, dest...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(-1)
|
osExit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = p.Parse(flags())
|
err = p.Parse(flags())
|
||||||
if err == ErrHelp {
|
switch {
|
||||||
|
case err == ErrHelp:
|
||||||
p.WriteHelp(os.Stdout)
|
p.WriteHelp(os.Stdout)
|
||||||
os.Exit(0)
|
osExit(0)
|
||||||
}
|
case err == ErrVersion:
|
||||||
if err == ErrVersion {
|
|
||||||
fmt.Println(p.version)
|
fmt.Println(p.version)
|
||||||
os.Exit(0)
|
osExit(0)
|
||||||
}
|
case err != nil:
|
||||||
if err != nil {
|
|
||||||
p.Fail(err.Error())
|
p.Fail(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue