Skip to content
This repository has been archived by the owner. It is now read-only.
Go to file


Failed to load latest commit information.
Latest commit message
Commit time

Archived project. No maintenance.

This project is not maintained anymore and is archived. Feel free to fork and make your own changes if needed. For more detail read my blog post: Taking an indefinite sabbatical from my projects

Thanks to everyone for their valuable feedback and contributions.

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:


go get

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

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

fmt.Println(regions) // prints: ["us-east-1", "eu-west-1"]
fmt.Println(ids)     // prints: [123,456]
You can’t perform that action at this time.