Skip to content

Latest commit

 

History

History
198 lines (147 loc) · 4.68 KB

README.md

File metadata and controls

198 lines (147 loc) · 4.68 KB

Example Boolean Count

Overview

This example demos the counting of boolean flags. Each matching flag is removed as encountered and added to the final count. No errors are generated by the Args.Count method.

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

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

// 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 messages representing the Args object.  It is
// formatted to the lineWidth provided.  A zero uses the defaultLineWidth
// while a negative value caused an effort to determine if writing to a
// terminal and if so using its width otherwise defaulting.
func (args *Args) Usage(lineWidth int) string

Contents

Source

The source used for this example. It simply defines a new szargs.Arg object, then defines a "count" flag which counts and removes the flags from the argument list. 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 utility to demo counting boolean flags.",
        os.Args,
    )

    howMany := args.Count(
        "[-c | --count ...]", // Short and long forms both repeatable.
        "How many times?",
    )

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

    if args.HasErr() {
        fmt.Fprintf(os.Stderr, "Error: %v\n\n%s\n", args.Err(), args.Usage(0))
    } else {
        fmt.Printf("How many: %d\n", howMany)
    }
}

Top of Page -- Szargs Contents

PASS: No Arguments


go run .
How many: 0

Top of Page -- Szargs Contents

PASS: Single Long Form


go run . --count
How many: 1

Top of Page -- Szargs Contents

PASS: Single Short Form


go run . -c
How many: 1

Top of Page -- Szargs Contents

PASS Multiple Mixed Forms


go run . --count -c -c
How many: 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 . --count extraUnknownArgument
Error: unexpected argument: [extraUnknownArgument]

usage: booleanCount [-c | --count ...]

A simple utility to demo counting boolean flags.

    [-c | --count ...]
        How many times?

Top of Page -- Szargs Contents