argparser

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2021 License: MIT Imports: 7 Imported by: 0

README

Arg Parser

====================================

A rich tool for parsing flags and values in pure Golang. No additional library is required and you can use everywhere.

Features

  • Supporting different formats:
	int (int64, int32, int16, int8)
	uint (uint64, uint32, uint16, uint8)
	string // (you can easily convert any type to string)
	bool // yes, on, true, 1 = true; no, off, false, 0 = false
  • Easily convert each type to another types. with only one functions.

  • Low time order! It's as fast as spliting two strings and then joining them together XD

  • Simple algorithms. You can easily read the algorithms used in this library, and you are allowed to copy and use them in your own projects (even if they are in Golang or not)!

  • No additional libs. You don't have to waste your time and storage for downloading another libraries and dependencies. Only this lib, will meet your needs.

  • Supports multi flags with the same name (Of course they have different indexes).

  • Rigorously tested. We are testing the lib in any possible ways. If you find out a bug, please create an issue on our issue tracker. We will check it and solve the problem


Examples:

parsing a command is a simple work!

const text = "/test --flag1 true --flag2 on" +
	" --flag3 false" +
	" --flag4 \"Hello, how are you?\"" +
	" --flag5 off\n\n\n\n\n\n" +
	" --flag6 123456789"

	//-----------------------------------------------------

	// parse it and get an *EventArgs value.
	args, err := argparser.ParseArg(boolTest)
	if err != nil {
		// if the format of the text is not correct,
		// you will get an error.
		// if you find out any bug here, please report us.
		// if the text doesn't contain ant command, you will
		// get error (it should start with command.)
		log.Fatal(err)
	}

	// check if it contains any flag with name `flag1` or not?
	if args.HasFlag("flag1", "anotherFlag") {
		// doesn't matter this
		log.Println("it has flag1 or anotherFlag")
	}

	// get a flag's value as bool.
	// if the flags are not present in EventArgs at all,
	// this method will return false.
	// if it's on, yes, true, 1 (one integer), it returns true
	// if it's off, no, false, 0, it returns false
	// if it's string, it will return true
	// if it doesn't have any value (empty string), it returns true
	// 
	b1 := args.GetAsBool("flag1")
	if b1 {
		log.Println("it's true!")
	} else {
		log.Println("it's false!")
	}

	s1 := args.GetAsString("flag4")
	log.Println(s1) // Hello, how are you?

	// lets try more than one flag this time.
	// this method will search for the flags you passed to it.
	// if the first flag doesn't exist, it will look
	// for another one.
	// if there are no such flags at all, it will return you
	// `0, false`
	// if the first flag exists but it can't be
	// converted to integer, it will give you `0, false`s
	i1, ok := args.GetAsInteger("flag6", "flag10")
	if !ok {
		log.Println("couldn't parse flag value to integer")
	} else {
		log.Println("oh yeah, the integer value is ", i1)
	}

need more examples? then take a look at example directory!


How to get started?

  • Download the library with the standard go get command:

go get github.com/ALiwoto/argparser

Support and Contributions

If you think you have found a bug or have a feature request, feel free to use our issue tracker. Before opening a new issue, please search to see if your problem has already been reported or not. Try to be as detailed as possible in your issue reports.

If you need help using argparser or have other questions we suggest you to join our telegram community. Please do not use the GitHub issue tracker for personal support requests.

Docs

Docs can be found here.

Be sure to read them carefuly!

License

The argparser project is under the MIT License. See the LICENSE file for more details.

Documentation

Index

Constants

View Source
const (
	NoneTypeStr   = "None"
	BoolTypeStr   = "bool"
	StringTypeStr = "string"
	UInt8TypeStr  = "uint8"
	UInt16TypeStr = "uint16"
	UInt32TypeStr = "uint32"
	UInt64TypeStr = "uint64"
	Int8TypeStr   = "int8"
	Int16TypeStr  = "int16"
	Int32TypeStr  = "int32"
	Int64TypeStr  = "int64"
)

flag type string values used in argparser library. Notice that you don't have to use them in order to determine the type, in fact they are for internal usages, but also publiced just-in-case that if a user wanted to compare them (or print them out), it won't be impossible.

View Source
const (
	TrueHlc  = "true"
	YesHlc   = "yes"
	OnHlc    = "on"
	FalseHlc = "false"
	NoHlc    = "no"
	OffHlc   = "off"
)

common and global hlcs.

Variables

This section is empty.

Functions

func ToBoolType

func ToBoolType(value string) (v, isBool bool)

Types

type EventArgs

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

EventArgs is our top-level struct. I'm planing to make an interface for it in future.

func ParseArg

func ParseArg(text string) (e *EventArgs, err error)

ParseArg will parse the whole text into an EventArg and will return it. It will return error if there is any, otherwise error will be nil.

func (*EventArgs) GetAsBool

func (e *EventArgs) GetAsBool(name ...string) bool

GetAsBool will give you the boolean value of the flag with the specified name. if there is no flag with this name, or there is an error on our path, it will return you false.

func (*EventArgs) GetAsInteger

func (e *EventArgs) GetAsInteger(name ...string) (vI int64, ok bool)

