szargs

package module
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2025 License: GPL-3.0 Imports: 8 Imported by: 1

README

Package szargs

Overview

package szargs

Package szargs provides a minimal and consistent interface for retrieving settings from command-line arguments ([]string) and environment variables.

It supports three types of arguments:

  • Flagged: Identified by a single dash (e.g., "-v") for short flags, or a double dash (e.g., "--dir") for long-form flags. Flags may be standalone booleans or followed by a value.
  • Positional: Identified by their order in the argument list after all flagged arguments have been processed.
  • Settings: A composite configuration mechanism that combines a default value, an environment variable, and a flagged argument—allowing each to override the previous in precedence: default < env < flag.

The package includes built-in parsers for standard Go data types.

Usage centers around the Args type, created using:

szargs.New(programDesc string, programArgs []string)

The programArgs slice must include the program name as the first element; this is ignored during argument parsing.

After retrieving all relevant arguments, the Args.Done() method must be called to report an error if any unprocessed arguments remain.

This utility reflects a preference for simplicity and clarity in tooling. If it helps your project flow a little more smoothly, it's done its job.

Dedication

This project is dedicated to Reem. Your brilliance, courage, and quiet strength continue to inspire me. Every line is written in gratitude for the light and hope you brought into my life.

NOTE: Documentation reviewed and polished with the assistance of ChatGPT from OpenAI.


Contents

Usage

Generally the flow of argument extraction proceeds as follows:

func main() {
  // Create the args object with a program description (that will be used in
  // the Usage message) and the system arguments.  These will be copied
  // leaving the original os.Args untouched.
  args := szargs.New(
        "A simple demo of values flag.",
        os.Args,
    )

  // Flagged arguments are then extracted using various methods defined on the
  // args object.

  verbose := args.Count("[-v | --verbose ...]","The verbose level.")
  lines := args.Value("[-n | --num numOfLines]","Number of lines to display.")

  // Positional arguments are then extracted.
  file := args.Next("filename","The file to display the lines.")

  // All expected arguments have now been extracted.
  args.Done()

  if args.HasErr() {
    //Report any errors and optionally providing a Usage message.
    fmt.Fprintf(os.Stderr, "Error: %v\n\n%s\n", args.Err(), args.Usage())
  } else {
    // Process with the arguments.
  }
}

General functions operating on the state of the szargs object can be divided into three categories as follows:

Relating to errors:

// HasErr returns true if any errors have been encountered or registered.
func (args *Args) HasErr() bool

// PushErr registers the provided error if not nil to the Args error stack.
func (args *Args) PushErr(err error)

// Err returns any errors encountered or registered while parsing the
// arguments.
func (args *Args) Err() error

Relating to the raw argument list:

// HasNext returns true if any arguments remain unabsorbed.
func (args *Args) HasNext() bool

// PushArg places the supplied argument to the end of the internal args list.
func (args *Args) PushArg(arg string)

// Args returns a copy of the current argument list.
func (args *Args) Args() []string

And general reporting and processing:

// Usage returns a usage message based on the parsed arguments.
//        /*
//        # Usage
// 
//        Golang to 'github' markdown.
// 
//            gotomd [options] [path ...]
// 
//            [-v | --verbose ...]
//                Provide more information when processing.
// 
//        */
func (args *Args) Usage() string

// Done registers an error if there are any remaining arguments.
func (args *Args) Done()

A working example can be found in the example directory as described here:

Contents

Boolean Flags

Boolean flags are defined by their presence only. If they are present then they are true and/or counted. If not present then they are considered false. There are two methods that operate with boolean flags as follows:

// Is returns true if the flag is present one and only one time.
func (args *Args) Is(flag, desc string) bool

// Count returns the number of times the flag appears.
func (args *Args) Count(flag, desc string) int

Contents

Value Flagged Arguments

A flagged argument has two components: the flag followed by the value. It may only appear once in the argument list. The basic string functions are:

// ValueString scans for a specific flagged argument and captures its
// following value as a string. The flag and its value are removed from the
// argument list.
// 
// If the flag appears more than once or lacks a following value, an error is
// registered.
// 
// Returns the string value and a boolean indicating whether the flag was
// found.
func (args *Args) ValueString(flag, desc string) (string, bool)

