stoker

package
v0.0.1-alpha Latest Latest
Warning

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

Go to latest
Published: May 3, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package stocker is for specialized argument parsing.

A list of string tokens are processed in order. When a token matches a defined Flag, the following tokens are captured into a TokenList until a new Flag token is found, or the end of input.

Each Flag has an associated TokenListHandler which is called once for each TokenList after parsing is complete and the HandleAll method is called. A TokenListHandler additionally takes a context object passed with HandleAll.

Example

Given the flags: --foo and --bar And the input: one two --foo three four --bar --five

  • Token one and two are ignored.
  • Token three and four are placed in a TokenList for the --foo Flag
  • Token --five is placed in a TokenList for the --bar Flag

Parser

A set of flags is used to create a Parser instance. The flags in a single Parser must have the same type for the TokenListHandler context object. Calling Parse returns a FlagHandlerList. HandleAll can be called on the list to call each handler in the order the flags where found during parsing.

Example

``` context := new(ExampleContext)

common_handler := func(context *ExampleContext, tl stoker.TokenList) error {
  return nil
}

parser := stoker.NewParser[*ExampleContext](

stoker.NewFlag("--foo", common_handler),
stoker.NewFlag("--bar", common_handler)

)

handlers := parser.Parse(os.Args...)

if err := handlers.HandleAll(context); err := nil {
  log.Fatal(err)
}

```

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFlag

func NewFlag[Context any](name string, handler TokenListHandler[Context]) *flag[Context]

func NewParser

func NewParser[Context any](flags ...Flag[Context]) *parser[Context]

Types

type Flag

type Flag[Context any] interface {
	Name() string
	HandleTokens(context Context, tokens TokenList) error
}

type FlagHandler

type FlagHandler[Context any] struct {
	// contains filtered or unexported fields
}

func (FlagHandler[Context]) Handle

func (fh FlagHandler[Context]) Handle(context Context) error

type FlagHandlerList

type FlagHandlerList[Context any] []FlagHandler[Context]

func (FlagHandlerList[Context]) HandleAll

func (fhl FlagHandlerList[Context]) HandleAll(context Context) error

type FlagList

type FlagList[Context any] []Flag[Context]

func (FlagList[Context]) FindByName

func (fl FlagList[Context]) FindByName(name string) (Flag[Context], bool)

type TokenList

type TokenList []string

type TokenListHandler

type TokenListHandler[Context any] func(context Context, tokens TokenList) error

Jump to

Keyboard shortcuts

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