blob: 05528d8d28477f8092ec1603bb5ca27688eb0ed3 [file] [log] [blame]
package flags
import (
// ErrorType represents the type of error.
type ErrorType uint
const (
// ErrUnknown indicates a generic error.
ErrUnknown ErrorType = iota
// ErrExpectedArgument indicates that an argument was expected.
// ErrUnknownFlag indicates an unknown flag.
// ErrUnknownGroup indicates an unknown group.
// ErrMarshal indicates a marshalling error while converting values.
// ErrHelp indicates that the built-in help was shown (the error
// contains the help message).
// ErrNoArgumentForBool indicates that an argument was given for a
// boolean flag (which don't not take any arguments).
// ErrRequired indicates that a required flag was not provided.
// ErrShortNameTooLong indicates that a short flag name was specified,
// longer than one character.
// ErrDuplicatedFlag indicates that a short or long flag has been
// defined more than once
// ErrTag indicates an error while parsing flag tags.
// ErrCommandRequired indicates that a command was required but not
// specified
// ErrUnknownCommand indicates that an unknown command was specified.
// ErrInvalidChoice indicates an invalid option value which only allows
// a certain number of choices.
// ErrInvalidTag indicates an invalid tag or invalid use of an existing tag
func (e ErrorType) String() string {
switch e {
case ErrUnknown:
return "unknown"
case ErrExpectedArgument:
return "expected argument"
case ErrUnknownFlag:
return "unknown flag"
case ErrUnknownGroup:
return "unknown group"
case ErrMarshal:
return "marshal"
case ErrHelp:
return "help"
case ErrNoArgumentForBool:
return "no argument for bool"
case ErrRequired:
return "required"
case ErrShortNameTooLong:
return "short name too long"
case ErrDuplicatedFlag:
return "duplicated flag"
case ErrTag:
return "tag"
case ErrCommandRequired:
return "command required"
case ErrUnknownCommand:
return "unknown command"
case ErrInvalidChoice:
return "invalid choice"
case ErrInvalidTag:
return "invalid tag"
return "unrecognized error type"
// Error represents a parser error. The error returned from Parse is of this
// type. The error contains both a Type and Message.
type Error struct {
// The type of error
Type ErrorType
// The error message
Message string
// Error returns the error's message
func (e *Error) Error() string {
return e.Message
func newError(tp ErrorType, message string) *Error {
return &Error{
Type: tp,
Message: message,
func newErrorf(tp ErrorType, format string, args ...interface{}) *Error {
return newError(tp, fmt.Sprintf(format, args...))
func wrapError(err error) *Error {
ret, ok := err.(*Error)
if !ok {
return newError(ErrUnknown, err.Error())
return ret