🚀 Launch Week Day 5:Introducing Immutable Scans.Learn More →
Socket
Book a DemoInstallSign in
Socket

sircmpwn.srht.site/getopt

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sircmpwn.srht.site/getopt

Go Modules
Version
v1.0.0
Version published
Created
Source

getopt godoc builds.sr.ht status

A POSIX-compatible getopt implementation for Go, because POSIX getopt is The Correct Way to interpret arguments to command line utilities.

Please send patches/bugs/feedback to ~sircmpwn/public-inbox@lists.sr.ht.

Example Usage

import (
	"os"
	"git.sr.ht/~sircmpwn/getopt"
)

func main() {
	opts, optind, err := getopt.Getopts(os.Args, "abc:d:")
	if err != nil {
		panic(err)
	}
	for _, opt := range opts {
		switch opt.Option {
		case 'a':
			println("Option -a specified")
		case 'b':
			println("Option -b specified")
		case 'c':
			println("Option -c specified: " + opt.Value)
		case 'd':
			println("Option -d specified: " + opt.Value)
		}
	}
	println("Remaining arguments:")
	for _, arg := range os.Args[optind:] {
		println(arg)
	}
}

A flag-like interface is also supported.

import (
	"git.sr.ht/~sircmpwn/getopt"
)

func main() {
	a := getopt.Bool("a", false, "turn on option a")
	b := getopt.Int("b", 1, "set b to a numerical value")
	var opt string
	getopt.StringVar(&opt, "c", "", "let c be specified string")

	err := getopt.Parse()
	if err != nil {
		panic(err)
	}

	print("Value of a: ")
	println(*a)
	print("Value of b: ")
	println(*b)
	println("Value of c: " + opt)

	println("Remaining arguments:")
	for _, arg := range getopt.Args() {
		println(arg)
	}
}

FAQs

Package last updated on 18 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts