multiflag

package module
v0.0.0-...-518d909 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2014 License: BSD-2-Clause Imports: 1 Imported by: 1

README

multiflag implements the flag.Value interface for handling repeated flag values.

It is useful for boolean flags where repeated use implies greater intensity or
values to be collected into an array.

To install, run

    go get github.com/gyepisam/multiflag

See the multiflag.go or main.go files for further instructions and examples.

Documentation

Overview

Package multiflag implements the flag.Value interface for handling repeated flag values.

It is useful for boolean flags where repeated use implies greater intensity or values to be collected into an array.

Usage:

Imports, definitions, etc:

  import (
	"flag"
	"github.com/gyepisam/multiflag"
  )

A boolean variable counts flags and does not consume any arguments.

var verbosity = multiflag.Bool("verbose", "false", "Verbosity. Repeat as necessary", "v")

String variables consume and collect their arguments into a string array.

var trace = multiflag.String("trace", "none", "Trace program sections", "t")

After calling

flag.Parse()

You can get the count

fmt.Println("Verbosity:", verbosity.NArg())

which, given the flags:

-v -v -verbose --verbose

produces the output:

Verbosity: 4

or the arguments

  for _, item := range trace.Args() {
	  fmt.Println("Tracing:", item)
  }

which, given the flags:

-t parse -trace compile

produces the output:

Tracing: parse
Tracing: compile

The examples above can be found in the file main/main.go, which can also be compiled and run. It has the following usage text:

  Usage of main:
	-t=none: Alias for trace
	-trace=none: Trace program sections
	-v=false: Alias for verbose
	-verbose=false: Verbosity. Repeat as necessary

multiflag also works with *flag.FlagSet instances. The previous example would require the following changes:

fs := flag.NewFlagSet("subcommand", flag.ContinueOnError)
var verbosity = multiflag.BoolSet(fs, "verbose", "false", "Verbosity. Repeat as necessary", "v")
var trace = multiflag.StringSet(fs, "trace", "none", "Trace program sections", "t")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AliasUsageFunc

type AliasUsageFunc func(orig, alias string) string

AliasUsageFunc specifies the signature for an alias usage function.

var AliasUsage AliasUsageFunc = func(orig, alias string) string {
	return "Alias for " + orig
}

AliasUsage returns the usage text for an alias. The function is a variable that may be changed to point to a custom function of type AliasUsageFunc.

type Flagger

type Flagger func(val flag.Value, name string, usage string)

type Value

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

Value counts and collects repeated uses of a flag.

func Bool

func Bool(name string, value string, usage string, aliases ...string) *Value

Bool returns a boolean multiflag instance associated with flag.. name, value, and usage are used to initial a flag.Value. aliases, if any, initialize aliases for name. See AliasUsage.

func BoolSet

func BoolSet(flg *flag.FlagSet, name string, value string, usage string, aliases ...string) *Value

BoolSet creates a boolean multiflag instance, associates it with the provided FlagSet and returns it.

func String

func String(name string, value string, usage string, aliases ...string) *Value

String returns a string multiflag instance associated with flag. name, value, and usage are used to initial a flag.Value. aliases, if any, initialize aliases for name. See AliasUsage.

func StringSet

func StringSet(flg *flag.FlagSet, name string, value string, usage string, aliases ...string) *Value

StringSet creates a string multiflag instance, associates it with the provided FlagSet and returns it.

func (*Value) Args

func (v *Value) Args() []string

Args returns an array of collected arguments. A Bool always returns an empty array.

func (*Value) IsBoolFlag

func (v *Value) IsBoolFlag() bool

IsBoolFlag returns a value denoting whether the variable represents a boolean value. Provided for flag package.

func (*Value) NArg

func (v *Value) NArg() int

NArg returns the number of invocations

func (*Value) Set

func (v *Value) Set(s string) error

Set records a usage instance. Provided for flag package.

func (*Value) String

func (v *Value) String() string

String produces a string representation. Provided for flag package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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