cmdmain

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package cmdmain contains the shared implementation for command-line tools. It is copied from Camlistore, where it is used for camget, camput, camtool, and other Camlistore command-line tools.

Index

Constants

This section is empty.

Variables

View Source
var (
	FlagHelp    = flag.Bool("help", false, "print usage")
	FlagVerbose = flag.Bool("verbose", false, "extra debug logging")
)
View Source
var (
	// ExtraFlagRegistration allows to add more flags from
	// other packages (with AddFlags) when Main starts.
	ExtraFlagRegistration = func() {}
	// PreExit runs after the subcommand, but before Main terminates
	// with either success or the error from the subcommand.
	PreExit = func() {}
	// ExitWithFailure determines whether the command exits
	// with a non-zero exit status.
	ExitWithFailure bool
	// CheckCwd checks the current working directory, and possibly
	// changes it, or aborts the run if needed.
	CheckCwd = func() {}
)
View Source
var (

	// Indirections for replacement by tests
	Stderr io.Writer = os.Stderr
	Stdout io.Writer = os.Stdout
	Stdin  io.Reader = os.Stdin

	Exit = realExit
)
View Source
var ErrUsage = UsageError("invalid command")

Functions

func Errorf

func Errorf(format string, args ...interface{})

Errorf prints to Stderr

func Main

func Main()

Main is meant to be the core of a command that has subcommands (modes). Chooses the matching subcommand, and runs it. Call CheckCwd on start, and PreExit before exiting.

func MainDefault

func MainDefault(defaultMode string)

MainDefault is meant to be the core of a command that has subcommands (modes). Chooses the matching subcommand, and runs it. Call CheckCwd on start, and PreExit before exiting.

defaultMode will be used if not arguments given on command line.

func RegisterCommand

func RegisterCommand(mode string, makeCmd func(Flags *flag.FlagSet) CommandRunner)

RegisterCommand adds a mode to the list of modes for the Main command. It is meant to be called in init() for each subcommand.

Types

type CommandRunner

type CommandRunner interface {
	Usage()
	RunCommand(args []string) error
}

CommandRunner is the type that a command mode should implement.

type UsageError

type UsageError string

func (UsageError) Error

func (ue UsageError) Error() string

Jump to

Keyboard shortcuts

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