Coder Social home page Coder Social logo

optionparser's Introduction

GoDoc

optionparser

Mature command line arguments processor for Go.

Inspired by Ruby's (OptionParser) command line arguments processor.

Installation

go get github.com/speedata/optionparser

Usage

op := optionparser.NewOptionParser()
op.On(arguments ...interface{})
...
err := op.Parse()

where arguments is one of:

  • "-a": a short argument
  • "--argument": a long argument
  • "--argument [FOO]" a long argument with an optional parameter
  • "--argument FOO" a long argument with a mandatory parameter
  • "Some text": The description text for the command line parameter
  • &aboolean: Set the given boolean to true if the argument is given, set to false if parameter is prefixed with no-, such as --no-foo.
  • &astring: Set the string to the value of the given parameter
  • function: Call the function. The function must have the signature func().
  • map[string]string: Set an entry of the map to the value of the given parameter and the key of the argument.
  • []string Set the slice values to a comma separated list.

Help usage

The options -h and --help are included by default. The example below output this on cmd -h:

Usage: [parameter] command
-h, --help                   Show this help
-a, --func                   call myfunc
    --bstring=FOO            set string to FOO
-c                           set boolean option (try -no-c)
-d, --dlong=VAL              set option
-e, --elong[=VAL]            set option with optional parameter
-f                           boolean option

Commands
      y                      Run command y
      z                      Run command z

Settings

After calling op := optionparser.NewOptionParser() you can set op.Banner to set the first line of the help output. The default value is Usage: [parameter] command.

To control the first and last column of the help output, set op.Start and op.Stop. The default values are the integer values of 30 and 79.

Example usage

package main

import (
	"fmt"
	"log"

	"github.com/speedata/optionparser"
)

func myfunc() {
	fmt.Println("myfunc called")
}

func main() {
	var somestring string
	var truefalse bool
	options := make(map[string]string)
	stringslice := []string{}

	op := optionparser.NewOptionParser()
	op.On("-a", "--func", "call myfunc", myfunc)
	op.On("--bstring FOO", "set string to FOO", &somestring)
	op.On("-c", "set boolean option (try -no-c)", options)
	op.On("-d", "--dlong VAL", "set option", options)
	op.On("-e", "--elong [VAL]", "set option with optional parameter", options)
	op.On("-f", "boolean option", &truefalse)
	op.On("-g VALUES", "give multiple values", &stringslice)

	op.Command("y", "Run command y")
	op.Command("z", "Run command z")

	err := op.Parse()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("string `somestring' is now %q\n", somestring)
	fmt.Printf("options %v\n", options)
	fmt.Printf("-f %v\n", truefalse)
	fmt.Printf("-g %v\n", stringslice)
	fmt.Printf("Extra: %#v\n", op.Extra)
}

and the output of go run main.go -a --bstring foo -c -d somevalue -e x -f -g a,b,c y z

is:

myfunc called
string `somestring' is now "foo"
options map[c:true dlong:somevalue elong:x]
-f true
-g [a b c]
Extra: []string{"y", "z"}

State: Actively maintained, and used in production. Without warranty, of course.
Maturity level: 5/5 (works well in all tested repositories, there will be no API change)
License: Free software (MIT License)
Installation: Just run go get github.com/speedata/optionparser
API documentation: https://pkg.go.dev/github.com/speedata/optionparser
Contact: [email protected], @[email protected]
Repository: https://github.com/speedata/optionparser
Dependencies: None
Contribution: We like to get any kind of feedback (success stories, bug reports, merge requests, ...)

optionparser's People

Contributors

pgundlach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

mmpx12

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.