Giter VIP home page Giter VIP logo

flags's Introduction

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:

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]

flags's People

Contributors

fatih avatar shawnps avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.