flag9

package module
v0.0.0-...-90aeb39 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2013 License: BSD-2-Clause Imports: 3 Imported by: 0

README

flag9

flag9 provides Plan9-like flag parsing for the Go language.

Installation

If you have your GOPATH set up, you can just run:

go get github.com/guelfey/flag9

Documentation

See the package documentation on godoc.org.

License

flag9 is licensed under a modified BSD license. See the LICENSE file for the full license text.

Documentation

Overview

Package flag9 provides Plan 9-like flag parsing.

Though it is not as convenient to use as the flag package from the standard library, it provides more control over what portion of the arguments is actually parsed.

See the example for a typical usage case.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Argc

func Argc() rune

Argc returns the current option character from the command-line arguments.

func Argf

func Argf() (string, bool)

Argf tries to return the current option argument (the rest of the option string if it's not empty, or the next argument) from the command-line arguments. If none is present, the empty string and false are returned. Otherwise, the option argument and true are returned.

It must not be called multiple times for the same argument.

func Argv

func Argv() []string

Argv returns the command-line arguments that are not (yet) processed.

func Eargf

func Eargf(f func()) string

Eargf behaves like Argf, but runs the given function and panics afterwards if no option argument is present.

func Next

func Next() bool

Next tries to read the next option character from the command-line arguments. If it is successfull, it returns true and Argc will return the option character. Otherwise, it returns false.

Types

type Args

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

Args are a set of arguments being parsed.

Example
s := []string{"-ab", "-carg", "--", "-d"}
args := NewArgs(s)
for args.Next() {
	switch args.Argc() {
	case 'a', 'b':
		fmt.Printf("%c\n", args.Argc())
	case 'c':
		argf, ok := args.Argf()
		if ok {
			fmt.Println("c", argf)
		} else {
			fmt.Println("c without argument")
		}
	case 'd':
		fmt.Println("not reached")
	}
}
fmt.Println(args.Argv())
Output:

a
b
c arg
[-d]

func NewArgs

func NewArgs(s []string) *Args

NewArgs returns an Args structure that can be used to parse the given slice of strings.

func (*Args) Argc

func (a *Args) Argc() rune

Argc returns the current option character.

func (*Args) Argf

func (a *Args) Argf() (string, bool)

Argf tries to return the current option argument (the rest of the option string if it's not empty, or the next member in the slice). If none is present, the empty string and false are returned. Otherwise, the option argument and true are returned.

It must not be called multiple times for the same argument.

func (*Args) Argv

func (a *Args) Argv() []string

Argv returns the arguments that are not (yet) processed.

func (*Args) Eargf

func (a *Args) Eargf(f func()) string

Eargf behaves like Argf, but runs the given function and panics afterwards if no option argument is present.

func (*Args) Next

func (a *Args) Next() bool

Next tries to read the next option character. If it is successfull, it returns true and Argc will return the option character. Otherwise, it returns false.

Jump to

Keyboard shortcuts

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