Skip to content

Commit

Permalink
Merge pull request docopt#9 from mboersma/optional-os-exit
Browse files Browse the repository at this point in the history
fix(exit): add optional "exit" final arg to Parse()
  • Loading branch information
mboersma committed Apr 19, 2014
2 parents f091e9e + 581273a commit c48a597
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 71 deletions.
16 changes: 13 additions & 3 deletions docopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,24 @@ import (
// "<port>": "8"',
// "serial": false,
// "tcp": true}
func Parse(doc string, argv []string, help bool, version string, optionsFirst bool) (map[string]interface{}, error) {
func Parse(doc string, argv []string, help bool, version string,
optionsFirst bool, exit ...bool) (map[string]interface{}, error) {
// if "false" was the (optional) last arg, don't call os.Exit()
exitOk := true
if len(exit) > 0 {
exitOk = exit[0]
}
args, output, err := parse(doc, argv, help, version, optionsFirst)
if _, ok := err.(*UserError); ok {
fmt.Println(output)
os.Exit(1)
if exitOk {
os.Exit(1)
}
} else if len(output) > 0 && err == nil {
fmt.Println(output)
os.Exit(0)
if exitOk {
os.Exit(0)
}
}
return args, err
}
Expand Down
Loading

0 comments on commit c48a597

Please sign in to comment.