parse

package
v1.9.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidPosition = errors.New("invalid position")

ErrInvalidPosition is an error that occurs when an invalid position is accessed

Functions

func Dependency added in v1.8.4

func Dependency(input string) (string, []string, error)

Dependency parses a single dependency entry using the same dependency format rules as Dependencies.

func InferFieldType added in v1.8.9

func InferFieldType(field interface{}) types.OptionType

func LegacyUnmarshalTagFormat added in v1.8.9

func LegacyUnmarshalTagFormat(field reflect.StructField) (*types.TagConfig, error)

func PatternValue added in v1.8.4

func PatternValue(input string) (*types.PatternValue, error)

PatternValue parses a pattern value in format {pattern:xyz,desc:abc}

Escape sequences:

  • Special characters: \, -> , (comma) \: -> : (colon) \{ -> { (left brace) \} -> } (right brace) \ -> ' ' (space)

  • Quotes: \" -> " (double quote) \' -> ' (single quote)

  • Backslashes: \\ -> \ (single backslash) \\\ -> \\ (escaped backslash followed by char)

Examples:

{pattern:a\,b,desc:Values a\, b}     -> pattern="a,b" desc="Values a, b"
{pattern:C:\\Windows,desc:Path}      -> pattern="C:\Windows" desc="Path"
{pattern:\w+\:\d+,desc:Key\: Value}  -> pattern="w+:d+" desc="Key: Value"

func PatternValues added in v1.8.4

func PatternValues(input string) ([]types.PatternValue, error)

PatternValues parses multiple pattern values

func Split

func Split(s string) ([]string, error)

Split splits a command string into arguments

func TypeOfFlagFromString added in v1.8.9

func TypeOfFlagFromString(s string) types.OptionType

TypeOfFlagFromString converts a string to a types.OptionType

func UnmarshalTagFormat added in v1.8.9

func UnmarshalTagFormat(tag string, field reflect.StructField) (*types.TagConfig, error)

Types

type DefaultState added in v1.6.0

type DefaultState struct {
	// contains filtered or unexported fields
}

DefaultState is the default implementation of the State interface

func NewState added in v1.6.0

func NewState(args []string) *DefaultState

NewState creates a new State instance with the given argument list

func (*DefaultState) Advance added in v1.6.0

func (s *DefaultState) Advance() bool

Advance advances to the next argument, returning true if successful

func (*DefaultState) ArgAt added in v1.6.0

func (s *DefaultState) ArgAt(pos int) (string, error)

ArgAt returns the argument at a specific position

func (*DefaultState) Args added in v1.6.0

func (s *DefaultState) Args() []string

Args returns the entire argument list

func (*DefaultState) CurrentArg added in v1.6.0

func (s *DefaultState) CurrentArg() string

CurrentArg returns the current argument

func (*DefaultState) CurrentPos added in v1.6.0

func (s *DefaultState) CurrentPos() int

CurrentPos returns the current position in the argument list

func (*DefaultState) InsertArgsAt added in v1.6.0

func (s *DefaultState) InsertArgsAt(pos int, newArgs ...string)

InsertArgsAt inserts new arguments at a specific position

func (*DefaultState) Len added in v1.6.0

func (s *DefaultState) Len() int

Len returns the length of the argument list

func (*DefaultState) Peek added in v1.6.0

func (s *DefaultState) Peek() string

Peek returns the next argument without advancing the current position

func (*DefaultState) ReplaceArgs added in v1.6.0

func (s *DefaultState) ReplaceArgs(newArgs ...string)

ReplaceArgs replaces the entire argument list with new arguments

func (*DefaultState) SetPos added in v1.6.0

func (s *DefaultState) SetPos(pos int)

SetPos sets the current position in the argument list

func (*DefaultState) SkipCurrent added in v1.6.0

func (s *DefaultState) SkipCurrent()

SkipCurrent advances the current position to the next argument

type DependencyMap added in v1.8.4

type DependencyMap map[string][]string

DependencyMap maps flag names to their allowed values empty slice means any value is acceptable

func Dependencies added in v1.8.4

func Dependencies(input string) (DependencyMap, error)

Dependencies parses multiple dependency entries in format {flag:a,value:1},{flag:b,values:[1,2]} using the dependency format rules defined above.

type PositionData added in v1.9.0

type PositionData struct {
	Index int // Sequential index for positional argument
}

PositionData represents a parsed position configuration

func Position added in v1.9.0

func Position(input string) (*PositionData, error)

Position parses a position tag value in both formats: - New format: "N" (just the number) - Legacy format: "{idx:N}"

type State added in v1.6.0

type State interface {
	CurrentPos() int                         // Get the current position
	SetPos(pos int)                          // Set the current position
	SkipCurrent()                            // Skip the current argument
	Args() []string                          // Get the entire argument list
	InsertArgsAt(pos int, newArgs ...string) // Insert new arguments at a specific position
	ReplaceArgs(newArgs ...string)           // Replace the entire argument list
	CurrentArg() string                      // Get the current argument
	Peek() string                            // Peek at the next argument
	Advance() bool                           // New method for advancing to the next argument
	Len() int                                // Gets the length of the argument list
}

State represents the current state of the argument parser

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL