argsparser

package module
v0.0.0-...-360eba8 Latest Latest
Warning

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

Go to latest
Published: May 21, 2020 License: MIT Imports: 2 Imported by: 0

README

argsparser

Install

go get github.com/AkvicorEdwards/argsparser

Examples

import (
    "github.com/AkvicorEdwards/argsparser"
)
 
    argsparser.Version = `v1.0`
    argsparser.Help = `
Usage: fi [option...] [arguments...]

Example: 

    fi port 7020

The commands are:

    port [port]    Set http listening port
`
	argsparser.AddBasicArg()
	argsparser.Add("port", 1, func(str []string) {
		config.Addr = str[1]
	})

	argsparser.Parse()

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var BasicArgs = make(Args)
View Source
var Help = "help"
View Source
var Version = "args parser 1.0.0"

Functions

func Add

func Add(arg string, size int, executor func([]string))

func AddBasicArg

func AddBasicArg()

func Parse

func Parse()
Example
// Clean up global variable
BasicArgs = make(Args)
// Simulate
os.Args = []string{"argsparser", "help", "version", "port",
	"1234", "unknown", "needmore", "a"}

AddBasicArg()
Add("port", 1, func(str []string) {
	fmt.Println(str[1])
})
Add("needmore", 2, func(str []string) {})
Parse()

BasicArgs = make(Args)
os.Args = []string{"argsparser", "port", "123"}
Add("port", 1, func(str []string) {
	fmt.Println(str[1])
})
Parse()
Output:
help
args parser 1.0.0
1234
Skipped: Unknown command [unknown]
The command [needmore] requires more arguments to execute
123

Types

type Arg

type Arg struct {
	// Number of parameters required
	// if Size=-1 All parameters that follow belong to this command
	Size int
	// Execute "command"
	Executor func([]string)
}

type Args

type Args map[string]Arg

Jump to

Keyboard shortcuts

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