// ValueOption scans for a specific flagged argument (e.g., "--mode value")
// and captures its associated value. The flag and its value are removed from
// the argument list.
// 
// If the flag appears more than once, or if it lacks a following value, an
// error is registered. If the value is not found in the provided list of
// validOptions, an error is also registered.
// 
// Returns the value and a boolean indicating whether the flag was found.
func (args *Args) ValueOption(flag string, validOptions []string, desc string) (string, bool)

with numeric versions for basic go data types

func (args *Args) ValueFloat64(flag, desc string) (float64, bool)
func (args *Args) ValueFloat32(flag, desc string) (float32, bool)
func (args *Args) ValueInt64(flag, desc string) (int64, bool)
func (args *Args) ValueInt32(flag, desc string) (int32, bool)
func (args *Args) ValueInt16(flag, desc string) (int16, bool)
func (args *Args) ValueInt8(flag, desc string) (int8, bool)
func (args *Args) ValueInt(flag, desc string) (int, bool)
func (args *Args) ValueUint64(flag, desc string) (uint64, bool)
func (args *Args) ValueUint32(flag, desc string) (uint32, bool)
func (args *Args) ValueUint16(flag, desc string) (uint16, bool)
func (args *Args) ValueUint8(flag, desc string) (uint8, bool)
func (args *Args) ValueUint(flag, desc string) (uint, bool)

Contents

Value Flagged Slices

A flagged argument has two components: the flag followed by the value. Multiple instances may be provided with all the values collected and returned in a slice. The basic string functions are:

// ValuesString scans for repeated instances of the specified flag and
// captures the following values as a slice of strings. The flags and values
// are removed from the argument list.
// 
// If any instance of the flag lacks a following value, an error is
// registered.
// 
// Returns a slice of the captured string values.
func (args *Args) ValuesString(flag, desc string) []string

// ValuesOption scans for repeated instances of the specified flag and
// captures the following values. Each value must appear in the provided list
// of validOptions. The flags and values are removed from the argument list.
// 
// If any flag lacks a following value, or if a value is not found in
// validOptions, an error is registered.
// 
// Returns a slice of the captured values.
func (args *Args) ValuesOption(flag string, validOptions []string, desc string) []string

with numeric versions for basic go data types

func (args *Args) ValuesFloat64(flag, desc string) []float64
func (args *Args) ValuesFloat32(flag, desc string) []float32
func (args *Args) ValuesInt64(flag, desc string) []int64
func (args *Args) ValuesInt32(flag, desc string) []int32
func (args *Args) ValuesInt16(flag, desc string) []int16
func (args *Args) ValuesInt8(flag, desc string) []int8
func (args *Args) ValuesInt(flag, desc string) []int
func (args *Args) ValuesUint64(flag, desc string) []uint64
func (args *Args) ValuesUint32(flag, desc string) []uint32
func (args *Args) ValuesUint16(flag, desc string) []uint16
func (args *Args) ValuesUint8(flag, desc string) []uint8
func (args *Args) ValuesUint(flag, desc string) []uint

Contents

Positional Arguments

A positional argument depends on its location in the argument list. Since flagged arguments are not automatically distinguished from positional ones, it is recommended to extract all flagged arguments first—before retrieving positional ones. The basic string functions are:

// NextString removes and returns the next argument from the argument list.
// 
// If no arguments remain, an error is registered.
// 
// Returns the next argument value as a string.
func (args *Args) NextString(name, desc string) string

// NextOption removes and returns the next argument from the argument list.
// The value must match one of the entries in validOptions.
// 
// If no arguments remain, or if the value is not found in validOptions,
// an error is registered.
// 
// Returns the next argument value.
func (args *Args) NextOption(name string, validOptions []string, desc string) string

with numeric versions for basic go data types

func (args *Args) NextFloat64(name, desc string) float64
func (args *Args) NextFloat32(name, desc string) float32
func (args *Args) NextInt64(name, desc string) int64
func (args *Args) NextInt32(name, desc string) int32
func (args *Args) NextInt16(name, desc string) int16
func (args *Args) NextInt8(name, desc string) int8
func (args *Args) NextInt(name, desc string) int
func (args *Args) NextUint64(name, desc string) uint64
func (args *Args) NextUint32(name, desc string) uint32
func (args *Args) NextUint16(name, desc string) uint16
func (args *Args) NextUint8(name, desc string) uint8
func (args *Args) NextUint(name, desc string) uint

