Skip to content

Commit 581273a

Browse files
committed
fix(exit): add optional "exit" final arg to Parse()
Users of docopt.go should be able to handle their own return codes and os.Exit() processing. Calling the Parse() func with an extra last arg of "false" means docopt.go will not call os.Exit(). Additionally, the docopt_test.go suite was changed to call the exported Parse() func instead of the parse() implementation. Fixes #7.
1 parent f091e9e commit 581273a

File tree

2 files changed

+105
-71
lines changed

2 files changed

+105
-71
lines changed

docopt.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,24 @@ import (
8585
// "<port>": "8"',
8686
// "serial": false,
8787
// "tcp": true}
88-
func Parse(doc string, argv []string, help bool, version string, optionsFirst bool) (map[string]interface{}, error) {
88+
func Parse(doc string, argv []string, help bool, version string,
89+
optionsFirst bool, exit ...bool) (map[string]interface{}, error) {
90+
// if "false" was the (optional) last arg, don't call os.Exit()
91+
exitOk := true
92+
if len(exit) > 0 {
93+
exitOk = exit[0]
94+
}
8995
args, output, err := parse(doc, argv, help, version, optionsFirst)
9096
if _, ok := err.(*UserError); ok {
9197
fmt.Println(output)
92-
os.Exit(1)
98+
if exitOk {
99+
os.Exit(1)
100+
}
93101
} else if len(output) > 0 && err == nil {
94102
fmt.Println(output)
95-
os.Exit(0)
103+
if exitOk {
104+
os.Exit(0)
105+
}
96106
}
97107
return args, err
98108
}

0 commit comments

Comments
 (0)