aboutsummaryrefslogtreecommitdiffhomepage
path: root/command/parse.go
diff options
context:
space:
mode:
Diffstat (limited to 'command/parse.go')
-rw-r--r--command/parse.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/command/parse.go b/command/parse.go
index 02120464..8e21433c 100644
--- a/command/parse.go
+++ b/command/parse.go
@@ -3,6 +3,7 @@ package command
import (
"errors"
"log"
+ "os"
)
var (
@@ -78,3 +79,27 @@ func (n *node) printf(format string, a ...any) {
n.logf(format, a...)
}
}
+
+func (n *node) MustParse(arguments []string, handleError func(error)) {
+ switch err := n.Parse(arguments); err {
+ case nil:
+ return
+ case ErrHelp:
+ os.Exit(0)
+ case ErrNoMatch:
+ os.Exit(1)
+ case ErrEmptyTree:
+ os.Exit(1)
+ default:
+ var flagError FlagError
+ if !errors.As(err, &flagError) { // returned by HandlerFunc
+ handleError(err)
+ os.Exit(1)
+ }
+
+ if flagError.Success() {
+ os.Exit(0)
+ }
+ os.Exit(1)
+ }
+}