Skip to content

pborman/options

Folders and files

NameName
Last commit message
Last commit date

Latest commit

286675e · Dec 22, 2023

History

42 Commits
Sep 9, 2022
Dec 22, 2023
Mar 28, 2019
Dec 13, 2023
Jan 1, 2023
Jul 29, 2018
Apr 9, 2019
Aug 17, 2020
Aug 17, 2020
Aug 17, 2020
Aug 17, 2020
Dec 13, 2023
Dec 13, 2023
Apr 9, 2019
Aug 17, 2020
Mar 12, 2023
Mar 12, 2023
Aug 17, 2020
Aug 17, 2020
Aug 17, 2020

Repository files navigation

options build status GoDoc

Structured getopt processing for Go programs using the github.com/pborman/getopt/v2 package.

The options package makes adding getopt style command line options to Go programs as easy as declaring a structure:

package main

import (
	"fmt"
	"time"

	"github.com/pborman/options"
)

var opts = struct {
	Help    options.Help  `getopt:"--help           display help"`
	Name    string        `getopt:"--name=NAME      name of the widget"`
	Count   int           `getopt:"--count -c=COUNT number of widgets"`
	Verbose bool          `getopt:"-v               be verbose"`
	N       int           `getopt:"-n=NUMBER        set n to NUMBER"`
	Timeout time.Duration `getopt:"--timeout        duration of run"`
	Lazy    string
}{
	Name: "gopher",
}

func main() {
	args := options.RegisterAndParse(&opts)

	if opts.Verbose {
		fmt.Printf("Command line parameters: %q\n", args)
	}
	fmt.Printf("Name: %s\n", opts.Name)
}

The options.Help type causes the command's usage to be displayed to standard error and the command to exit when the option is parsed from the command line.

The options package also supports reading options from file specified on the command line or an optional defaults file:

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/pborman/options"
)

var opts = struct {
	Flags   options.Flags `getopt:"--flags=PATH     read options from PATH"`
	Name    string        `getopt:"--name=NAME      name of the widget"`
	Count   int           `getopt:"--count -c=COUNT number of widgets"`
	Verbose bool          `getopt:"-v               be verbose"`
	N       int           `getopt:"-n=NUMBER        set n to NUMBER"`
	Timeout time.Duration `getopt:"--timeout        duration of run"`
	Lazy    string
}{
	Name: "gopher",
}

func main() {
	options.Register(&opts)
	// read defaults from ~/.example.flags if the file exists.
	if err := opts.Flags.Set("?${HOME}/.example.flags", nil); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	args := options.Parse()

	if opts.Verbose {
		fmt.Printf("Command line parameters: %q\n", args)
	}
	fmt.Printf("Name: %s\n", opts.Name)
}

Using the following .example.flags file in your home directory that contains:

v = true
name = "github user"

The above program produces the following output:

$ go run x.go a parameter
Command line parameters: ["a" "parameter"]
Name: github user

$ go run x.go --help     
unknown option: --help
Usage: x [-v] [-c COUNT] [--flags PATH] [--lazy value] [-n NUMBER] [--name NAME] [--timeout value] [parameters ...]
 -c, --count=COUNT  number of widgets
     --flags=PATH   read options from PATH
     --lazy=value   unspecified
 -n NUMBER          set n to NUMBER
     --name=NAME    name of the widget [gopher]
     --timeout=value
                    duration of run
 -v                 be verbose
exit status 1