Contents

Settings

A setting implements an argument that has a default that can be overridden by an system environment variable which can be overridden by a flagged value. The basic string functions are:

// SettingString returns a configuration value based on a default,
// optionally overridden by an environment variable, and further overridden
// by a flagged command-line argument.
// 
// Returns the final selected string value.
func (args *Args) SettingString(flag, env, def, desc string) string

// SettingOption returns a configuration value based on a default,
// optionally overridden by an environment variable, and further overridden
// by a flagged command-line argument.
// 
// If the final value is not found in the list of validOptions,
// an error is registered.
// 
// Returns the final selected value.
func (args *Args) SettingOption(flag, env string, def string, validOptions []string, desc string) string

// SettingIs returns true if a specified environment variable is set to a
// truthy value, or if a corresponding boolean command-line flag is present.
// 
// Unlike other Setting methods, there is no default.
// 
// The environment variable is considered true if it is set to one of: "",
// "T", "Y", "TRUE", "YES", "ON" or "1" (case-insensitive). Any other value is
// considered false.
// 
// The command-line flag override takes no value—its presence alone indicates
// true.
// 
// Returns the resulting boolean value.
func (args *Args) SettingIs(flag, env string, desc string) bool

with numeric versions for basic go data types

func (args *Args) SettingFloat64(flag, env string, def float64, desc string) float64
func (args *Args) SettingFloat32(flag, env string, def float32, desc string) float32
func (args *Args) SettingInt64(flag, env string, def int64, desc string) int64
func (args *Args) SettingInt32(flag, env string, def int32, desc string) int32
func (args *Args) SettingInt16(flag, env string, def int16, desc string) int16
func (args *Args) SettingInt8(flag, env string, def int8, desc string) int8
func (args *Args) SettingInt(flag, env string, def int, desc string) int
func (args *Args) SettingUint64(flag, env string, def uint64, desc string) uint64
func (args *Args) SettingUint32(flag, env string, def uint32, desc string) uint32
func (args *Args) SettingUint16(flag, env string, def uint16, desc string) uint16
func (args *Args) SettingUint8(flag, env string, def uint8, desc string) uint8
func (args *Args) SettingUint(flag, env string, def uint, desc string) uint

Contents

Documentation

Overview

Package szargs provides a minimal and consistent interface for retrieving settings from command-line arguments ([]string) and environment variables.

It supports three types of arguments:

  • Flagged: Identified by a single dash (e.g., "-v") for short flags, or a double dash (e.g., "--dir") for long-form flags. Flags may be standalone booleans or followed by a value.
  • Positional: Identified by their order in the argument list after all flagged arguments have been processed.
  • Settings: A composite configuration mechanism that combines a default value, an environment variable, and a flagged argument—allowing each to override the previous in precedence: default < env < flag.

The package includes built-in parsers for standard Go data types.

Usage centers around the Args type, created using:

szargs.New(programDesc string, programArgs []string)

The `programArgs` slice must include the program name as the first element; this is ignored during argument parsing.

After retrieving all relevant arguments, the `Args.Done()` method must be called to report an error if any unprocessed arguments remain.

This utility reflects a preference for simplicity and clarity in tooling. If it helps your project flow a little more smoothly, it's done its job.

Dedication

This project is dedicated to Reem. Your brilliance, courage, and quiet strength continue to inspire me. Every line is written in gratitude for the light and hope you brought into my life.

NOTE: Documentation reviewed and polished with the assistance of ChatGPT from OpenAI.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoArgs         = errors.New("no program arguments provided")
	ErrSyntax         = errors.New("syntax")
	ErrRange          = errors.New("range")
	ErrInvalidFloat64 = errors.New("invalid float64")
	ErrInvalidFloat32 = errors.New("invalid float32")
	ErrInvalidInt64   = errors.New("invalid int64")
	ErrInvalidInt16   = errors.New("invalid int16")
	ErrInvalidInt32   = errors.New("invalid int32")
	ErrInvalidInt8    = errors.New("invalid int8")
	ErrInvalidInt     = errors.New("invalid int")
	ErrInvalidUint64  = errors.New("invalid uint64")
	ErrInvalidUint32  = errors.New("invalid uint32")
	ErrInvalidUint16  = errors.New("invalid uint16")
	ErrInvalidUint8   = errors.New("invalid uint8")
	ErrInvalidUint    = errors.New("invalid uint")
	ErrInvalidOption  = errors.New("invalid option")
	ErrInvalidDefault = errors.New("invalid default")
	ErrInvalidFlag    = errors.New("invalid flag")
	ErrInvalidEnv     = errors.New("invalid environment variable")
)

