Skip to content

Latest commit

 

History

History
213 lines (158 loc) · 5.11 KB

README.md

File metadata and controls

213 lines (158 loc) · 5.11 KB

Example Boolean Is

Overview

This example demos the identification of a single boolean flags. If found the matching flag is removed as encountered. An ambiguous error will be generated if the flag appears more than once.

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

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

// 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 "true" flag which identified the flag's presence in 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 identifying a boolean flag.",
        os.Args,
    )

    isTrue := args.Is(
        "[-t | --true]", // A short and long form.
        "A single flag indicating true.",
    )

    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 {
        if isTrue {
            fmt.Println("Is true.")
        } else {
            fmt.Println("Is NOT true.")
        }
    }
}

Top of Page -- Szargs Contents

PASS: No Arguments


go run .
Is NOT true.

Top of Page -- Szargs Contents

PASS: Single Long Form


go run . --true
Is true.

Top of Page -- Szargs Contents

PASS: Single Short Form


go run . -t
Is true.

Top of Page -- Szargs Contents

FAIL: Ambiguous Argument

The error is because a true flag appeared more than once. A second error is generated by the args.Done() statement causing both the error and a usage statement to be returned.


go run . --true -t
Error: ambiguous argument: '[-t | --true]' found 2 times

usage: booleanIs [-t | --true]

A simple utility to demo identifying a boolean flag.

    [-t | --true]
        A single flag indicating true.

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 . --true extraUnknownArgument
Error: unexpected argument: [extraUnknownArgument]

usage: booleanIs [-t | --true]

A simple utility to demo identifying a boolean flag.

    [-t | --true]
        A single flag indicating true.

Top of Page -- Szargs Contents