Skip to content

Commit c48a597

Browse files
committed
Merge pull request #9 from mboersma/optional-os-exit
fix(exit): add optional "exit" final arg to Parse()
2 parents f091e9e + 581273a commit c48a597

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)