args

package
v0.0.0-...-3de72e3 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FlagResult

type FlagResult struct {
	Args          []string
	NormalizedKey string
	OriginalKey   string
	Values        []string
	Joined        bool
}

FlagResult is a result of Scanner's Next operation

type PrefixOption

type PrefixOption struct {
	Prefix  string
	NumArgs int
}

PrefixOption is option for joined flags.

type Scanner

type Scanner struct {
	// Args is remaining arguments.
	Args []string

	// Flags are map keyed by -flag and number of argments for the flag.
	// The flag requires additional value from args if value > 0.
	// The flag doesn't require additional value from args if value == 0
	Flags map[string]int

	// Joined are prefixes of flag that has value in the same arg.
	// sorted Prefixes by reverse lexicographic order.
	Joined []PrefixOption

	// flag normalization.
	Normalized map[string]string

	// PrevResult is the previous Flag Result that has been handled.
	PrevResult FlagResult

	// CurResult is the current Flag Result being processed.
	CurResult FlagResult
}

Scanner scans command line arguments. it scans args and gets (flag, args, value) in each Next call.

for `-flagname`, it is recognized as flag by default, and it returns ("-flagname", ["-flagname"], nil).

for `-flagnameflagvalue`, need to set "-flagname" in joined, and it returns ("-flagname", ["-flagnameflagvalue"], ["flagvalue"]).

for `-flagname flagvalue`, need to set flags["-flagname"] = 1, and it returns ("-flagname", ["-flagname", "flagvalue"], ["flagvalue"]).

for non-flag "parameter", it returns ("", ["parameter"], nil).

if "-flagname" is used for both `-flagnameflagvalue`, `-flagname flagvalue`, need to set it both in joined and flags.

to accept "/flagname", need to use "/flagname" for flags, joined.

for `--flagname`, need to use "--flagname" for flags, joined.

func (*Scanner) HasNext

func (s *Scanner) HasNext() bool

HasNext returns true if there is more args to process.

func (*Scanner) LookAhead

func (s *Scanner) LookAhead() string

LookAhead looks ahead next flag.

func (*Scanner) Next

func (s *Scanner) Next() (string, []string, []string, bool)

Next returns next flag. flag is normalized flag, empty string if not started with "-". args are consumed arguments. values are flag value if flag needs value (next arg in args) or joined (rest after prefix in the arg).

func (*Scanner) ReadNextFlag

func (s *Scanner) ReadNextFlag() *FlagResult

ReadNextFlag moves forward the current FlagResult, update the scanner's PrevResult, and CurResult fields, and return the CurResult. NormalizedKey is normalized flag, or empty string if not started with "-". OriginalKey is a key before the normalization Args are consumed arguments. Values are flag value if flag needs value (next arg in args) or Joined (rest after prefix in the arg).

Jump to

Keyboard shortcuts

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