Exported errors.

View Source
var (
	ErrAmbiguous  = errors.New("ambiguous argument")
	ErrMissing    = errors.New("missing argument")
	ErrUnexpected = errors.New("unexpected argument")
)

Exported errors.

Functions

This section is empty.

Types

type Args added in v0.0.4

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

Args provides a single point to access and extract program arguments.

func New added in v0.0.4

func New(programDesc string, args []string) *Args

New creates a new Args object based in the arguments passed. The first element of the arguments must be the program name.

func (*Args) AddSynopsis added in v0.0.10

func (args *Args) AddSynopsis(s string)

AddSynopsis includes another static synopsis message.

func (*Args) Args added in v0.0.4

func (args *Args) Args() []string

Args returns a copy of the current argument list.

func (*Args) Count added in v0.0.4

func (args *Args) Count(flag, desc string) int

Count returns the number of times the flag appears.

func (*Args) Done added in v0.0.4

func (args *Args) Done()

Done registers an error if there are any remaining arguments.

func (*Args) Err added in v0.0.4

func (args *Args) Err() error

Err returns any errors encountered or registered while parsing the arguments.

func (*Args) HasErr added in v0.0.6

func (args *Args) HasErr() bool

HasErr returns true if any errors have been encountered or registered.

func (*Args) HasNext added in v0.0.6

func (args *Args) HasNext() bool

HasNext returns true if any arguments remain unabsorbed.

func (*Args) Is added in v0.0.4

func (args *Args) Is(flag, desc string) bool

Is returns true if the flag is present one and only one time.

func (*Args) NextFloat32 added in v0.0.4

func (args *Args) NextFloat32(name, desc string) float32

NextFloat32 removes and returns the next argument from the argument list, parsing it as a 32 bit floating point number.

If no arguments remain, or if the value has invalid syntax or is out of range for a float32, an error is registered.

Returns the next argument value parsed as a float32.

func (*Args) NextFloat64 added in v0.0.4

func (args *Args) NextFloat64(name, desc string) float64

NextFloat64 removes and returns the next argument from the argument list, parsing it as a 64 bit floating point number.

If no arguments remain, or if the value has invalid syntax or is out of range for a float64, an error is registered.

Returns the next argument value parsed as a float64.

func (*Args) NextInt added in v0.0.4

func (args *Args) NextInt(name, desc string) int

NextInt removes and returns the next argument from the argument list, parsing it as n signed integer.

If no arguments remain, or if the value has invalid syntax or is out of range for an int, an error is registered.

Returns the next argument value parsed as an int.

func (*Args) NextInt16 added in v0.0.4

func (args *Args) NextInt16(name, desc string) int16

NextInt16 removes and returns the next argument from the argument list, parsing it as a signed 16 bit integer.

If no arguments remain, or if the value has invalid syntax or is out of range for an int16, an error is registered.

Returns the next argument value parsed as an int16.

func (*Args) NextInt32 added in v0.0.4

func (args *Args) NextInt32(name, desc string) int32

NextInt32 removes and returns the next argument from the argument list, parsing it as a signed 32 bit integer.

If no arguments remain, or if the value has invalid syntax or is out of range for an int32, an error is registered.

Returns the next argument value parsed as an int32.

func (*Args) NextInt64 added in v0.0.4

func (args *Args) NextInt64(name, desc string) int64

NextInt64 removes and returns the next argument from the argument list, parsing it as a signed 64 bit integer.

If no arguments remain, or if the value has invalid syntax or is out of range for an int64, an error is registered.

Returns the next argument value parsed as an int64.

func (*Args) NextInt8 added in v0.0.4

func (args *Args) NextInt8(name, desc string) int8

NextInt8 removes and returns the next argument from the argument list, parsing it as a signed 8 bit integer.

