⬆️ Update dependencies
This commit is contained in:
+32
-13
@@ -41,7 +41,7 @@ func (t TokenType) String() string {
|
||||
|
||||
// Token created by Scanner.
|
||||
type Token struct {
|
||||
Value interface{}
|
||||
Value any
|
||||
Type TokenType
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ func (t Token) InferredType() TokenType {
|
||||
return t.Type
|
||||
}
|
||||
if v, ok := t.Value.(string); ok {
|
||||
if strings.HasPrefix(v, "--") { // nolint: gocritic
|
||||
if strings.HasPrefix(v, "--") { //nolint: gocritic
|
||||
return FlagToken
|
||||
} else if v == "-" {
|
||||
return PositionalArgumentToken
|
||||
@@ -109,18 +109,24 @@ func (t Token) IsValue() bool {
|
||||
//
|
||||
// For example, the token "--foo=bar" will be split into the following by the parser:
|
||||
//
|
||||
// [{FlagToken, "foo"}, {FlagValueToken, "bar"}]
|
||||
// [{FlagToken, "foo"}, {FlagValueToken, "bar"}]
|
||||
type Scanner struct {
|
||||
args []Token
|
||||
allowHyphenated bool
|
||||
args []Token
|
||||
}
|
||||
|
||||
// ScanAsType creates a new Scanner from args with the given type.
|
||||
func ScanAsType(ttype TokenType, args ...string) *Scanner {
|
||||
s := &Scanner{}
|
||||
for _, arg := range args {
|
||||
s.args = append(s.args, Token{Value: arg, Type: ttype})
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Scan creates a new Scanner from args with untyped tokens.
|
||||
func Scan(args ...string) *Scanner {
|
||||
s := &Scanner{}
|
||||
for _, arg := range args {
|
||||
s.args = append(s.args, Token{Value: arg})
|
||||
}
|
||||
return s
|
||||
return ScanAsType(UntypedToken, args...)
|
||||
}
|
||||
|
||||
// ScanFromTokens creates a new Scanner from a slice of tokens.
|
||||
@@ -128,6 +134,14 @@ func ScanFromTokens(tokens ...Token) *Scanner {
|
||||
return &Scanner{args: tokens}
|
||||
}
|
||||
|
||||
// AllowHyphenPrefixedParameters enables or disables hyphen-prefixed flag parameters on this Scanner.
|
||||
//
|
||||
// Disabled by default.
|
||||
func (s *Scanner) AllowHyphenPrefixedParameters(enable bool) *Scanner {
|
||||
s.allowHyphenated = enable
|
||||
return s
|
||||
}
|
||||
|
||||
// Len returns the number of input arguments.
|
||||
func (s *Scanner) Len() int {
|
||||
return len(s.args)
|
||||
@@ -157,7 +171,7 @@ func (e *expectedError) Error() string {
|
||||
// "context" is used to assist the user if the value can not be popped, eg. "expected <context> value but got <type>"
|
||||
func (s *Scanner) PopValue(context string) (Token, error) {
|
||||
t := s.Pop()
|
||||
if !t.IsValue() {
|
||||
if !s.allowHyphenated && !t.IsValue() {
|
||||
return t, &expectedError{context, t}
|
||||
}
|
||||
return t, nil
|
||||
@@ -166,7 +180,7 @@ func (s *Scanner) PopValue(context string) (Token, error) {
|
||||
// PopValueInto pops a value token into target or returns an error.
|
||||
//
|
||||
// "context" is used to assist the user if the value can not be popped, eg. "expected <context> value but got <type>"
|
||||
func (s *Scanner) PopValueInto(context string, target interface{}) error {
|
||||
func (s *Scanner) PopValueInto(context string, target any) error {
|
||||
t, err := s.PopValue(context)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -198,14 +212,19 @@ func (s *Scanner) Peek() Token {
|
||||
return s.args[0]
|
||||
}
|
||||
|
||||
// PeekAll remaining tokens
|
||||
func (s *Scanner) PeekAll() []Token {
|
||||
return s.args
|
||||
}
|
||||
|
||||
// Push an untyped Token onto the front of the Scanner.
|
||||
func (s *Scanner) Push(arg interface{}) *Scanner {
|
||||
func (s *Scanner) Push(arg any) *Scanner {
|
||||
s.PushToken(Token{Value: arg})
|
||||
return s
|
||||
}
|
||||
|
||||
// PushTyped pushes a typed token onto the front of the Scanner.
|
||||
func (s *Scanner) PushTyped(arg interface{}, typ TokenType) *Scanner {
|
||||
func (s *Scanner) PushTyped(arg any, typ TokenType) *Scanner {
|
||||
s.PushToken(Token{Value: arg, Type: typ})
|
||||
return s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user