args

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2023 License: MIT Imports: 3 Imported by: 2

README

args-parser

GoDoc

A simple arguments parser written in Go. Mainly here so I can easily use it in my other projects.

Register arguments

You can currently register options/flags, then check on if that flag was used and access its value if applicable.

args.Register(Argument{
        name: "arg",
        description: "My first argument",
})
Auto-generated usage information
args.PrintUsage()
Usage

Flags follow the UNIX rules of having one dash for single-letter versions of flags and double-dashed versions of flags with whole words. (e.g. -a --all). It doesn't technically matter though since it just trims dashes from the beginning of the argument.

Argument values proceed the flag with a = sign separating (e.g. -a=value --arg=value).

Then either check if the flag is being used or get its value.

args.Using("arg") // bool

args.Value("arg") // string

Does not yet support subcommands.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Args map[string]string

Args is a map of the args that were passed after the first arg with dash prefixes (e.g. -- or -) trimmed. A value is set for a member of Args if an arg is proceeded with an equality operator (e.g. --arg=value).

View Source
var CustomUsage string

CustomUsage allows you to add custom usage details. The value of CustomUsage is printed in between the Name of the binary and the flags in the usage message.

Functions

func PrintUsage

func PrintUsage()

PrintUsage prints a usage message based on the arguments and usage you have registered then exits.

func Register

func Register(arg Argument)

Register an Argument.

func Using

func Using(name string) bool

Using returns a boolean indicating if Argument Name was passed to your executable.

func Value

func Value(name string) string

Value returns a string of the value of Argument Name if passed to your executable.

Types

type Argument added in v1.1.0

type Argument struct {
	Name         string
	Short        string
	Description  string
	DefaultValue string
	Values       []string
	ExpectsValue bool
}

Jump to

Keyboard shortcuts

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