next

command
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: 3 Imported by: 0

README

Example Next Positional Argument

Overview

This example demos the parsing of the next argument. If found the argument is removed and parsed as encountered. A missing argument will generate an error.

It demonstrates the following szargs functions:

// New creates a new Args object based in the arguments passed.  The first
// element of the arguments must be the program name.
func New(programDesc string, args []string) *Args

// 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

// 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)

// 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 *Args) NextUint(name, desc string) uint

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

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

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

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

Contents

Source

The source used for this example. It simply defines a new szargs.Arg object, then defines two positional arguments (one mandatory and one optional). Finally the Done function is called to insure that no more arguments exist in the list.

cat ./main.go
// Package main implements a simple example of using szargs.
//
//nolint:forbidigo // OK to print to os.Stdout.
package main

import (
    "fmt"
    "os"

    "github.com/dancsecs/szargs"
)

func main() {
    args := szargs.New(
        "A simple demo of repeating a string.",
        os.Args,
    )

    mandatory := args.NextString(
        "message",
        "What to repeat.",
    )

    if !args.HasNext() {
        args.PushArg(("3")) // Default value dor optional integer.
    }

    optional := args.NextUint(
        "[times]",
        "The number of times to repeat.  Defaults to 3.",
    )

    args.Done() // All arguments should have consumed.

    if args.HasErr() {
        fmt.Fprintf(os.Stderr, "Error: %v\n\n%s\n", args.Err(), args.Usage())
    } else {
        for range optional {
            fmt.Println(mandatory)
        }
    }
}

Top of Page -- Szargs Contents

PASS: Count Defaulted

go run . StringToRepeat
StringToRepeat
StringToRepeat
StringToRepeat

Top of Page -- Szargs Contents

PASS: Count Provided

go run . StringToRepeat 5
StringToRepeat
StringToRepeat
StringToRepeat
StringToRepeat
StringToRepeat

Top of Page -- Szargs Contents

FAIL: No Arguments

go run .
Error: missing argument: message

next
A simple demo of repeating a string.

Usage: next message [times]

message
What to repeat.

[times]
The number of times to repeat.  Defaults to 3.

Top of Page -- Szargs Contents

FAIL: Extra Unknown Argument

The error is generated by the args.Done() statement causing both the error and a usage statement to be returned.


go run . stringToRepeat 5 extraUnknownArgument
Error: unexpected argument: [extraUnknownArgument]

next
A simple demo of repeating a string.

Usage: next message [times]

message
What to repeat.

[times]
The number of times to repeat.  Defaults to 3.

Top of Page -- Szargs Contents

Documentation

Overview

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