summaryrefslogtreecommitdiff
path: root/parse.go
diff options
context:
space:
mode:
authorDaniele Sluijters <[email protected]>2022-10-05 17:59:23 +0200
committerDaniele Sluijters <[email protected]>2022-10-10 17:25:14 +0200
commit4fc9666f79d7a9a84be9963e34b5f2479cd340b6 (patch)
tree3c922731582aa6a69241607838bcb52d783457ce /parse.go
parent11f9b624a9e5ee803b3810d2329fb027662c69f4 (diff)
Implement MustParse on Parse
This moves most of the body of the MustParse function into a MustParse method on a Parser. The MustParse function is now implemented by calling the MustParse function on the Parser it implicitly creates. Closes: #194
Diffstat (limited to 'parse.go')
-rw-r--r--parse.go27
1 files changed, 15 insertions, 12 deletions
diff --git a/parse.go b/parse.go
index c8cd79e..dc87947 100644
--- a/parse.go
+++ b/parse.go
@@ -82,18 +82,7 @@ func MustParse(dest ...interface{}) *Parser {
return nil // just in case osExit was monkey-patched
}
- err = p.Parse(flags())
- switch {
- case err == ErrHelp:
- p.writeHelpForSubcommand(stdout, p.lastCmd)
- osExit(0)
- case err == ErrVersion:
- fmt.Fprintln(stdout, p.version)
- osExit(0)
- case err != nil:
- p.failWithSubcommand(err.Error(), p.lastCmd)
- }
-
+ p.MustParse(flags())
return p
}
@@ -449,6 +438,20 @@ func (p *Parser) Parse(args []string) error {
return err
}
+func (p *Parser) MustParse(args []string) {
+ err := p.Parse(args)
+ switch {
+ case err == ErrHelp:
+ p.writeHelpForSubcommand(stdout, p.lastCmd)
+ osExit(0)
+ case err == ErrVersion:
+ fmt.Fprintln(stdout, p.version)
+ osExit(0)
+ case err != nil:
+ p.failWithSubcommand(err.Error(), p.lastCmd)
+ }
+}
+
// process environment vars for the given arguments
func (p *Parser) captureEnvVars(specs []*spec, wasPresent map[*spec]bool) error {
for _, spec := range specs {