cmd

package
v0.0.0-...-52d6522 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: AGPL-3.0, Apache-2.0, CC-BY-SA-3.0 Imports: 12 Imported by: 1

Documentation

Overview

Package cmd helps define reusable functions that can be exposed as [subcommands of] command line programs.

Index

Constants

View Source
const EXIT_INVALIDARGUMENT = 2

Variables

View Source
var Version versionCommand

Version is a Handler that prints the package version (set at build time using -ldflags) and Go runtime version to stdout, and returns 0.

Functions

func ParseFlags

func ParseFlags(f FlagSet, prog string, args []string, positional string, stderr io.Writer) (ok bool, exitCode int)

ParseFlags calls f.Parse(args) and prints appropriate error/help messages to stderr.

The positional argument is "" if no positional arguments are accepted, otherwise a string to print with the usage message, "Usage: {prog} [options] {positional}".

The first return value, ok, is true if the program should continue running normally, or false if it should exit now.

If ok is false, the second return value is an appropriate exit code: 0 if "-help" was given, 2 if there was a usage error.

func SubcommandToFront

func SubcommandToFront(args []string, flagset FlagSet) []string

SubcommandToFront silently parses args using flagset, and returns a copy of args with the first non-flag argument moved to the front. If parsing fails or consumes all of args, args is returned unchanged.

SubcommandToFront invokes methods on flagset that have side effects, including Parse. In typical usage, flagset will not used for anything else after being passed to SubcommandToFront.

Types

type FlagSet

type FlagSet interface {
	Init(string, flag.ErrorHandling)
	Args() []string
	NArg() int
	Parse([]string) error
	SetOutput(io.Writer)
	PrintDefaults()
}

type Handler

type Handler interface {
	RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int
}

type HandlerFunc

type HandlerFunc func(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int

func (HandlerFunc) RunCommand

func (f HandlerFunc) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int

type Multi

type Multi map[string]Handler

Multi is a Handler that looks up its first argument in a map (after stripping any "arvados-" or "crunch-" prefix), and invokes the resulting Handler with the remaining args.

Example:

os.Exit(Multi(map[string]Handler{
        "foobar": HandlerFunc(func(prog string, args []string) int {
                fmt.Println(args[0])
                return 2
        }),
})("/usr/bin/multi", []string{"foobar", "baz"}, os.Stdin, os.Stdout, os.Stderr))

...prints "baz" and exits 2.

func (Multi) RunCommand

func (m Multi) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int

func (Multi) Usage

func (m Multi) Usage(stderr io.Writer)

type NoPrefixFormatter

type NoPrefixFormatter struct{}

func (NoPrefixFormatter) Format

func (NoPrefixFormatter) Format(entry *logrus.Entry) ([]byte, error)

Jump to

Keyboard shortcuts

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