From de4ead0713173fd501f036c5e9b98e73b1a0954f Mon Sep 17 00:00:00 2001 From: Sergey Kamardin Date: Mon, 7 Sep 2020 14:13:34 +0300 Subject: [PATCH] parse/args: --help is help request too --- parse/pargs/posix.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/parse/pargs/posix.go b/parse/pargs/posix.go index 2626868..28d1fb0 100644 --- a/parse/pargs/posix.go +++ b/parse/pargs/posix.go @@ -40,12 +40,15 @@ func (p *Parser) Parse(_ context.Context, fs parse.FlagSet) (err error) { for p.next() { p.pairs(func(name, value string) bool { name = p.resolve(name) - // Special case for help request. - if fs.Lookup(name) == nil && (name == "help" || name == "h") { + + _, isHelp := lookup(fs, name) + if isHelp { err = flag.ErrHelp return false } + err = fs.Set(name, value) + return err == nil }) if err != nil { @@ -124,11 +127,16 @@ func (p *Parser) reset(fs parse.FlagSet) { } } +func lookup(fs parse.FlagSet, name string) (f *flag.Flag, isHelp bool) { + f = fs.Lookup(name) + isHelp = f == nil && (name == "h" || name == "help") + return +} + func (p *Parser) isBoolFlag(name string) bool { name = p.resolve(name) - f := p.fs.Lookup(name) - if f == nil && name == "h" { - // Special case for help message request. + f, isHelp := lookup(p.fs, name) + if isHelp { return true } if f == nil {