flags

package
v7.0.0-beta.26+incompa... Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2019 License: Apache-2.0, Apache-2.0 Imports: 5 Imported by: 0

README

flags - Golang command-line flag parser

GoDoc

  • Fully tested, reliable
  • Support flag ShortName (Alias)
  • Catches any non-defined flags, and any invalid flag values
  • Flags can come before or after the arguments. The followings are all valid inputs:
testapp -i 100 -m 500 arg1 arg2   # flags go first
testapp arg1 arg2 --i 100 -m 500  # flags go last
testapp arg1 -i 100 arg2 -m=500   # flags go in between arguments

The parsed results for all 3 statements are identical: i=100, Args=[arg1, arg2], m=500

Installation

go get github.com/cloudfoundry/cli/cf/flags  # installs the flags library

Usage

package main

import "github.com/cloudfoundry/cli/cf/flags"

func main(){
  fc := flags.New()
  fc.NewStringFlag("password", "p", "flag for password")  //name, short_name and usage of the string flag
  fc.Parse(os.Args...)  //parse the OS arguments
  println("Flag 'password' is set: ", fc.IsSet("s"))
  println("Flag 'password' value: ", fc.String("s"))
}

Running the above code

$ main -password abc
Flag 'password' is set: true
Flag 'password' value: abc

Available Flag Constructor

Flags: String, Int, float64, Bool, String Slice

NewStringFlag(name string, short_name string, usage string)
NewStringFlagWithDefault(name string, short_name string, usage string, value string)
NewIntFlag(name string, short_name string, usage string)
NewIntFlagWithDefault(name string, short_name string, usage string, value int)
NewFloat64Flag(name string, short_name string, usage string)
NewFloat64FlagWithDefault(name string, short_name string, usage string, value float64)
NewStringSliceFlag(name string, short_name string, usage string) //this flag can be supplied more than 1 time
NewStringSliceFlagWithDefault(name string, short_name string, usage string, value []string)
NewBoolFlag(name string, short_name string, usage string)

Functions for flags/args reading

IsSet(flag_name string)bool
String(flag_name string)string
Int(flag_name string)int
Float64(flag_name string)float64
Bool(flag_name string)bool
StringSlice(flag_name string)[]string  
Args()[]string

Parsing flags and arguments

Parse(args ...string)error  //returns error for any non-defined flags & invalid value for Int, Float64 and Bool flag.

Sample Code

fc := flags.New()
fc.NewIntFlag("i", "", "Int flag name i")  //set up a Int flag '-i'
fc.NewBoolFlag("verbose", "v", "Bool flag name verbose")  //set up a bool flag '-verbose'
err := fc.Parse(os.Args...) //Parse() returns any error it finds during parsing
If err != nil {
  fmt.Println("Parsing error:", err)
}
fmt.Println("Args:", fc.Args())  //Args() returns an array of all the arguments
fmt.Println("Verbose:", fc.Bool("verbose"))
fmt.Println("i:", fc.Int("i"))

Running above

$ app arg_1 -i 100 arg_2 -verbose  # run the code
Args: [arg_1 arg_2]
Verbose: true
i: 100

Special function

SkipFlagParsing(bool)  //if set to true, all flags become arguments
ShowUsage(leadingSpace int)string  //string containing all the flags and their usage text

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackwardsCompatibilityFlag

type BackwardsCompatibilityFlag struct{}

func (*BackwardsCompatibilityFlag) GetName

func (f *BackwardsCompatibilityFlag) GetName() string

func (*BackwardsCompatibilityFlag) GetShortName

func (f *BackwardsCompatibilityFlag) GetShortName() string

func (*BackwardsCompatibilityFlag) GetValue

func (f *BackwardsCompatibilityFlag) GetValue() interface{}

func (*BackwardsCompatibilityFlag) Set

func (*BackwardsCompatibilityFlag) String

func (f *BackwardsCompatibilityFlag) String() string

func (*BackwardsCompatibilityFlag) Visible

func (f *BackwardsCompatibilityFlag) Visible() bool

type BoolFlag

type BoolFlag struct {
	Name      string
	Value     bool
	Usage     string
	ShortName string
	Hidden    bool
}

func (*BoolFlag) GetName

func (f *BoolFlag) GetName() string

func (*BoolFlag) GetShortName

func (f *BoolFlag) GetShortName() string

func (*BoolFlag) GetValue

func (f *BoolFlag) GetValue() interface{}

func (*BoolFlag) Set

func (f *BoolFlag) Set(v string)

func (*BoolFlag) String

func (f *BoolFlag) String() string

func (*BoolFlag) Visible

func (f *BoolFlag) Visible() bool

type FlagContext

type FlagContext interface {
	Parse(...string) error
	Args() []string
	Int(string) int
	Float64(string) float64
	Bool(string) bool
	String(string) string
	StringSlice(string) []string
	IsSet(string) bool
	SkipFlagParsing(bool)
	NewStringFlag(name string, shortName string, usage string)
	NewStringFlagWithDefault(name string, shortName string, usage string, value string)
	NewBoolFlag(name string, shortName string, usage string)
	NewIntFlag(name string, shortName string, usage string)
	NewIntFlagWithDefault(name string, shortName string, usage string, value int)
	NewFloat64Flag(name string, shortName string, usage string)
	NewFloat64FlagWithDefault(name string, shortName string, usage string, value float64)
	NewStringSliceFlag(name string, shortName string, usage string)
	NewStringSliceFlagWithDefault(name string, shortName string, usage string, value []string)
	ShowUsage(leadingSpace int) string
}

func New

func New() FlagContext

func NewFlagContext

func NewFlagContext(cmdFlags map[string]FlagSet) FlagContext

type FlagSet

type FlagSet interface {
	fmt.Stringer
	GetName() string
	GetShortName() string
	GetValue() interface{}
	Set(string)
	Visible() bool
}

type Float64Flag

type Float64Flag struct {
	Name      string
	Value     float64
	Usage     string
	ShortName string
	Hidden    bool
}

func (*Float64Flag) GetName

func (f *Float64Flag) GetName() string

func (*Float64Flag) GetShortName

func (f *Float64Flag) GetShortName() string

func (*Float64Flag) GetValue

func (f *Float64Flag) GetValue() interface{}

func (*Float64Flag) Set

func (f *Float64Flag) Set(v string)

func (*Float64Flag) String

func (f *Float64Flag) String() string

func (*Float64Flag) Visible

func (f *Float64Flag) Visible() bool

type IntFlag

type IntFlag struct {
	Name      string
	Value     int
	Usage     string
	ShortName string
	Hidden    bool
}

func (*IntFlag) GetName

func (f *IntFlag) GetName() string

func (*IntFlag) GetShortName

func (f *IntFlag) GetShortName() string

func (*IntFlag) GetValue

func (f *IntFlag) GetValue() interface{}

func (*IntFlag) Set

func (f *IntFlag) Set(v string)

func (*IntFlag) SetVisibility

func (f *IntFlag) SetVisibility(v bool)

func (*IntFlag) String

func (f *IntFlag) String() string

func (*IntFlag) Visible

func (f *IntFlag) Visible() bool

type StringFlag

type StringFlag struct {
	Name      string
	Value     string
	Usage     string
	ShortName string
	Hidden    bool
}

func (*StringFlag) GetName

func (f *StringFlag) GetName() string

func (*StringFlag) GetShortName

func (f *StringFlag) GetShortName() string

func (*StringFlag) GetValue

func (f *StringFlag) GetValue() interface{}

func (*StringFlag) Set

func (f *StringFlag) Set(v string)

func (*StringFlag) String

func (f *StringFlag) String() string

func (*StringFlag) Visible

func (f *StringFlag) Visible() bool

type StringSliceFlag

type StringSliceFlag struct {
	Name      string
	Value     []string
	Usage     string
	ShortName string
	Hidden    bool
}

StringSlice flag can be define multiple times in the arguments

func (*StringSliceFlag) GetName

func (f *StringSliceFlag) GetName() string

func (*StringSliceFlag) GetShortName

func (f *StringSliceFlag) GetShortName() string

func (*StringSliceFlag) GetValue

func (f *StringSliceFlag) GetValue() interface{}

func (*StringSliceFlag) Set

func (f *StringSliceFlag) Set(v string)

func (*StringSliceFlag) String

func (f *StringSliceFlag) String() string

func (*StringSliceFlag) Visible

func (f *StringSliceFlag) Visible() bool

Jump to

Keyboard shortcuts

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