opt

package
v0.0.0-...-b76af60 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package opt is an adapter to the getopt-package `github.com/pborman/getopt` providing `getopt` like processing of command line arguments.

Example
package main

import "fmt"

func someOptions() *OptionSet {
	opts := NewOptionSet()
	opts.Add(NewOption(Bool, "foo", 'f', false, "a boolean option"))
	opts.Add(NewOption(String, "bar", 'b', "42", "a string option"))
	return opts
}

func main() {
	opts := someOptions()
	args := []string{"_", "-f", "-x", "--bar", "thirtyfive"}

	opts.Parse(args)
	var foo bool = opts.GetBool("foo")
	var bar string = opts.GetString("bar")
	fmt.Println(foo, bar)
	fmt.Println(args)

}
Output:

true thirtyfive
[_ -f -x --bar thirtyfive]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option struct {
	Type         OptionType
	Name         string
	Short        rune
	DefaultValue interface{}
	Description  string
}

Option specifies a command line option (argument). Each option has a type, a name, a short representation of the name, a default value, and a description.

func NewOption

func NewOption(t OptionType, name string, short rune, defval interface{},
	desc string) Option

NewOption creates a new Option

type OptionSet

type OptionSet getopt.Set

OptionSet is a container type holding multiple Options. It provides methods to parse cmd arguments based on the contained options, and methods to access the respective values afterwards.

func NewOptionSet

func NewOptionSet() *OptionSet

NewOptionSet creates a new OptionSet

func (*OptionSet) Add

func (s *OptionSet) Add(o Option)

Add adds option `o` to OptionSet `s`. Panics if OptionType is not supported.

func (*OptionSet) GetBool

func (s *OptionSet) GetBool(name string) bool

GetBool returns the (bool) value of an option given its `name`

func (*OptionSet) GetFloat64

func (s *OptionSet) GetFloat64(name string) float64

GetFloat64 returns the (float64) value of an option given its `name`

func (*OptionSet) GetInt32

func (s *OptionSet) GetInt32(name string) int32

GetInt32 returns the (int32) value of an option given its `name`

func (*OptionSet) GetList

func (s *OptionSet) GetList(name string) []string

GetList returns the ([]string) value of an option given its `name`

func (*OptionSet) GetString

func (s *OptionSet) GetString(name string) string

GetString returns the (string) value of an option given its `name`

func (*OptionSet) Parse

func (s *OptionSet) Parse(args []string)

Parse parses a list of command line arguments according to an OptionSet

func (*OptionSet) ParseUnforgiving

func (s *OptionSet) ParseUnforgiving(args []string)

ParseUnforgiving parses a list of command line arguments according to an OptionSet. When an error occurs during option parsing, os.Exit(1) is called.

type OptionType

type OptionType byte

OptionType allows to distinguish between different cmd option types

const (
	// Bool is an option type for cmd arguments that can be either true or false
	Bool OptionType = iota
	// String is the option type for cmd arguments holding arbitrary strings
	String
	// Int32 is the option type for cmd arguments holding int32 values
	Int32
	// List is the option type for cmd arguments that are lists of strings
	List
	// Float64 is the option type for cmd arguments holding float64 values
	Float64
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL