Skip to content

Commit

Permalink
parse/args: --help is help request too
Browse files Browse the repository at this point in the history
  • Loading branch information
gobwas committed Sep 7, 2020
1 parent 96bae6b commit de4ead0
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions parse/pargs/posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit de4ead0

Please sign in to comment.