paramset

package
v4.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package paramset offers helper functions for creating a new param.PSet. It is expected that you will use the

paramset.NewOrDie(...)

function which will create a new param.PSet with the standard helper set up. Any errors detected while constructing the PSet will cause the program to exit, this is almost certainly what you want as they constitute coding errors. You would typically pass it a list of param.PSetOptFunc's which can be used, for instance, to add the params to the PSet.

If you want to handle errors explicitly then the paramset.New(...) will return the constructed PSet and any errors.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(psof ...param.PSetOptFunc) (*param.PSet, error)

New creates a new PSet with the standard helper set. This is a suitable choice in most cases.

func NewNoHelp

func NewNoHelp(psof ...param.PSetOptFunc) (*param.PSet, error)

NewNoHelp creates a new PSet with the helper set to the noHelp helper which does nothing. In particular it will not add any parameters and so it returns a suitable parameter set for the case where you want to add positional parameters the last of which is terminal. This style of interface is used if you have a positional parameter which will invoke a different command based on the value - see the 'git' or 'go' commands for examples of this CLI interface style. If you are choosing an interface like this you might want to consider having one of the possible parameter values being "help" so that the available options can be listed.

If errors are detected then they will be reported and the program will exit.

func NewNoHelpNoExit

func NewNoHelpNoExit(psof ...param.PSetOptFunc) (*param.PSet, error)

NewNoHelpNoExit returns a paramset and any errors encountered while creating it. It adds no parameters and doesn't provide a Usage message. It does report errors but doesn't exit if Parse errors are seen.

This is only likely to be of any use for testing purposes

func NewNoHelpNoExitNoErrRpt

func NewNoHelpNoExitNoErrRpt(psof ...param.PSetOptFunc) (*param.PSet, error)

NewNoHelpNoExitNoErrRpt returns a paramset and any errors encountered while creating it. It adds no parameters and doesn't provide a Usage message. It does report errors but doesn't exit if Parse errors are seen.

This is only likely to be of any use for testing purposes

func NewOrDie

func NewOrDie(psof ...param.PSetOptFunc) *param.PSet

NewOrDie creates a new PSet with the standard helper set. It then checks the error returned and if it is not nil it will report the error on stderr and exit with a non-zero exit status. This is a suitable choice unless you want to perform any special error handling.

Example (Simple)
package main

import (
	"github.com/nickwells/check.mod/check"
	"github.com/nickwells/param.mod/v4/param"
	"github.com/nickwells/param.mod/v4/param/paramset"
	"github.com/nickwells/param.mod/v4/param/psetter"
)

var thingName string
var action = "nothing"

// addParams will add parameters to the passed ParamSet
func addParams(ps *param.PSet) error {
	// This adds a parameter to the PSet that can be given with either of two
	// names: '-name' or '-n'. The parameter parsing will report an error if
	// the parameter is not given or if the value given is an empty string
	ps.Add("name", psetter.String{
		Value:  &thingName,
		Checks: []check.String{check.StringLenGT(0)},
	},
		"set the name of the thing to do that other thing to",
		param.AltName("n"),
		param.Attrs(param.CommandLineOnly|param.MustBeSet),
	)

	// This adds another parameter to the PSet. This can be given with either
	// of '-action' or '-a'. The value given must be one of the allowed
	// values: 'delete', 'copy' or 'nothing'. An error will be reported if
	// the parameter is not seen
	ps.Add("action",
		psetter.Enum{
			Value: &action,
			AllowedVals: param.AllowedVals{
				"delete":  "delete the thing",
				"copy":    "copy the thing",
				"nothing": "do nothing",
			},
		},
		"give the action to perform on the thing",
		param.AltName("a"),
		param.Attrs(param.MustBeSet),
	)

	return nil
}

func main() {
	ps := paramset.NewOrDie(addParams,
		param.SetProgramDescription(
			"a description of the purpose of the program"))
	ps.Parse()

	// the rest of your program goes here
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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