Skip to content

Commit

Permalink
Check for duplicate flags.
Browse files Browse the repository at this point in the history
Check that the short and long flag of each option has not been
defined before adding it to the group. Returns an ErrDuplicatedFlag
in case of error.
  • Loading branch information
Laurent Cozic committed Dec 12, 2013
1 parent 062143c commit abf4c5b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const (

// A short flag name is longer than one character
ErrShortNameTooLong

// A short or long flag has been defined more than once
ErrDuplicatedFlag
)

func (e ErrorType) String() string {
Expand All @@ -54,6 +57,8 @@ func (e ErrorType) String() string {
return "no argument for bool"
case ErrRequired:
return "required"
case ErrDuplicatedFlag:
return "duplicated flag"
}

return "unknown"
Expand Down
21 changes: 21 additions & 0 deletions group_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ func (g *Group) scanStruct(realval reflect.Value, sfield *reflect.StructField, h
value: realval.Field(i),
tag: mtag,
}

var existingOption *Option
if longname != "" {
existingOption = g.optionByName(longname, nil)
if existingOption != nil {
return newErrorf(ErrDuplicatedFlag,
"option `%s' uses the same long name as option `%s'",
option,
existingOption)
}
}

if short != 0 {
existingOption = g.optionByName(string(short), nil)
if existingOption != nil {
return newErrorf(ErrDuplicatedFlag,
"option `%s' uses the same short name as option `%s'",
option,
existingOption)
}
}

g.options = append(g.options, option)
}
Expand Down

0 comments on commit abf4c5b

Please sign in to comment.