sflag

package
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package sflag is an simple cli flag parse tool

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArgsParser

type ArgsParser struct {
	// BoolOpts bool option names. list all bool value options.
	// eg. "--debug -h" -> []string{"debug", "h"}
	BoolOpts []string

	// ArrayOpts array option names. list all array value options.
	// eg. "--name tom --name john" should add []string{"name"}
	ArrayOpts []string
	// contains filtered or unexported fields
}

ArgsParser definition. a simple command line args parser

func NewArgsParser

func NewArgsParser(boolOpts []string, arrayOpts []string) *ArgsParser

NewArgsParser create

func ParseArgs

func ParseArgs(args []string, boolOpts []string, arrayOpts []string) *ArgsParser

ParseArgs parse os.Args to options.

func (*ArgsParser) Args

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

Args get parsed args

func (*ArgsParser) Opts

func (p *ArgsParser) Opts() map[string]any

Opts get parsed opts

func (*ArgsParser) OptsString

func (p *ArgsParser) OptsString() string

OptsString convert all options to string

func (*ArgsParser) Parse

func (p *ArgsParser) Parse(args []string)

Parse args list to options

Supports options format:

-e  // bool, short option
-e <value> // short option
-e=<value>
-aux // multi short bool options
--bool-opt // bool, lang option
--long-opt <value> // lang option
--long-opt=<value>

type ValueGetter

type ValueGetter struct {
	// value store parsed argument data. (type: string, []string)
	Value any
	// is array
	Arrayed bool
}

ValueGetter struct

func (*ValueGetter) Array

func (v *ValueGetter) Array() (ss []string)

Array alias of the Strings()

func (*ValueGetter) HasValue

func (v *ValueGetter) HasValue() bool

HasValue value is empty

func (*ValueGetter) Int

func (v *ValueGetter) Int(defVal ...int) int

Int argument value to int

func (*ValueGetter) Ints

func (v *ValueGetter) Ints() (ints []int)

Ints value to int slice

func (*ValueGetter) String

func (v *ValueGetter) String(defVal ...string) string

String argument value to string

func (*ValueGetter) Strings

func (v *ValueGetter) Strings() (ss []string)

Strings value to string slice, if argument isArray = true.

Jump to

Keyboard shortcuts

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