flags

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2015 License: MIT Imports: 5 Imported by: 13

README

Flags GoDoc Build Status

Flags is a low level package for parsing or managing single flag arguments and their associated values from a list of arguments. It's useful for CLI applications or creating logic for parsing arguments(custom or os.Args) manually.

Note that there is no context available for flags. You need to know upfront how flags are supposed to be parsed.

Checkout the usage below for examples:

Install

go get github.com/fatih/flags

Usage and examples

Let us define three flags. Flags needs to be compatible with the flag package.

args := []string{"--key", "123", "--name=example", "--debug"}

Check if a flag exists in the argument list

flags.Has("key", args)    // true
flags.Has("--key", args)  // true
flags.Has("secret", args) // false

Get the value for from a flag name

val, _ := flags.Value("--key", args) // val -> "123"
val, _ := flags.Value("name", args)  // val -> "example"
val, _ := flags.Value("debug", args) // val -> "" (means true boolean)

Exclude a flag and it's value from the argument list

rArgs := flags.Exclude("key", args)  // rArgs -> ["--name=example", "--debug"]
rArgs := flags.Exclude("name", args) // rArgs -> ["--key", "123", "--debug"]
rArgs := flags.Exclude("foo", args)  // rArgs -> ["--key", "123", "--name=example "--debug"]

Is a flag in its valid representation (compatible with the flag package)?

flags.Valid("foo")      // false
flags.Valid("--foo")    // true
flags.Valid("-key=val") // true
flags.Valid("-name=")   // true

Parse a flag and return the name

name, _ := flags.Parse("foo")        // returns error, because foo is invalid
name, _ := flags.Parse("--foo")      // name -> "foo
name, _ := flags.Parse("-foo")       // name -> "foo
name, _ := flags.Parse("-foo=value") // name -> "foo
name, _ := flags.Parse("-foo=")      // name -> "foo

flag.Value implementations (StringSlice and IntSlice)

Parse into a []string or []int variable

os.Args = []string{"cmd", "--key", "123,456", "--regions", "us-east-1,eu-west-1"}

var regions []string
var ids []int

flags.StringSliceVar(&regions, []string{}, "to", "Regions to be used")
flags.IntSliceVar(&ids, []int{678}, "ids", "Servers to be used")
flag.Parse()

fmt.Println(regions) // prints: ["us-east-1", "eu-west-1"]
fmt.Println(ids)     // prints: [123,456]

Or plug it into a flag.FlagSet instance:

args := []string{"--key", "123,456", "--regions", "us-east-1,eu-west-1"}

var regions []string
var ids []int

f := flag.NewFlagSet()
f.Var(flags.NewStringSlice(nil, &regions), "to", "Regions to be used")
f.Var(flags.NewIntSlice(nil, &ids), "to", "Regions to be used")
f.Parse(args)

fmt.Println(regions) // prints: ["us-east-1", "eu-west-1"]
fmt.Println(ids)     // prints: [123,456]

Documentation

Overview

Package flags is a low level package for parsing or managing single flag arguments and their associated values from an argument list. It's useful for CLI applications or creating logic for parsing arguments(custom or os.Args) manually.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exclude

func Exclude(flagName string, args []string) []string

Exclude excludes/removes the given valid flagName with it's associated value (or none) from the args. It returns the remaining arguments. If no flagName is passed or if the flagName is invalid, remaining arguments are returned without any change.

func Has

func Has(name string, args []string) bool

Has checks whether the given flag name is available or not in the argument list.

func IntSlice

func IntSlice(value []int, name, usage string) *[]int

IntSlice defines a []int flag with specified name, default value, and usage string. The return value is the address of a []int variable that stores the value of the flag.

func IntSliceVar

func IntSliceVar(p *[]int, value []int, name, usage string)

IntSliceVar defines a []int flag with specified name, default value, and usage string. The argument p points to a []int variable in which to store the value of the flag.

func Parse

func Parse(arg string) (string, error)

Parse parses a flags name. A flag can be in form of --name=value, -name=value, or a boolean flag --name, -name=, etc... If it's a correct flag, the name is returned. If not an empty string and an error message is returned.

func StringSlice

func StringSlice(value []string, name, usage string) *[]string

StringSlice defines a []string flag with specified name, default value, and usage string. The return value is the address of a []string variable that stores the value of the flag.

func StringSliceVar

func StringSliceVar(p *[]string, value []string, name, usage string)

StringSliceVar defines a []string flag with specified name, default value, and usage string. The argument p points to a []string variable in which to store the value of the flag.

func Valid

func Valid(arg string) bool

Valid checks whether the given argument is a valid flag or not

func Value

func Value(flagName string, args []string) (string, error)

Value parses the given flagName from the args slice and returns the value passed to the flag. An example: args: ["--provider", "aws"] will return "aws" for the flag name "provider". An empty string and non error means the flag is in the boolean form, i.e: ["--provider", "--foo", "bar], will return "" as value for the flag name "provider.

Types

type IntSliceValue

type IntSliceValue []int

func NewIntSlice

func NewIntSlice(val []int, p *[]int) *IntSliceValue

NewIntSlice returns a new IntSlice which satisfies the flag.Value interface. This is useful to be used with flag.FlagSet. For the global flag instance, IntSlice() and IntSliceVar() can be used.

func (*IntSliceValue) Get

func (i *IntSliceValue) Get() interface{}

func (*IntSliceValue) Set

func (i *IntSliceValue) Set(val string) error

func (*IntSliceValue) String

func (i *IntSliceValue) String() string

type StringSliceValue

type StringSliceValue []string

func NewStringSlice

func NewStringSlice(val []string, p *[]string) *StringSliceValue

NewStringSlice returns a new StringSlice which satisfies the flag.Value interface. This is useful to be used with flag.FlagSet. For the global flag instance, StringSlice() and StringSliceVar() can be used.

func (*StringSliceValue) Get

func (s *StringSliceValue) Get() interface{}

func (*StringSliceValue) Set

func (s *StringSliceValue) Set(val string) error

func (*StringSliceValue) String

func (s *StringSliceValue) String() string

Jump to

Keyboard shortcuts

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