GithubHelp home page GithubHelp logo

flagplus's Introduction

Package flagplus

flagplus is a wrapper and extension to the Go core flag package. The flagplus.FlagSet type emphasizes simple declarative flag definitions, easily divisible by main() or init()

The Usage output is readable and informative, including a summary option list.

Getting Started

Installing the package

go get github.com/scu/flagplus

Usage

import "github.com/scu/flagplus"

Basic Example

See godocs for additional code examples.

package main

import (
	"fmt"

	"github.com/scu/util/flagplus"
)

var flags *flagplus.FlagSet

func main() {
	// Get and use the flag options
	outOpt, err := flags.GetString("output")
	if err != nil {
		panic("Error retreiving output option")
	}
	fmt.Printf("output option is %q\n", outOpt)

	// Get and use command line arguments provided after flags
	args := flags.GetArgs()
	for _, arg := range args {
		fmt.Printf("arg is %q\n", arg)
	}

	// Print program usage
	fmt.Println(flags.Usage())
}

func init() {
	// Create the FlagSet object
	flags = flagplus.NewFlagSet("util", "utility")

	// Set optional description
	flags.FlagSetDescription("Utility that does stuff")

	// Add flags of various types
	flags.AddBoolFlag("verbose", "v", "Print extra debugging information", false)
	flags.AddFlag("help", "h", "Help")
	flags.AddIntFlag("line", "l", "Start counting at `line_number`", 1)
	flags.AddStringFlag("output", "o", "Output `directory`", "/var/log/output")
	flags.AddFloatFlag("skew", "s", "Skew `percentage`", 2.33)

	// Parse the flags
	flags.Parse()
}

Usage Output:

Utility that does stuff
Usage:
  util|utility [-h|l line_number|o directory|s percentage|v bool]
Options:
  -h, --help 
     Help
  -l, --line line_number
     Start counting at line_number (default=1)
  -o, --output directory
     Output directory (default=/var/log/output)
  -s, --skew percentage
     Skew percentage (default=2.33)
  -v, --verbose bool
     Print extra debugging information (default=false)

Running the tests

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Versioning

SemVer is used for versioning. For the versions available, see the tags on this repository.

Authors

License

MIT

flagplus's People

Contributors

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