If no arguments remain, or if the value has invalid syntax or is out of range for an int8, an error is registered.

Returns the next argument value parsed as an int8.

func (*Args) NextOption added in v0.0.6

func (args *Args) NextOption(
	name string, validOptions []string, desc string,
) string

NextOption removes and returns the next argument from the argument list. The value must match one of the entries in validOptions.

If no arguments remain, or if the value is not found in validOptions, an error is registered.

Returns the next argument value.

func (*Args) NextString added in v0.0.4

func (args *Args) NextString(name, desc string) string

NextString removes and returns the next argument from the argument list.

If no arguments remain, an error is registered.

Returns the next argument value as a string.

func (*Args) NextUint added in v0.0.4

func (args *Args) NextUint(name, desc string) uint

NextUint removes and returns the next argument from the argument list, parsing it as an unsigned integer.

If no arguments remain, or if the value has invalid syntax or is out of range for a uint, an error is registered.

Returns the next argument value parsed as a uint.

func (*Args) NextUint16 added in v0.0.4

func (args *Args) NextUint16(name, desc string) uint16

NextUint16 removes and returns the next argument from the argument list, parsing it as an unsigned 16 bit integer.

If no arguments remain, or if the value has invalid syntax or is out of range for a uint16, an error is registered.

Returns the next argument value parsed as a uint16.

func (*Args) NextUint32 added in v0.0.4

func (args *Args) NextUint32(name, desc string) uint32

NextUint32 removes and returns the next argument from the argument list, parsing it as an unsigned 32 bit integer.

If no arguments remain, or if the value has invalid syntax or is out of range for a uint32, an error is registered.

Returns the next argument value parsed as a uint32.

func (*Args) NextUint64 added in v0.0.4

func (args *Args) NextUint64(name, desc string) uint64

NextUint64 removes and returns the next argument from the argument list, parsing it as an unsigned 64 bit integer.

If no arguments remain, or if the value has invalid syntax or is out of range for a uint64, an error is registered.

Returns the next argument value parsed as a uint64.

func (*Args) NextUint8 added in v0.0.4

func (args *Args) NextUint8(name, desc string) uint8

NextUint8 removes and returns the next argument from the argument list, parsing it as an unsigned 8 bit integer.

If no arguments remain, or if the value has invalid syntax or is out of range for a uint8, an error is registered.

Returns the next argument value parsed as a uint8.

func (*Args) ProgramName added in v0.1.1

func (args *Args) ProgramName() string

ProgramName returns the configured program name.

func (*Args) PushArg added in v0.0.6

func (args *Args) PushArg(arg string)

PushArg places the supplied argument to the end of the internal args list.

func (*Args) PushErr added in v0.0.4

func (args *Args) PushErr(err error)

PushErr registers the provided error if not nil to the Args error stack.

func (*Args) RegisterUsage added in v0.1.3

func (args *Args) RegisterUsage(item, desc string)

RegisterUsage registers a new flag and its description if and only if the flag has not been already registered.

func (*Args) SettingFloat32 added in v0.0.4

func (args *Args) SettingFloat32(
	flag, env string, def float32, desc string,
) float32

SettingFloat32 returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as a 32 bit floating point number.

If the final value has invalid syntax or is out of range for a float32, an error is registered.

Returns the final parsed float32 value.

func (*Args) SettingFloat64 added in v0.0.4

func (args *Args) SettingFloat64(
	flag, env string, def float64, desc string,
) float64

SettingFloat64 returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as a 64 bit floating point number.

If the final value has invalid syntax or is out of range for a float64, an error is registered.

Returns the final parsed float64 value.

func (*Args) SettingInt added in v0.0.4

func (args *Args) SettingInt(
	flag, env string, def int, desc string,
) int

SettingInt returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as a signed integer.

If the final value has invalid syntax or is out of range for an int, an error is registered.

Returns the final parsed int value.

func (*Args) SettingInt16 added in v0.0.4

func (args *Args) SettingInt16(
	flag, env string, def int16, desc string,
) int16

SettingInt16 returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as a signed 16 bit integer.

If the final value has invalid syntax or is out of range for an int16, an error is registered.

Returns the final parsed int16 value.

func (*Args) SettingInt32 added in v0.0.4

