Add Decode() method to Value to DRY.

This commit is contained in:
Alec Thomas
2018-05-17 23:50:12 +10:00
parent 31fe51f9d8
commit f929749094
4 changed files with 30 additions and 11 deletions
+7 -2
View File
@@ -2,6 +2,7 @@ package kong
import (
"strconv"
"strings"
)
//go:generate stringer -type=TokenType
@@ -48,8 +49,12 @@ func (t Token) IsAny(types ...TokenType) bool {
return false
}
// IsValue returns true if token is usable as a parseable value.
//
// A parseable value is either a value typed token, or an untyped token NOT starting with a hyphen.
func (t Token) IsValue() bool {
return t.IsAny(FlagValueToken, ShortFlagTailToken, PositionalArgumentToken, UntypedToken)
return t.IsAny(FlagValueToken, ShortFlagTailToken, PositionalArgumentToken) ||
(t.Type == UntypedToken && !strings.HasPrefix(t.Value, "-"))
}
type Scanner struct {
@@ -77,7 +82,7 @@ func (s *Scanner) Pop() Token {
return arg
}
// PopValue or panic with Error.
// PopValue token, or panic with Error.
func (s *Scanner) PopValue(context string) string {
t := s.Pop()
if !t.IsValue() {