cmdline

package
v0.0.0-...-883d5d7 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: MPL-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package cmdline provides command line option handling.

Index

Constants

This section is empty.

Variables

View Source
var (
	// AppCmdName holds the application's name as specified on the command line.
	AppCmdName string
	// AppName holds the name of the application. By default, this is the same as AppCmdName.
	AppName string
	// CopyrightYears holds the years to place in the copyright banner. Instead of setting this explicitly, consider
	// using CopyrightStartYear and CopyrightEndYear instead. For example, setting CopyrightStartYear early in your
	// main() method, and allowing the build system to populate CopyrightEndYear for you.
	CopyrightYears string
	// CopyrightStartYear holds the starting year to place in the copyright banner. Will not be used if CopyrightYears
	// is already set. If not set explicitly, will be set to the year of the "vcs.time" build tag, if available.
	CopyrightStartYear string
	// CopyrightEndYear holds the ending year to place in the copyright banner. Will not be used if CopyrightYears is
	// already set. If not set explicitly, will be set to the year of the "vcs.time" build tag, if available.
	CopyrightEndYear string
	// CopyrightHolder holds the name of the copyright holder.
	CopyrightHolder string
	// License holds the license the software is being distributed under. This is intended to be a simple one line
	// description, such as "Mozilla Public License 2.0" and not the full license itself.
	License string
	// AppVersion holds the application's version information. If not set explicitly, will be the version of the main
	// module. Unfortunately, this automatic setting only works for binaries created using
	// "go install <package>@<version>".
	AppVersion string
	// GitVersion holds the vcs revision and clean/dirty status. If not set explicitly, will be generated from the value
	// of the build tags "vcs.revision" and "vcs.modified".
	GitVersion string
	// VCSModified is true if the "vcs.modified" build tag is true.
	VCSModified bool
	// BuildNumber holds the build number. If not set explicitly, will be generated from the value of the build tag
	// "vcs.time".
	BuildNumber string
	// AppIdentifier holds the uniform type identifier (UTI) for the application. This should contain only alphanumeric
	// (A-Z,a-z,0-9), hyphen (-), and period (.) characters. The string should also be in reverse-DNS format. For
	// example, if your company’s domain is ajax.com and you create an application named Hello, you could assign the
	// string com.ajax.Hello as your AppIdentifier.
	AppIdentifier string
)

Functions

func Copyright() string

Copyright returns the copyright notice.

func LongVersion

func LongVersion() string

LongVersion returns a combination of the app version and the build number.

func Parse

func Parse(command string) ([]string, error)

Parse a command line string into its component parts.

func ResolveCopyrightYears

func ResolveCopyrightYears() string

ResolveCopyrightYears resolves the copyright years. If the CopyrightYears has been explicitly set, that will be returned unmodified. Otherwise, it will be generated based on the values of CopyrightStartYear and CopyrightEndYear.

func ShortVersion

func ShortVersion() string

ShortVersion returns the app version.

Types

type Cmd

type Cmd interface {
	// Name should return the name of the command as it needs to be entered on the command line.
	Name() string
	// Usage should return a description of what the command does.
	Usage() string
	// Run will be called to run the command. It will be passed a fresh command line created from the command line that
	// was parsed to determine this command would be run, along with the arguments that have not yet been consumed. The
	// first argument, much like an application called from main, will be the name of the command.
	Run(cmdLine *CmdLine, args []string) error
}

Cmd represents a sub-command available on the command-line.

type CmdLine

type CmdLine struct {
	// UsageSuffix, if set, will be appended to the 'Usage: ' line of the output.
	UsageSuffix string
	// Description, if set, will be inserted after the program identity section, before the usage.
	Description string
	// UsageTrailer, if set, will be appended to the end of the usage output.
	UsageTrailer string
	// contains filtered or unexported fields
}

CmdLine holds information about the command line.

func New

func New(includeDefaultOptions bool) *CmdLine

