Giter VIP home page Giter VIP logo

flag2's Introduction

Flag2

A more traditional flag library for the go programming language

What?

A more traditional flag library for the Go programming language. I also have a long history with Python, so the implimentation code looks similar to Python's argparse class.

Why?

I did not like how the flag library that comes with Go parses command line flags.

Differences

  • You can define full word flags with the -- prefix. You can define single character flags with the - prefix.

  • Example of a full word flag: --help

  • Example of a single character flag: -h

  • Single character strings can be grouped, but only for boolean types: -abcd is essentially -a, -b, -c, -d

    • This only works for boolean type flags
  • -- denotes the end of the command line flag options

    • Everything to the right of -- will not be counted as flags

Getting started

To install: go get github.com/ProfOak/flag2

package main

import (
    "os"
    "fmt"
    "github.com/ProfOak/flag2"
)

func main() {
    f := flag2.NewFlag()

    // short flag, long flag, description, default argument
    f.AddString("n", "name", "this flag wants a name as input", "billy")
    f.AddBool("b", "bool", "this flag will store true", false)

    // a help flag is added during the parse step
    options, args := f.Parse(os.Args)

    // A usage method is provided, with details about each flag

    // unfortunate side effect of interfaces
    if options["help"] == true {
        f.Usage()
    }

    fmt.Println()
    if options["name"] != nil {
        fmt.Println("The name is:", options["name"])
    }

    fmt.Println()
    fmt.Println("===== FINAL RESULTS =====")
    fmt.Println("Options:", options)
    fmt.Println("Args:", args)
}

The result of running this program:

go run main.go -b -n ProfOak Extra args

--- Bools ---
-b, --bool      this flag will store true
-h, --help      Display this message and exit

--- Strings ---
-n, --name      this flag wants a name as input

Name is: ProfOak

===== FINAL RESULTS =====
Options: map[help:true bool:true name:ProfOak]
Args: [Extra args]

Reference ./test/test.go for a more detailed example.

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.