argparser

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2021 License: MIT Imports: 4 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"
)
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
}

func ParseArg

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

ParseArg will parse the whole text into an EventArg and will return it.

func (*EventArgs) CheckCommand added in v1.0.1

func (e *EventArgs) CheckCommand(cmd string) bool

CheckCommand will compare the command of this event arg with the given command. This function is case insensitive, if you want to compare it case sensitive, then use CompareCommand method.

func (*EventArgs) CompareCommand added in v1.0.1

func (e *EventArgs) CompareCommand(cmd string) bool

CompareCommand will compare the command of this event arg with the given command. This function is case sensitive, if you want to compare it case insensitive, then use CheckCommand method.

func (*EventArgs) GetAsBool

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

GetAsString 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)

GetAsString 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) GetAsIntegerOrRaw added in v1.0.1

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

GetAsString 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) 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

func (*EventArgs) HasFlags

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

func (*EventArgs) HasRawData added in v1.0.1

func (e *EventArgs) HasRawData() bool

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 pTools.

func (*Flag) GetAsBool

func (f *Flag) GetAsBool() bool

func (*Flag) GetAsInteger

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

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 you the index of this flag. index of a flag is a unique int. even if the name of two flags are the same, their index will not be the same.

func (*Flag) GetName

func (f *Flag) GetName() string

GetName will give you the name of this flag. it's not unique actually. for example:

`/command --test "hello" --test = "HI!"

func (*Flag) GetType

func (f *Flag) GetType() FlagType

GetType will return you the type of the value this flag. it's an enum.

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)

GetValueAndType returns both value and type of this flag. originally it has internal usage, but maybe you want to use it in your own package, so I made it public!

type FlagType

type FlagType uint8
const (
	NoneFlagType FlagType = iota
	BoolFlagType
	StringFlagType
	UInt8FlagType
	UInt16FlagType
	UInt32FlagType
	UInt64FlagType
	Int8FlagType
	Int16FlagType
	Int32FlagType
	Int64FlagType
)

func (*FlagType) Compare

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

func (*FlagType) IsInteger

func (t *FlagType) IsInteger() bool

func (*FlagType) IsString added in v1.0.1

func (t *FlagType) IsString() 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