New creates a new CmdLine. If 'includeDefaultOptions' is true, help (-h, --help) and version (-v, --version, along with hidden -V, --Version for long variants) options will be added, otherwise, only the help options will be added, although they will be hidden.

func (*CmdLine) AddCommand

func (cl *CmdLine) AddCommand(cmd Cmd)

AddCommand adds a command to the available commands.

func (*CmdLine) DisplayUsage

func (cl *CmdLine) DisplayUsage()

DisplayUsage displays the program usage information.

func (*CmdLine) FatalError

func (cl *CmdLine) FatalError(err error)

FatalError emits an error message and causes the program to exit.

func (*CmdLine) FatalIfError

func (cl *CmdLine) FatalIfError(err error)

FatalIfError emits an error message and causes the program to exit if err != nil.

func (*CmdLine) FatalMsg

func (cl *CmdLine) FatalMsg(msg string)

FatalMsg emits an error message and causes the program to exit.

func (*CmdLine) NewGeneralOption

func (cl *CmdLine) NewGeneralOption(value any) *Option

NewGeneralOption creates a new Option and attaches it to this CmdLine. Valid value types are: *bool, *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *string, *time.Duration, *[]bool, *[]uint8, *[]uint16, *[]uint32, *[]uint64, *[]int8, *[]int16, *[]int32, *[]int64, *[]string, *[]time.Duration

func (*CmdLine) NewOption

func (cl *CmdLine) NewOption(value Value) *Option

NewOption creates a new Option and attaches it to this CmdLine.

func (*CmdLine) Parse

func (cl *CmdLine) Parse(args []string) []string

Parse the 'args', filling in any options. Returns the remaining arguments that weren't used for option content.

func (*CmdLine) RunCommand

func (cl *CmdLine) RunCommand(args []string) error

RunCommand attempts to run a command. Pass in the command line arguments, usually the result from calling Parse().

func (*CmdLine) SetWriter

func (cl *CmdLine) SetWriter(w io.Writer)

SetWriter sets the io.Writer to use for output. By default, a new CmdLine uses os.Stderr.

func (*CmdLine) Write

func (cl *CmdLine) Write(p []byte) (n int, err error)

Write implements the io.Writer interface.

type GeneralValue

type GeneralValue struct {
	Value any
}

GeneralValue holds a general option value. Valid value types are: *bool, *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64, *string, *time.Duration, *[]bool, *[]uint8, *[]uint16, *[]uint32, *[]uint64, *[]int8, *[]int16, *[]int32, *[]int64, *[]string, *[]time.Duration

func (*GeneralValue) Set

func (v *GeneralValue) Set(str string) error

Set implements Value

func (*GeneralValue) String

func (v *GeneralValue) String() string

type Option

type Option struct {
	// contains filtered or unexported fields
}

Option represents an option available on the command line.

func (*Option) SetArg

func (op *Option) SetArg(name string) *Option

SetArg sets the argument name for this option. Returns self for easy chaining.

func (*Option) SetDefault

func (op *Option) SetDefault(def string) *Option

SetDefault sets the default value for this option. Returns self for easy chaining.

func (*Option) SetName

func (op *Option) SetName(name string) *Option

SetName sets the name for this option. Returns self for easy chaining.

func (*Option) SetSingle

func (op *Option) SetSingle(ch rune) *Option

SetSingle sets the single character name for this option. Returns self for easy chaining.

func (*Option) SetUsage

func (op *Option) SetUsage(usage string) *Option

SetUsage sets the usage message for this option. Returns self for easy chaining.

type Options

type Options []*Option

Options represents a set of options.

func (Options) Len

func (op Options) Len() int

Len implements the sort.Interface interface.

func (Options) Less

func (op Options) Less(i, j int) bool

Less implements the sort.Interface interface.

func (Options) Swap

func (op Options) Swap(i, j int)

Swap implements the sort.Interface interface.

type Value

type Value interface {
	// Set the contents of this value.
	Set(value string) error
	// String implements the fmt.Stringer interface.
	String() string
}

Value represents a value that can be set for an Option.

Jump to

Keyboard shortcuts

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