subc

package module
v0.0.0-...-db345be Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2021 License: MIT Imports: 6 Imported by: 0

README

subc GoDoc

Wrapper for Go's flag that makes adding subcommands easy.

Usage

Each subcommand is a flag.FlagSet accessed via subc.Sub("<name>"), so you can do anything you would be able to do with a normal Go flag.FlagSet.

For code samples see the examples/ directory.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSubc = errors.New("Subcommand not given")

ErrNoSubc is returned when the given arguments do not contain a subcommand.

View Source
var ErrSubcNotExist = errors.New("Subcommand does not exist")

ErrSubcNotExist is returned when a subcommand that does not exist is requested.

View Source
var ErrUsage = errors.New("Usage requested")

ErrUsage is returned when usage/help is requested.

View Source
var Usage = func() {

	names := make([]string, len(subcommands))
	i := 0
	for name := range subcommands {
		names[i] = name
		i++
	}

	fmt.Fprintf(outputWriter, "Usage of %s [%s]\n", os.Args[0], strings.Join(names, "|"))

	for name, f := range subcommands {
		fmt.Fprintf(outputWriter, "%s:\n", name)
		f.PrintDefaults()
	}
}

Prints a usage message documenting all defined subcommands and their flags.

Functions

func Output

func Output() io.Writer

Output returns the destination writer for usage and error messages.

func Parse

func Parse() (string, error)

Parse parses the flags for the subcommand os.Args[1]. Must be called after all flags are defined and before flags are accessed by the program. Returns the name of the requested subcommand.

func ParseArgs

func ParseArgs(arguments []string) (string, error)

Parse parses the the flags of the subcommand given as the first item in the argument list. The given argument list should not include the command name. Returns the name of the parsed subcommand.

func SetOutput

func SetOutput(output io.Writer)

Sets destination for all subcommand usage and messages. By default this is os.Stderr.

func Sub

func Sub(name string) (s *flag.FlagSet)

Sub adds a new subcommand or accesses and existing subcommand.

Types

This section is empty.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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