Giter Site home page Giter Site logo

flago's Introduction

Flago

๐Ÿ‰ Simple and Flexible Command line flag parser


Stars Issues Contributors LICENSE Pkg.go

Install โญ๏ธ

go get github.com/Gers2017/flago

Basic Usage ๐Ÿ”ฅ

Import flago

import (
    "github.com/Gers2017/flago"
)

Populate the flagset

get := flago.NewFlagSet("get")
get.Bool("all", false)
get.Switch("verbose") // Same as get.Bool("verbose", false)

Builder

get := flago.NewFlagSet("get").
    Bool("all", false).
    Switch("verbose") // Same as get.Bool("verbose", false)

Using the Cli struct

cli := flago.NewCli()
cli.Handle(get, func(fs *flago.FlagSet) error {
    HandleFlagset(fs) // do something with parsed get
    return nil
})

if err := cli.Execute(os.Args); err != nil {
    log.Fatal(err)
}
func HandleFlagset(fs *flago.FlagSet) {
    if fs.Bool("help") {
		fmt.Println("Some helpful help message")
		return
	}

    // Do something...
    fmt.Println(todos)
}

Without the Cli struct

Parse the arguments into flags

// os.Args = []string{ "cli", "all", "help" }
if err := get.ParseFlags(os.Args[1:]); err != nil {
    log.Fatal(err)
    return
}

Then use the parsed flagset

if get.IsParsed("all") {
    if get.Bool("help") {
        fmt.Println("Some helpful help message")
        return
    }
    // Do something...
    fmt.Println(todos)
}

Demo ๐Ÿฒ

A complete example can be found here

New FlagSet

get := flago.NewFlagSet("get")

Add flags

get.SetSwitch("verbose")
get.Int("x", 0)

Check if a flag was parsed

It's highly recommended to use this method to check first if a flag was parsed correctly.

get.IsParsed("your-flag-name")

The FlagSet.[Bool, Int, Float, Str] methods are just a shortcut for:

Get values

verbose := get.Bool("verbose")
x := get.Int("x")

If the flag name inside the getter method is not registered in the flagset, you'll get an error at runtime.

wrong := get.Bool("some-invalid-flag")

About the API

Why so many strings? Isn't that error-prone?

  1. The FlagSet.[Bool, Int, Float, Str] method can raise an error at runtine (use FlagSet.IsParsed to avoid this)

  2. A note on golang's generics Behind the scenes flago uses maps + generics + type parsing The Flag struct contains a Value property of type any.

    Because if we try to use generics we'd need to declare a map for every type of flag inside Flagset, and Flags map[string]*Flag wouldn't work anymore leading to repeated code.

    The flag module in the standard library solves this by using pointers to the underliying values.

flago's People

Contributors

gers2017 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.