command

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

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

Go to latest
Published: Jan 9, 2026 License: AGPL-3.0 Imports: 7 Imported by: 9

README

Package command exposes CommandLine which can be used like flag.CommandLine (embedded). The idea is to provide an API nearly identical to the stdlib flag package, with extra features. This package will remain small, so consumers can either import this package or copy this file to their main package, if they prefer to avoid an additional dependency.

In addition to flag variables as provided by the stdlib, this package introduces Operation callbacks. Each operation has a function signature similar to the top level main, and can be implemented in a similar way. That is, the operation callback can define flags on the CommandLine in the same way that main does.

If a command "foo" defines operations "bar" and "baz", then when the main function calls [CommandLine.Parse], one of those callbacks will be run, depending on the args passed to the command. For example,

foo -fooarg=x # runs `main` as usual
foo -fooarg=x bar # runs the bar callback
foo baz -fooarg=x # runs the baz callback

If the operations need their own flags or arguments, they rely on the CommandLine API, exactly as the main function does. They may even define their own sub-operation callbacks.

On the command line, flags defined in main can either preceed or follow the operation name, as a convenience to the user.

Documentation

Overview

Package command exposes CommandLine which can be used like flag.CommandLine (embedded). The idea is to provide an API nearly identical to the stdlib flag package, with extra features. This package will remain small, so consumers can either import this package or copy this file to their main package, if they prefer to avoid an additional dependency.

In addition to flag variables as provided by the stdlib, this package introduces Operation callbacks. Each operation has a function signature similar to the top level `main`, and can be implemented in a similar way. That is, the operation callback can define flags on the CommandLine in the same way that `main` does.

If a command "foo" defines operations "bar" and "baz", then when the `main` function calls [CommandLine.Parse], one of those callbacks will be run, depending on the args passed to the command. For example,

foo -fooarg=x # runs `main` as usual
foo -fooarg=x bar # runs the bar callback
foo baz -fooarg=x # runs the baz callback

If the operations need their own flags or arguments, they rely on the CommandLine API, exactly as the `main` function does. They may even define their own sub-operation callbacks.

On the command line, flags defined in `main` can either preceed or follow the operation name, as a convenience to the user.

Index

Constants

This section is empty.

Variables

View Source
var CommandLine = &command{
	FlagSet: flag.CommandLine,
	arg:     os.Args[1:],
}

Functions

func Check

func Check(err error)

func CheckFatal

func CheckFatal(err error)

CheckFatal exits with error message, if err is not nil.

func CheckUsage

func CheckUsage(err error)

CheckUsage writes usage and exits, if err is not nil.

Types

type Operation

type Operation func() error

type Path

type Path string

Source Files

  • command.go

Directories

Path Synopsis
Package genvironment
Package genvironment
Package command provides consistency in parsing command line flags, reading config files, and writing logs.
Package command provides consistency in parsing command line flags, reading config files, and writing logs.
v2
Package command offers an minimalist approach to building command-line executables, in particular those that have sub-commands (here called "operations").
Package command offers an minimalist approach to building command-line executables, in particular those that have sub-commands (here called "operations").
example command
Package main provides an example of how to use src.d10.dev/command/v2.
Package main provides an example of how to use src.d10.dev/command/v2.
v3
example command

Jump to

Keyboard shortcuts

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