func (args *Args) SettingInt32(
	flag, env string, def int32, desc string,
) int32

SettingInt32 returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as a signed 32 bit integer.

If the final value has invalid syntax or is out of range for an int32, an error is registered.

Returns the final parsed int32 value.

func (*Args) SettingInt64 added in v0.0.4

func (args *Args) SettingInt64(
	flag, env string, def int64, desc string,
) int64

SettingInt64 returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as a signed 64 bit integer.

If the final value has invalid syntax or is out of range for an int64, an error is registered.

Returns the final parsed int64 value.

func (*Args) SettingInt8 added in v0.0.4

func (args *Args) SettingInt8(
	flag, env string, def int8, desc string,
) int8

SettingInt8 returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as a signed 8 bit integer.

If the final value has invalid syntax or is out of range for an int8, an error is registered.

Returns the final parsed int8 value.

func (*Args) SettingIs added in v0.0.6

func (args *Args) SettingIs(flag, env string, desc string) bool

SettingIs returns true if a specified environment variable is set to a truthy value, or if a corresponding boolean command-line flag is present.

Unlike other Setting methods, there is no default.

The environment variable is considered true if it is set to one of: "", "T", "Y", "TRUE", "YES", "ON" or "1" (case-insensitive). Any other value is considered false.

The command-line flag override takes no value—its presence alone indicates true.

Returns the resulting boolean value.

func (*Args) SettingOption added in v0.0.6

func (args *Args) SettingOption(
	flag, env string, def string, validOptions []string, desc string,
) string

SettingOption returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument.

If the final value is not found in the list of validOptions, an error is registered.

Returns the final selected value.

func (*Args) SettingString added in v0.0.4

func (args *Args) SettingString(
	flag, env, def, desc string,
) string

SettingString returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument.

Returns the final selected string value.

func (*Args) SettingUint added in v0.0.4

func (args *Args) SettingUint(
	flag, env string, def uint, desc string,
) uint

SettingUint returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as an unsigned integer.

If the final value has invalid syntax or is out of range for a uint, an error is registered.

Returns the final parsed uint value.

func (*Args) SettingUint16 added in v0.0.4

func (args *Args) SettingUint16(
	flag, env string, def uint16, desc string,
) uint16

SettingUint16 returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as an unsigned 16 bit integer.

If the final value has invalid syntax or is out of range for a uint16, an error is registered.

Returns the final parsed uint16 value.

func (*Args) SettingUint32 added in v0.0.4

func (args *Args) SettingUint32(
	flag, env string, def uint32, desc string,
) uint32

SettingUint32 returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as an unsigned 32 bit integer.

If the final value has invalid syntax or is out of range for a uint32, an error is registered.

Returns the final parsed uint32 value.

func (*Args) SettingUint64 added in v0.0.4

func (args *Args) SettingUint64(
	flag, env string, def uint64, desc string,
) uint64

SettingUint64 returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as an unsigned 64 bit integer.

If the final value has invalid syntax or is out of range for a uint64, an error is registered.

Returns the final parsed uint64 value.

func (*Args) SettingUint8 added in v0.0.4

func (args *Args) SettingUint8(
	flag, env string, def uint8, desc string,
) uint8

SettingUint8 returns a configuration value based on a default, optionally overridden by an environment variable, and further overridden by a flagged command-line argument. The value is parsed as an unsigned 8 bit integer.

If the final value has invalid syntax or is out of range for a uint8, an error is registered.

Returns the final parsed uint8 value.

func (*Args) Usage added in v0.0.4

func (args *Args) Usage() string
	Usage returns a usage message based on the parsed arguments.
   /*
   # Usage

   Golang to 'github' markdown.

       gotomd [options] [path ...]

   	[-v | --verbose ...]
   		Provide more information when processing.

   */

func (*Args) UsageWidth added in v0.0.9

func (args *Args) UsageWidth(newLineWidth int)

UsageWidth sets the width the usage message. Must be called before any options or arguments are removed otherwise the width will only apply to subsequent additions.

func (*Args) ValueFloat32 added in v0.0.4

func (args *Args) ValueFloat32(flag, desc string) (float32, bool)

ValueFloat32 scans for a specific flagged argument and parses its value as a 32 bit floating point number. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for a float32, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValueFloat64 added in v0.0.4

