From 891f8623c1af232a0b09f80fc4a3354edfa3ec07 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Tue, 5 Jun 2018 21:31:14 +1000 Subject: [PATCH] Make kong.Parse() global more useful. --- global.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/global.go b/global.go index a9fd899..10aaa27 100644 --- a/global.go +++ b/global.go @@ -4,13 +4,38 @@ import ( "os" ) +// App is the default global instance. It is populated by Parse(). +var App *Kong + // Parse constructs a new parser and parses the default command-line. func Parse(cli interface{}, options ...Option) string { parser, err := New(cli, options...) if err != nil { panic(err) } + App = parser cmd, err := parser.Parse(os.Args[1:]) parser.FatalIfErrorf(err) return cmd } + +func FatalIfErrorf(err error, args ...interface{}) { + if App == nil { + panic("call kong.Parse() before using kong.FatalIfErrorf()") + } + App.FatalIfErrorf(err, args...) +} + +func Errorf(format string, args ...interface{}) { + if App == nil { + panic("call kong.Parse() before using kong.Errorf()") + } + App.Errorf(format, args...) +} + +func Printf(format string, args ...interface{}) { + if App == nil { + panic("call kong.Parse() before using kong.Printf()") + } + App.Printf(format, args...) +}