optparse

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2020 License: Unlicense Imports: 2 Imported by: 5

README

Traditional long option parser for Go

Package optparse parses command line arguments very similarly to GNU getopt_long(). It supports long options and optional arguments, but does not permute arguments. It is intended as a replacement for Go's flag package.

Like the traditional getopt(), it doesn't automatically parse option arguments, instead delivering them as strings. Nor does it automatically generate a usage message.

Online documentation: https://godoc.org/nullprogram.com/x/optparse

Example usage

package main

import (
	"fmt"
	"os"
	"strconv"

	"nullprogram.com/x/optparse"
)

func fatal(err error) {
	fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], err)
	os.Exit(1)
}

func main() {
	options := []optparse.Option{
		{"amend", 'a', optparse.KindNone},
		{"brief", 'b', optparse.KindNone},
		{"color", 'c', optparse.KindOptional},
		{"delay", 'd', optparse.KindRequired},
		{"erase", 'e', optparse.KindNone},
	}

	var amend bool
	var brief bool
	var color string
	var delay int
	var erase int

	results, rest, err := optparse.Parse(options, os.Args)
	if err != nil {
		fatal(err)
	}

	for _, result := range results {
		switch result.Long {
		case "amend":
			amend = true
		case "brief":
			brief = true
		case "color":
			color = result.Optarg
		case "delay":
			delay, err = strconv.Atoi(result.Optarg)
			if err != nil {
				fatal(err)
			}
		case "erase":
			erase++
		}
	}

	fmt.Println("amend", amend)
	fmt.Println("brief", brief)
	fmt.Println("color", color)
	fmt.Println("delay", delay)
	fmt.Println("erase", erase)
	fmt.Println(rest)
}

Documentation

Overview

Package optparse parses command line arguments very similarly to GNU getopt_long(). It supports long options and optional arguments, but does not permute arguments. It is intended as a replacement for Go's flag package.

To use, define your options as an Option slice and pass it, along with the arguments string slice, to the Parse() function. It will return a slice of parsing results, which is to be iterated over just like getopt().

Index

Constants

View Source
const (
	// KindNone means the option takes no argument
	KindNone Kind = iota
	// KindRequired means the argument requires an option
	KindRequired
	// KindOptional means the argument is optional
	KindOptional

	// ErrInvalid is used when an option is not recognized.
	ErrInvalid = "invalid option"
	// ErrMissing is used when a required argument is missing.
	ErrMissing = "option requires an argument"
	// ErrTooMany is used when an unwanted argument is provided.
	ErrTooMany = "option takes no arguments"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	Option
	Message string
}

Error represents all possible parsing errors. It embeds the option that has been misused, and Message is one of the three error strings. Implements error.

func (Error) Error

func (e Error) Error() string

type Kind

type Kind int

Kind is an enumeration indicating how an option is used.

type Option

type Option struct {
	Long  string
	Short rune
	Kind  Kind
}

Option represents a single argument. Unicode is fully supported, so a short option may be any character. Using the zero value for Long or Short means the option has form of that size. Kind must be one of the constants.

type Result

type Result struct {
	Option
	Optarg string
}

Result is an individual successfully-parsed option. It embeds the original Option plus any argument. For options with optional arguments (KindOptional), it is not possible determine the difference between an empty supplied argument or no argument supplied.

func Parse

func Parse(options []Option, args []string) ([]Result, []string, error)

Parse results a slice of the parsed results, the remaining arguments, and the first parser error. The results slice always contains results up until the first error.

The first argument, args[0], is skipped, and arguments are not permuted. Parsing stops at the first non-option argument, or "--". The latter is not included in the remaining, unparsed arguments.

Jump to

Keyboard shortcuts

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