fla
github.com/simonleung8/flags
go get github.com/simonleung8/flags
fla

github.com/simonleung8/flags

Improved command-line flag parsing for Go lang

by Simon Leung

v0.0.0-20170704170018-8020ed7bcf1a (see all)License:Apache-2.0
go get github.com/simonleung8/flags
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

GitHub Stars

11

LAST COMMIT

5yrs ago

MAINTAINERS

0

CONTRIBUTORS

4

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
v0.0.0-20160630185256-ae5fa132d3df
2yrs ago
v0.0.0-20150828222106-4304da028804
3yrs ago
v0.0.0-20161027062941-17269f01eb93
3yrs ago
v0.0.0-20170704170018-8020ed7bcf1a
3yrs ago
No alternatives found
No tutorials found
Add a tutorial