getopt

package module
v0.0.0-...-07db84d Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2023 License: BSD-3-Clause Imports: 2 Imported by: 2

README

getopt library For Go

This is a very simple library for getopt-style argument/option parsing, written in and for Go. It supports both traditional "short" options (like -a, -b 1, -dfe, etc), and the GNU-style "long" options (like --help, --fix=everything, etc).

I strongly believe that exposing a common and familiar user interface in your programs is important; the getopt-style command line argument parsing is the single most universally accepted convention, dating back to at least 1980, and widely supported by many platforms, languages, and utilities.

Unfortunately, Go's standard flag module ignores that convention, and proposes its own. This package offers a simple alternative.

Example

You can find more examples in the examples directory of the source distribution.

package main

import (
	"github.com/rollcat/getopt"
	"os"
)

func main() {
	args, opts, err := getopt.GetOpt(
		os.Args[1:],
		"hv",
		nil,
	)
	if err != nil || len(args) > 0 {
		println("Usage: program [-hv]")
		os.Exit(1)
	}
	for _, opt := range opts {
		switch opt.Opt() {
		case "-v":
			println("Version 0.1")
			os.Exit(0)
		case "-h":
			println("Usage: program [-hv]")
			os.Exit(0)
		default:
			panic("unexpected argument")
		}
	}
}

Documentation

On pkg.go.dev.

You can also use godocs, or the command line:

go doc github.com/rollcat/getopt

Author and license

Original code by Tim Henderson <tim.tadh@gmail.com>.

This fork, and all of its opinionated tweaks, by Kamil Cholewiński <kamil@rollc.at>.

License is BSD.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OptArg

type OptArg struct {
	Option   string
	Argument string
}

OptArg represents a single parsed option (and its argument, if applicable), as parsed by GetOpt.

func GetOpt

func GetOpt(
	args []string,
	shortopts string,
	longopts []string,
) (
	leftovers []string,
	optargs []OptArg,
	err error,
)

GetOpt parses the provided args, according to shortopts and longopts; and returns the leftover args, parsed options with their arguments, and (if there was one) any encountered parsing error.

See the package documentation for a description of the shortops and longopts formats, as well as how the args are interpreted in their context.

If there is a programming error in shortopts or longopts (rather than a parsing error resulting from unexpected arguments in the resulting program), GetOpt may cause a runtime panic.

func GetOptSafe

func GetOptSafe(
	args []string,
	shortopts string,
	longopts []string,
) (
	leftovers []string,
	optargs []OptArg,
	err error,
)

GetOptSafe works identically to GetOpt, but will not trigger runtime panics on errors such as programmer mistakes in shortopts or longopts. This is for situations, where you'd like to implement getopt(1), or otherwise allow the end user to specify their own shortops/longopts, and get a useful error message rather than a stack trace.

func (OptArg) Arg

func (o OptArg) Arg() string

Arg returns the Argument from OptArg. It exists to maintain backward compatibility with github.com/timtadh/getopt.

func (OptArg) Opt

func (o OptArg) Opt() string

Opt returns the Option from OptArg. It exists to maintain backward compatibility with github.com/timtadh/getopt.

type ParseError

type ParseError struct {
	// message and opt are used to build the friendly user-facing
	// error message.
	Message string
	Opt     string

	// This can be somewhat useful in debugging.
	Unexpected string
	Expected   string
	// contains filtered or unexported fields
}

ParseError contains hints about what exactly went wrong when parsing the arguments in GetOpt. The resulting message (ParseError.Error()) should be displayed to the user.

func (ParseError) Error

func (err ParseError) Error() string

Directories

Path Synopsis
This tiny example program serves as a little stress-test, attempting to parse (only parse!) all arguments supported by GNU coreutils ls(1).
This tiny example program serves as a little stress-test, attempting to parse (only parse!) all arguments supported by GNU coreutils ls(1).

Jump to

Keyboard shortcuts

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