positional

package
v0.0.0-...-cffa139 Latest Latest
Warning

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

Go to latest
Published: May 29, 2015 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package positional implements parsing positional arguments, for example command line arguments, into fields of a struct.

Example
package main

import (
	"fmt"

	"bazil.org/bazil/cliutil/positional"
)

func main() {
	type Inventory struct {
		Item string
		positional.Optional
		Count int
	}

	porch := Inventory{}
	err := positional.Parse(&porch, []string{"cat", "3"})
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Printf("You have %d %s(s) on the porch\n", porch.Count, porch.Item)

	house := Inventory{
		Count: 1,
	}
	err = positional.Parse(&house, []string{"dog"})
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Printf("You have %d %s(s) in the house\n", house.Count, house.Item)

}
Output:

You have 3 cat(s) on the porch
You have 1 dog(s) in the house

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(args interface{}, list []string) error

Parse fills the fields of the struct pointed to by args with input from the given list.

Fields are handled based on their data type, for example strings are converted into integers when necessary.

In addition to built-in conversion rules, if a field implements Setter, the Set method is called to do the conversion. This interface is compatible with flag.Value.

To mark fields optional, insert an Optional marker in the struct after the last mandatory field.

To consume any number of arguments, use a slice as the last field.

Parse returns an error of type ErrMissingMandatoryArg if a mandatory field was not filled.

Parse returns an error of type ErrTooManyArgs if there are more arguments than (non-slice) fields.

func Usage

func Usage(args interface{}) string

Usage returns a string suitable for use in a command line synopsis.

Struct tags with the key "positional" can be used to control the usage message. The following struct tags are supported:

  • "metavar": meta variable name to use, defaults to field name in upper case
Example
package main

import (
	"fmt"

	"bazil.org/bazil/cliutil/positional"
)

func main() {
	var args struct {
		Op string `positional:",metavar=ACTION"`
		positional.Optional
		Path []string
	}
	usage := positional.Usage(&args)
	fmt.Printf("Usage: doit [-v] %s\n", usage)
}
Output:

Usage: doit [-v] ACTION [PATH..]

Types

type ErrMissingMandatoryArg

type ErrMissingMandatoryArg struct {
	Name string
}

ErrMissingMandatoryArg indicates that a mandatory argument is missing.

func (ErrMissingMandatoryArg) Error

func (e ErrMissingMandatoryArg) Error() string

type ErrTooManyArgs

type ErrTooManyArgs struct{}

ErrTooManyArgs indicates that there were too many arguments.

func (ErrTooManyArgs) Error

func (ErrTooManyArgs) Error() string

type Optional

type Optional struct{}

Optional is a marker for the point in the arguments struct where the rest of the fields are optional.

type Setter

type Setter interface {
	Set(string) error
}

Setter is an interface that can be implemented by fields that need to control their conversion from string to the data type.

Jump to

Keyboard shortcuts

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