std / flag

flag

import "std:flag"

Provides command-line argument parsing. Flag syntax: -name value, -name=value, --name, -- stops flag parsing. Uses strings/conv for number parsing and os.Args() for arguments.

View source on Codeberg →

Types

#
type Error int

const (
    OK         Error = iota
    ErrNotFound
    ErrSyntax
    ErrMissing
    ErrHelp
)

Error represents the result of a flag parsing operation. OK indicates success; ErrNotFound means an unrecognized flag was encountered; ErrSyntax indicates a malformed value; ErrMissing means a required value was not provided; ErrHelp means the -help or -h flag was invoked.

#
type FlagSet struct { /* unexported fields */ }

FlagSet holds a collection of defined flags, the remaining non-flag arguments after parsing, and whether parsing has been performed.

Construction

#
func NewFlagSet() FlagSet

Creates and returns a new, empty FlagSet ready for flag definitions.

#
func FreeFlagSet(fs *FlagSet)

Releases all resources associated with the given FlagSet.

Definition

#
func DefString(fs *FlagSet, name, defVal, usage string) int

Defines a string flag with the given name, default value, and usage description. Returns the index used to retrieve the flag value after parsing.

#
func DefInt(fs *FlagSet, name string, defVal int64, usage string) int

Defines an integer flag with the given name, default value, and usage description. Returns the index used to retrieve the flag value after parsing.

#
func DefBool(fs *FlagSet, name string, defVal bool, usage string) int

Defines a boolean flag with the given name, default value, and usage description. Returns the index used to retrieve the flag value after parsing.

#
func DefFloat64(fs *FlagSet, name string, defVal float64, usage string) int

Defines a float64 flag with the given name, default value, and usage description. Returns the index used to retrieve the flag value after parsing.

Retrieval

#
func GetString(fs *FlagSet, idx int) string

Returns the value of the string flag at the given index after parsing.

#
func GetInt(fs *FlagSet, idx int) int64

Returns the value of the integer flag at the given index after parsing.

#
func GetBool(fs *FlagSet, idx int) bool

Returns the value of the boolean flag at the given index after parsing.

#
func GetFloat64(fs *FlagSet, idx int) float64

Returns the value of the float64 flag at the given index after parsing.

#
func IsSet(fs *FlagSet, idx int) bool

Reports whether the flag at the given index was explicitly set during parsing, as opposed to retaining its default value.

Arguments

#
func Args(fs *FlagSet) []string

Returns the non-flag arguments remaining after parsing.

#
func NArg(fs *FlagSet) int

Returns the number of non-flag arguments remaining after parsing.

#
func Arg(fs *FlagSet, i int) string

Returns the i-th non-flag argument. It is only valid after parsing has been performed.

Parsing

#
func Parse(fs *FlagSet, args []string) Error

Parses the given argument slice according to the defined flags. Returns an Error indicating success or the kind of failure encountered.

#
func ParseArgs(fs *FlagSet) Error

Parses the command-line arguments from os.Args(), skipping the program name. Returns an Error indicating success or the kind of failure encountered.

Usage

#
func Usage(fs *FlagSet)

Prints a usage message listing all defined flags, their default values, and their usage descriptions to standard error.