func (args *Args) ValueFloat64(flag, desc string) (float64, bool)

ValueFloat64 scans for a specific flagged argument and parses its value as a 64 bit floating point number. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for a float64, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValueInt added in v0.0.4

func (args *Args) ValueInt(flag, desc string) (int, bool)

ValueInt scans for a specific flagged argument and parses its value as a signed integer. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for an int, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValueInt16 added in v0.0.4

func (args *Args) ValueInt16(flag, desc string) (int16, bool)

ValueInt16 scans for a specific flagged argument and parses its value as a signed 16 bit integer. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for an int16, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValueInt32 added in v0.0.4

func (args *Args) ValueInt32(flag, desc string) (int32, bool)

ValueInt32 scans for a specific flagged argument and parses its value as a signed 32 bit integer. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for an int32, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValueInt64 added in v0.0.4

func (args *Args) ValueInt64(flag, desc string) (int64, bool)

ValueInt64 scans for a specific flagged argument and parses its value as a signed 64 bit integer. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for an int64, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValueInt8 added in v0.0.4

func (args *Args) ValueInt8(flag, desc string) (int8, bool)

ValueInt8 scans for a specific flagged argument and parses its value as a signed 8 bit integer. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for an int8, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValueOption added in v0.0.6

func (args *Args) ValueOption(
	flag string, validOptions []string, desc string,
) (string, bool)

ValueOption scans for a specific flagged argument (e.g., "--mode value") and captures its associated value. The flag and its value are removed from the argument list.

If the flag appears more than once, or if it lacks a following value, an error is registered. If the value is not found in the provided list of validOptions, an error is also registered.

Returns the value and a boolean indicating whether the flag was found.

func (*Args) ValueString added in v0.0.4

func (args *Args) ValueString(flag, desc string) (string, bool)

ValueString scans for a specific flagged argument and captures its following value as a string. The flag and its value are removed from the argument list.

If the flag appears more than once or lacks a following value, an error is registered.

Returns the string value and a boolean indicating whether the flag was found.

func (*Args) ValueUint added in v0.0.4

func (args *Args) ValueUint(flag, desc string) (uint, bool)

ValueUint scans for a specific flagged argument and parses its value as an unsigned integer. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for a uint, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValueUint16 added in v0.0.4

func (args *Args) ValueUint16(flag, desc string) (uint16, bool)

ValueUint16 scans for a specific flagged argument and parses its value as an unsigned 16 bit integer. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for a uint16, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValueUint32 added in v0.0.4

func (args *Args) ValueUint32(flag, desc string) (uint32, bool)

ValueUint32 scans for a specific flagged argument and parses its value as an unsigned 32 bit integer. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for a uint32, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValueUint64 added in v0.0.4

func (args *Args) ValueUint64(flag, desc string) (uint64, bool)

ValueUint64 scans for a specific flagged argument and parses its value as an unsigned 64 bit integer. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for a uint64, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValueUint8 added in v0.0.4

func (args *Args) ValueUint8(flag, desc string) (uint8, bool)

ValueUint8 scans for a specific flagged argument and parses its value as an unsigned 8 bit integer. The flag and its value are removed from the argument list.

If the flag appears more than once, lacks a following value, or if the value has invalid syntax or is out of range for a uint8, an error is registered.

Returns the parsed value and a boolean indicating whether the flag was found.

func (*Args) ValuesFloat32 added in v0.0.4

func (args *Args) ValuesFloat32(flag, desc string) []float32

ValuesFloat32 scans for repeated instances of the specified flag and parses the following values as 32 bit floating point numbers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for a float32, an error is registered.

Returns a slice of the parsed float32 values.

func (*Args) ValuesFloat64 added in v0.0.4

func (args *Args) ValuesFloat64(flag, desc string) []float64

ValuesFloat64 scans for repeated instances of the specified flag and parses the following values as 64 bit floating point numbers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for a float64, an error is registered.

Returns a slice of the parsed float64 values.

func (*Args) ValuesInt added in v0.0.4

func (args *Args) ValuesInt(flag, desc string) []int

ValuesInt scans for repeated instances of the specified flag and parses the following values as signed integers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for an int, an error is registered.

Returns a slice of the parsed int values.

