optopia

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2019 License: Apache-2.0 Imports: 5 Imported by: 4

README

= Optopia™

image:https://img.shields.io/github/workflow/status/gdamore/optopia/build?logoColor=grey&logo=github&label=[Build Status,link="https://github.com/gdamore/optopia/actions"]
image:https://img.shields.io/codecov/c/github/gdamore/optopia?logoColor=grey&logo=codecov&label=[Coverage,link="https://codecov.io/gh/gdamore/optopia"]
image:https://img.shields.io/codacy/grade/ae82e1343985431aac6b6ad182021c6b?logoColor=grey&logo=codacy&label=[Code Quality,link="https://app.codacy.com/manual/gdamore/optopia/dashboard"]
image:https://img.shields.io/badge/godoc-docs-blue.svg?label=&logo=go[GoDoc,link="https://godoc.org/github.com/gdamore/optopia"]
image:https://img.shields.io/github/license/gdamore/optopia.svg?logoColor=silver&logo=Open Source Initiative&label=&color=blue[Apache 2.0 License,link="https://github.com/gdamore/optopia/blob/master/LICENSE"]

_Optopia_ is a simple utility library for parsing options, much in the
style of `getopt()` or `getopt_long()`, but for Go programs.

Why yet another `getopt` clone?

We needed some basic functionality with easy callbacks that could be used
disconnected from an application (so that we could create individual instances
of an application to facilitate testing.)
It wasn't immediately obvious that any of the usual suspects met that need.

There are many other option parsers that offer more richness.
Please feel free to use those if that's what you're looking for;
we've aimed for simplicity.

Note that some option parsers work by using reflection and structure tags.
We find that a bit obtuse, and difficult to use.
Such approaches also bypass the good type validation that Go gives us,
so we "`opt`" (pardon the pun) to let the language help us.


== Documentation

For docs, see https://godoc.org/gdamore/optopia or run:

    $ godoc -http=:6060

then see http://localhost:6060/pkg/gdamore/optopia/

== Dependencies

None, apart from things found in the standard library.
_Optopia_ should be portable to any platform Go runs on.

== Testing

This package supports internal self tests, which can be run in
the idiomatic Go way.
At present the code is completely covered by the test suite.

'''
Copyright 2019 Garrett D'Amore

Documentation

Overview

Package optopia implements a fairly simple options style parser. It supports long (--option) and short (-o) options. The reason for its existence is that we wanted something simple, but with support for callback functions.

Index

Constants

View Source
const (
	ErrNoSuchOption        = err("no such option")
	ErrOptionRequiresValue = err("option requires value")
	ErrParsingValue        = err("failure parsing option value")
	ErrDuplicateOption     = err("duplicate option")
	ErrShortAndLongEmpty   = err("long and short options both empty")
)

These are standard error codes.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option struct {
	// Long is the long form of the option (without the --).
	Long string

	// Short is the short (single character) form of the option.
	Short rune

	// HasArg indicates that the option takes a value.
	// This is presumed if ArgP is not nil.
	HasArg bool

	// ArgName is the name of the associated argument.
	// Used principally in help output.
	ArgName string

	// ArgP is used to store the value.  At present
	// this can be a pointer to string, int, int64, uint64, or bool.
	// It can also be a TextUnmarshaller.
	ArgP interface{}

	// Handle is executed when this option is found, and passed the
	// raw string.  If ArgP is set, then any conversion is
	// is done first.  (If the conversion fails, then that error
	// is returned to the caller, and Handle is not called.)
	Handle func(string) error

	// Help is a short help message about the option.
	Help string

	// Seen is updated after Options.Parse.  It is true if the option
	// was seen.  This is useful for options that have no value.
	Seen bool

	// Raw contains the raw value for options that take one.
	// It is updated on Options.Parse.
	Raw string
}

Option represents a single option. Allocate one of these and pass it to Options.Add() to register.

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options are the main set of Options for a program. The zero value is usable immediately.

func (*Options) Add

func (o *Options) Add(opts ...*Option) error

Add registers a given function.

func (*Options) Help added in v0.2.0

func (o *Options) Help() string

Help returns a help string based on the options that have been registered. It only includes the option-specific help now -- nothing about the application itself is provided.

func (*Options) Parse

func (o *Options) Parse(args []string) ([]string, error)

Parse parses the options. Any residual options are returned, and if a parse error that is returned too.

func (*Options) Reset

func (o *Options) Reset()

Reset resets the values of any Option that has been added. Use it to run through the option parsing multiple times.

Jump to

Keyboard shortcuts

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