flags

package module
v0.0.0-...-8020ed7 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2017 License: Apache-2.0 Imports: 5 Imported by: 85

README

flags - Golang command-line flag parser

GoDoc Build Status

  • Fully tested, reliable
  • Support flag ShortName (Alias)
  • Catches any non-defined flags, and any invalid flag values
  • Flags are allowed to 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/simonleung8/flags  # installs the flags library

Usage

package main

import (
	"github.com/simonleung8/flags"
	"os"
)

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("p"))
	println("Flag 'password' value: ", fc.String("p"))
}

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 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)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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