argp

package
v0.0.0-...-d931726 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package argp is a stupid simple flag (argument) parser which allows to parse flags without panicing when non-registered flags are passed.

Index

Constants

This section is empty.

Variables

View Source
var (
	Scan   = singleton.Scan
	String = singleton.String
	Bool   = singleton.Bool
	Int    = singleton.Int
	Float  = singleton.Float
	Args   = singleton.Args
	Help   = singleton.Help
)

Functions

This section is empty.

Types

type Parser

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

Parser takes an array of arguments and provides functionalities to parse flags and values contained.

func New

func New(args ...[]string) (p *Parser)

New initializes a new instance of Parser.

It defaultly takes the value of os.Args[1:] as array of arguments. Optionally, you can pass a custom array of arguments you want to scan.

func (*Parser) Args

func (p *Parser) Args() []string

Args returns all other un-scanned arguments of the passed arguments array.

Example:

p := New([]string{"whats", "-n", "up"})
val, err := p.Bool("-n")
// val      = true
// err      = nil
// p.Args() = []string{"whats", "up"}

func (*Parser) Bool

func (p *Parser) Bool(flag string, def bool, help ...string) (val bool, err error)

Bool is shorthand for Scan with a bool flag value. If the flag was passed (with or wirhout value specified), true is returned. If the parsing fails, the error is returned. When def is passed and no flag was found, def is returned as val.

func (*Parser) Float

func (p *Parser) Float(flag string, def float64, help ...string) (val float64, err error)

Float is shorthand for Scan with a float flag value. It returns the scanned value and an error if the parsing failed. If no flag or value was found and a def value was passed, def is returned as val.

func (*Parser) Help

func (p *Parser) Help() string

Help returns a compiled string listing all scanned flag specifications.

func (*Parser) Int

func (p *Parser) Int(flag string, def int, help ...string) (val int, err error)

Int is shorthand for Scan with a integer flag value. It returns the scanned value and an error if the parsing failed. If no flag or value was found and a def value was passed, def is returned as val.

func (*Parser) Scan

func (p *Parser) Scan(flag string, val interface{}) (ok bool, err error)

Scan looks for the passed flag (unprefixed) in the arguments array. If the flag was found, the value of the flag is scanned into the pointer of val. If the flag and value was found and valid, true is returned. Otherwise, false is returned and if an error occurs, the error is returned as well.

Example:

var config string
p := argp.New([]string{"--config", "myconfig.yml"})
ok, err := p.Scan("--config", &config)
// config = "myconfig.yml"
// ok     = true
// err    = nil

func (*Parser) String

func (p *Parser) String(flag string, def string, help ...string) (val string, err error)

String is shorthand for Scan with a string flag value. It returns the scanned value and an error if the parsing failed. If no flag or value was found and a def value was passed, def is returned as val.

Jump to

Keyboard shortcuts

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