GetAsInteger will give you the integer value of the flag with the specified name. if there is no flag with this name, or there is an error on our path, it will return you zero and false.

func (*EventArgs) GetAsString

func (e *EventArgs) GetAsString(name ...string) string

GetAsString will give you the string value of the flag with the specified name. if there is no flag with this name, or there is an error on our path, it will return you an empty string.

func (*EventArgs) GetAsStringOrRaw

func (e *EventArgs) GetAsStringOrRaw(name ...string) string

GetAsStringOrRaw will give you the string value of the flag with the specified name. if there is no flag with this name, or there is an error on our path, it will return you the raw data in the EventArgs struct.

func (*EventArgs) GetAsStringT

func (e *EventArgs) GetAsStringT(name ...string) string

GetAsStringT's functionality is exactly like GetAsString, but it will also trim the spaces, so you can use a pure string. please consider that using this method is somehow dangerous and the usage is for places like toLang in wotoTranslation package.

func (*EventArgs) GetAsStringTOrRaw

func (e *EventArgs) GetAsStringTOrRaw(name ...string) string

GetAsStringTOrRaw's functionality is exactly like GetAsString, but it will also trim the spaces, so you can use a pure string. please consider that using this method is somehow dangerous and the usage is for places like toLang in wotoTranslation package. if the value is empty, it will return the raw data.

func (*EventArgs) GetCommand

func (e *EventArgs) GetCommand() string

---------------------------------------------------------

func (*EventArgs) GetFlag

func (e *EventArgs) GetFlag(names ...string) *Flag

func (*EventArgs) GetFlags

func (e *EventArgs) GetFlags() []Flag

func (*EventArgs) GetIndexFlag

func (e *EventArgs) GetIndexFlag(index int) *Flag

func (*EventArgs) GetLength

func (e *EventArgs) GetLength() int

func (*EventArgs) HasFlag

func (e *EventArgs) HasFlag(names ...string) bool

HasFlag will check if this EventArgs has at least one the provided flags or not. please notice that if you want to check if it has ALL of the flags, use `HasFlags` method.

func (*EventArgs) HasFlags

func (e *EventArgs) HasFlags(names ...string) bool

HasFlag will check if this EventArgs has ALL of the provided flags or not. please notice that if you want to check if it has at least one of the flags, use `HasFlag` method.

func (*EventArgs) IsEmpty

func (e *EventArgs) IsEmpty() bool

func (*EventArgs) IsEmptyOrRaw

func (e *EventArgs) IsEmptyOrRaw() bool

type Flag

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

Flag is the options passed along with the commands by users. they should send them with prefex "--", but we will remove them in the argparser.

func (*Flag) GetAsBool

func (f *Flag) GetAsBool() bool

func (*Flag) GetAsInteger

func (f *Flag) GetAsInteger() (vI int64, ok bool)

GetAsInteger will give the value of this flag as an int64 value. please notice that if it fails to convert the value to int64, it will return you zero and the second return value will be false.

func (*Flag) GetAsString

func (f *Flag) GetAsString() string

GetAsString will give you the value as an string. for example if value is `true` (with flag type of `bool`), then the string will be "true". or if it's 10(an integer), it will give you: "10".

func (*Flag) GetIndex

func (f *Flag) GetIndex() int

GetIndex will return the index of this flag.

func (*Flag) GetName

func (f *Flag) GetName() string

GetName will give you the name of this flag.

func (*Flag) GetType

func (f *Flag) GetType() FlagType

GetType will return the type of the value of this flag. please notice that if you don't enter any value for this flag, it will be considered as `true` (which is type bool).

func (*Flag) GetValue

func (f *Flag) GetValue() interface{}

GetValue will return you the value of this flag. remember that value can be

func (*Flag) GetValueAndType

func (f *Flag) GetValueAndType() (interface{}, FlagType)

type FlagType

type FlagType uint8
const (
	NoneFlagType   FlagType = iota
	BoolFlagType   FlagType = iota + 1
	StringFlagType FlagType = iota + 2
	UInt8FlagType  FlagType = iota + 3
	UInt16FlagType FlagType = iota + 4
	UInt32FlagType FlagType = iota + 5
	UInt64FlagType FlagType = iota + 6
	Int8FlagType   FlagType = iota + 7
	Int16FlagType  FlagType = iota + 8
	Int32FlagType  FlagType = iota + 9
	Int64FlagType  FlagType = iota + 10
)

falg types used in argparser library

func (*FlagType) Compare

func (t *FlagType) Compare(value *FlagType) bool

func (*FlagType) IsInteger

func (t *FlagType) IsInteger() bool

func (*FlagType) ToString

func (t *FlagType) ToString() string

type Hlc

type Hlc struct {
	Name  string
	Value interface{}
	Type  FlagType
}

High-level constant.

type HlcCollection

type HlcCollection []Hlc

func GetHlcCollection

func GetHlcCollection(h ...Hlc) HlcCollection

func (*HlcCollection) Contains

func (c *HlcCollection) Contains(name string) bool

func (*HlcCollection) GetValue

func (c *HlcCollection) GetValue(name string) (interface{}, FlagType)

Directories

Path Synopsis
examples
bools command

Jump to

Keyboard shortcuts

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