func (*Args) ValuesInt16 added in v0.0.4

func (args *Args) ValuesInt16(flag, desc string) []int16

ValuesInt16 scans for repeated instances of the specified flag and parses the following values as signed 16 bit integers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for an int16, an error is registered.

Returns a slice of the parsed int16 values.

func (*Args) ValuesInt32 added in v0.0.4

func (args *Args) ValuesInt32(flag, desc string) []int32

ValuesInt32 scans for repeated instances of the specified flag and parses the following values as signed 32 bit integers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for an int32, an error is registered.

Returns a slice of the parsed int32 values.

func (*Args) ValuesInt64 added in v0.0.4

func (args *Args) ValuesInt64(flag, desc string) []int64

ValuesInt64 scans for repeated instances of the specified flag and parses the following values as signed 64 bit integers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for an int64, an error is registered.

Returns a slice of the parsed int64 values.

func (*Args) ValuesInt8 added in v0.0.4

func (args *Args) ValuesInt8(flag, desc string) []int8

ValuesInt8 scans for repeated instances of the specified flag and parses the following values as signed 8 bit integers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for an int8, an error is registered.

Returns a slice of the parsed int8 values.

func (*Args) ValuesOption added in v0.0.6

func (args *Args) ValuesOption(
	flag string, validOptions []string, desc string,
) []string

ValuesOption scans for repeated instances of the specified flag and captures the following values. Each value must appear in the provided list of validOptions. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value is not found in validOptions, an error is registered.

Returns a slice of the captured values.

func (*Args) ValuesString added in v0.0.4

func (args *Args) ValuesString(flag, desc string) []string

ValuesString scans for repeated instances of the specified flag and captures the following values as a slice of strings. The flags and values are removed from the argument list.

If any instance of the flag lacks a following value, an error is registered.

Returns a slice of the captured string values.

func (*Args) ValuesUint added in v0.0.4

func (args *Args) ValuesUint(flag, desc string) []uint

ValuesUint scans for repeated instances of the specified flag and parses the following values as unsigned integers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for a uint, an error is registered.

Returns a slice of the parsed uint values.

func (*Args) ValuesUint16 added in v0.0.4

func (args *Args) ValuesUint16(flag, desc string) []uint16

ValuesUint16 scans for repeated instances of the specified flag and parses the following values as unsigned 16 bit integers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for a uint16, an error is registered.

Returns a slice of the parsed uint16 values.

func (*Args) ValuesUint32 added in v0.0.4

func (args *Args) ValuesUint32(flag, desc string) []uint32

ValuesUint32 scans for repeated instances of the specified flag and parses the following values as unsigned 32 bit integers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for a uint32, an error is registered.

Returns a slice of the parsed uint32 values.

func (*Args) ValuesUint64 added in v0.0.4

func (args *Args) ValuesUint64(flag, desc string) []uint64

ValuesUint64 scans for repeated instances of the specified flag and parses the following values as unsigned 64 bit integers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for a uint64, an error is registered.

Returns a slice of the parsed uint64 values.

func (*Args) ValuesUint8 added in v0.0.4

func (args *Args) ValuesUint8(flag, desc string) []uint8

ValuesUint8 scans for repeated instances of the specified flag and parses the following values as unsigned 8 bit integers. The flags and values are removed from the argument list.

If any flag lacks a following value, or if a value has invalid syntax or is out of range for a uint8, an error is registered.

Returns a slice of the parsed uint8 values.

Directories

Path Synopsis
example
average command
Package main implements a simple example of using a flag to provide a list of floating point numbers and returning the sum or average of the list.
Package main implements a simple example of using a flag to provide a list of floating point numbers and returning the sum or average of the list.
booleanCount command
Package main implements a simple example of using szargs.
Package main implements a simple example of using szargs.
booleanIs command
Package main implements a simple example of using szargs.
Package main implements a simple example of using szargs.
next command
Package main implements a simple example of using szargs.
Package main implements a simple example of using szargs.
setting command
Package main implements a simple example of using szargs.
Package main implements a simple example of using szargs.
value command
Package main implements a simple example of using szargs.
Package main implements a simple example of using szargs.
values command
Package main implements a simple example of using szargs.
Package main implements a simple example of using szargs.

Jump to

Keyboard shortcuts

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