func (p *ParseContext) Next() *Token {
	if len(p.peek) > 0 {
		return p.pop()
	}
	if len(p.args) == 0 {
		return &Token{Index: p.argi, Type: TokenEOL}
	}
	arg := p.args[0]
	p.next()
	if p.argsOnly {
		return &Token{p.argi, TokenArg, arg}
	}
	if arg == "--" {
		p.argsOnly = true
		return p.Next()
	}
	if strings.HasPrefix(arg, "--") {
		parts := strings.SplitN(arg[2:], "=", 2)
		token := &Token{p.argi, TokenLong, parts[0]}
		if len(parts) == 2 {
			p.Push(&Token{p.argi, TokenArg, parts[1]})
		}
		return token
	}
	if strings.HasPrefix(arg, "-") {
		if len(arg) == 1 {
			return &Token{Index: p.argi, Type: TokenShort}
		}
		shortRune, size := utf8.DecodeRuneInString(arg[1:])
		short := string(shortRune)
		flag, ok := p.flags.short[short]
		if !ok {
		} else if fb, ok := flag.value.(boolFlag); ok && fb.IsBoolFlag() {
		} else {
			token := &Token{p.argi, TokenShort, short}
			if len(arg) > size+1 {
				p.Push(&Token{p.argi, TokenArg, arg[size+1:]})
			}
			return token
		}
		if len(arg) > size+1 {
			p.args = append([]string{"-" + arg[size+1:]}, p.args...)
		}
		return &Token{p.argi, TokenShort, short}
	} else if strings.HasPrefix(arg, "@") {
		expanded, err := ExpandArgsFromFile(arg[1:])
		if err != nil {
			return &Token{p.argi, TokenError, err.Error()}
		}
		if len(p.args) == 0 {
			p.args = expanded
		} else {
			p.args = append(expanded, p.args...)
		}
		return p.Next()
	}
	return &Token{p.argi, TokenArg, arg}
}
Next token in the parse context.
package kingpin
import (
	"bufio"
	"fmt"
	"os"
	"strings"
	"unicode/utf8"
)
type TokenType int
const (
	TokenShort TokenType = iota
	TokenLong
	TokenArg
	TokenError
	TokenEOL
)
func (t TokenType) String() string {
	switch t {
	case TokenShort:
		return "short flag"
	case TokenLong:
		return "long flag"
	case TokenArg:
		return "argument"
	case TokenError:
		return "error"
	case TokenEOL:
		return "<EOL>"
	}
	return "?"
}
var (
	TokenEOLMarker = Token{-1, TokenEOL, ""}
)
type Token struct {
	Index int
	Type  TokenType
	Value string
}
func (t *Token) Equal(o *Token) bool {
	return t.Index == o.Index
}
func (t *Token) IsFlag() bool {
	return t.Type == TokenShort || t.Type == TokenLong
}
func (t *Token) IsEOF() bool {
	return t.Type == TokenEOL
}
func (t *Token) String() string {
	switch t.Type {
	case TokenShort:
		return "-" + t.Value
	case TokenLong:
		return "--" + t.Value
	case TokenArg:
		return t.Value
	case TokenError:
		return "error: " + t.Value
	case TokenEOL:
		return "<EOL>"
	default:
		panic("unhandled type")
	}
}
type ParseElement struct {
	Clause interface{}
	Value *string
}
type ParseContext struct {
	SelectedCommand *CmdClause
	ignoreDefault   bool
	argsOnly        bool
	peek            []*Token
	argi            int  
	args            []string
	rawArgs         []string
	flags           *flagGroup
	arguments       *argGroup
	argumenti       int  
	Elements []*ParseElement
}
func (p *ParseContext) nextArg() *ArgClause {
	if p.argumenti >= len(p.arguments.args) {
		return nil
	}
	arg := p.arguments.args[p.argumenti]
	if !arg.consumesRemainder() {
		p.argumenti++
	}
	return arg
}
func (p *ParseContext) next() {
	p.argi++
	p.args = p.args[1:]
}
func (p *ParseContext) HasTrailingArgs() bool {
	return len(p.args) > 0
}
func tokenize(args []string, ignoreDefault bool) *ParseContext {
	return &ParseContext{
		ignoreDefault: ignoreDefault,
		args:          args,
		rawArgs:       args,
		flags:         newFlagGroup(),
		arguments:     newArgGroup(),
	}
}
func (p *ParseContext) mergeFlags(flags *flagGroup) {
	for _, flag := range flags.flagOrder {
		if flag.shorthand != 0 {
			p.flags.short[string(flag.shorthand)] = flag
		}
		p.flags.long[flag.name] = flag
		p.flags.flagOrder = append(p.flags.flagOrder, flag)
	}
}
func (p *ParseContext) mergeArgs(args *argGroup) {
	for _, arg := range args.args {
		p.arguments.args = append(p.arguments.args, arg)
	}
}
func (p *ParseContext) EOL() bool {
	return p.Peek().Type == TokenEOL
}
func (p *ParseContext) Error() bool {
	return p.Peek().Type == TokenError
}