sflag

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2020 License: MIT Imports: 5 Imported by: 0

README

SFlag

简单的选项参数解析器,不依赖go内置的flag包。

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 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]interface{}

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 Arguments

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

Arguments definition

func (*Arguments) Add

func (ags *Arguments) Add(name, description string)

Add a new argument

type Option

type Option struct {
	Name  string
	Short string
	// Type value type. allow: int, string, bool, ints, strings, bools, custom
	Type string
	// Value of the option. allow: bool, string, slice(ints, strings)
	Value interface{}

	// refer flag.Value interface
	Setter func(string) error

	Required bool
	DefValue interface{}
	// Description
	Description string
}

Option is config info for a option Usage:

cmd.AddOpt(Option{
	Name: "name"
	Short: "n"
	DType: "string"
})

cmd.Flags.String()

func NewOpt

func NewOpt(name, description string) *Option

NewOpt create new option

func (*Option) StrVar

func (opt *Option) StrVar(s *string) *Option

StrVar binding

type Options

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

Options the values for parsed arguments and options

func (*Options) Bindings

func (r *Options) Bindings(longs, shorts map[string]interface{})

Bindings all parsed long and short option values to Option

func (*Options) Opt

func (r *Options) Opt(name string) *Option

Opt get

func (*Options) String

func (r *Options) String() string

String the options

func (*Options) VisitAll

func (r *Options) VisitAll()

VisitAll options

type ValueGetter

type ValueGetter struct {
	// value store parsed argument data. (type: string, []string)
	Value interface{}
	// is array
	IsArray 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