opts

package
v0.0.0-...-bc19bdb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2016 License: MIT, MIT Imports: 18 Imported by: 0

README

opts

A low friction command-line interface library for Go (Golang)

GoDoc

Command-line parsing should be easy. Use configuration structs:

package main

import (
	"fmt"

	"github.com/jpillora/opts"
)

func main() {
	config := struct {
		File  string `help:"file to load"`
		Lines int    `help:"number of lines to show"`
	}{}
	opts.Parse(&config)
	fmt.Println(config)
}
$ go run main.go -f foo -l 12
{foo 12}
Features (with examples)
  • Easy to use (simple)
  • Promotes separation of CLI code and library code (separation)
  • Automatically generated --help text via struct tags help:"Foo bar" (help)
  • Subcommands by nesting structs (subcmds)
  • Default values by modifying the struct prior to Parse() (defaults)
  • Default values from a JSON config file, unmarshalled via your config struct (config)
  • Default values from environment, defined by your field names (env)
  • Infers program name from package name (and optional repository link)
  • Extensible via flag.Value (customtypes)
  • Customizable help text by modifying the default templates (customhelp)
Overview

Internally, opts creates flag.FlagSets from your configuration structs using pkg/reflect. So, given the following program:

type Config struct {
	Alpha   string        `help:"a string"`
	Bravo   int           `help:"an int"`
	Charlie bool          `help:"a bool"`
	Delta   time.Duration `help:"a duration"`
}

c := Config{
	Bravo: 42,
	Delta: 2 * time.Minute,
}

opts.Parse(&c)

opts would approximately perform:

foo := Config{}
set := flag.NewFlagSet("Config")
set.StringVar(&foo.Alpha, "", "a string")
set.IntVar(&foo.Bravo, 42, "an int")
set.BoolVar(&foo.Charlie, false, "a bool")
set.DurationVar(&foo.Delta, 2 * time.Minute, "a duration")
set.Parse(os.Args)

And, you get pretty --help output:

$ ./foo --help

  Usage: foo [options]

  Options:
  --alpha, -a    a string
  --bravo, -b    an int (default 42)
  --charlie, -c  an bool
  --delta, -d    a duration (default 2m0s)
  --help, -h

All Examples

See all examples here

Struct Tag API
Common tags

These tags are usable across all types:

  • name - Name is used to display the field in the help text (defaults to the field name converted to lowercase and dashes)
  • help - Help is used to describe the field (defaults to "")
  • type - The opts type assigned the field (defaults using the table below)
type defaults

All fields will have a type. By default a struct field will be assigned a type depending on its field type:

Field Type Default type Valid types
int opt opt, arg
string opt opt, arg, cmdname
bool opt opt, arg
flag.Value opt opt, arg
time.Duration opt opt, arg
[]string arglist arglist
struct subcmd subcmd, embedded

This default assignment can be overridden with a type struct tag. For example you could set a string struct field to be an arg field with type:"arg".

type specific properties
  • opt

    An option (opt) field will appear in the options list and by definition, be optional.

    • short - An alias or shortcut to this option (defaults to the first letter of the name property)
    • env - An environment variable to use to retrieve the default (when UseEnv() is set this defaults to name property converted to uppercase and underscores)

    Restricted to fields with type int,string,bool,time.Duration and flag.Value

  • arg

    An argument (arg) field will appear in the usage and will be required if it does not have a default value set.

    Restricted to fields with type string

  • arglist

    An argument list (arglist) field will appear in the usage. Useful for a you allow any number number of arguments. For example file and directory targets.

    • min - An integer representing the minimum number of args specified

    Restricted to fields with type []string

  • subcmd

    A subcommand is nested opts.Opt instance, so its fields behave in exactly the same way as the parent struct.

    You can access the options of a subcommand with prog --prog-opt X subcmd --subcmd-opt Y

    Restricted to fields with type struct

  • cmdname

    A special type which will assume the name of the selected subcommand

    Restricted to fields with type string

  • embedded

    A special type which causes the fields of struct to be used in the current struct. Useful if you want to extend existing structs with extra command-line options.

Other projects

Other CLI libraries which infer flags from struct tags:

Todo
  • More tests
  • Option groups (Separate sets of options in --help)
  • Bash completion
  • Multiple short options -aux (Requires a non-pkg/flag parser)
MIT License

Copyright © 2015 <dev@jpillora.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOrder = []string{
	"usage",
	"args",
	"arglist",
	"options",
	"subcmds",
	"author",
	"version",
	"repo",
	"errmsg",
}
View Source
var DefaultTemplates = map[string]string{

	"help": `{{ $root := . }}` +
		`{{range $t := .Order}}{{ templ $t $root }}{{end}}`,

	"usage": `Usage: {{.Name }}` +
		`{{template "usageoptions" .}}` +
		`{{template "usageargs" .}}` +
		`{{template "usagearglist" .}}` +
		`{{template "usagesubcmd" .}}` + "\n",
	"usageoptions": `{{if .Opts}} [options]{{end}}`,
	"usageargs":    `{{range .Args}} {{.Name}}{{end}}`,
	"usagearglist": `{{if .ArgList}} {{.ArgList.Name}}{{end}}`,
	"usagesubcmd":  `{{if .Subcmds}} <subcommand>{{end}}`,

	"helpextra": `{{if .Def}}default {{.Def}}{{end}}` +
		`{{if and .Def .Env}}, {{end}}` +
		`{{if .Env}}env {{.Env}}{{end}}`,

	"args":    `{{range .Args}}{{template "arg" .}}{{end}}`,
	"arg":     "{{if .Help}}\n{{.Help}}\n{{end}}",
	"arglist": "{{if .ArgList}}{{ if .ArgList.Help}}\n{{.ArgList.Help}}\n{{end}}{{end}}",

	"options": `{{if .Opts}}` + "\nOptions:\n" +
		`{{ range $opt := .Opts}}{{template "option" $opt}}{{end}}{{end}}`,
	"option": `{{.Name}}{{if .Help}}{{.Pad}}{{.Help}}{{end}}` + "\n",

	"subcmds": "{{if .Subcmds}}\nSubcommands:\n" +
		`{{ range $sub := .Subcmds}}{{template "subcmd" $sub}}{{end}}{{end}}`,
	"subcmd": "* {{ .Name }}{{if .Help}} - {{ .Help }}{{end}}\n",

	"version": "{{if .Version}}\nVersion:\n{{.Pad}}{{.Version}}\n{{end}}",
	"repo":    "{{if .Repo}}\nRead more:\n{{.Pad}}{{.Repo}}\n{{end}}",
	"author":  "{{if .Author}}\nAuthor:\n{{.Pad}}{{.Author}}\n{{end}}",
	"errmsg":  "{{if .ErrMsg}}\nError:\n{{.Pad}}{{.ErrMsg}}\n{{end}}",
}

Functions

This section is empty.

Types

type Opts

type Opts struct {

	//public format settings
	LineWidth int  //42
	PadAll    bool //true
	PadWidth  int  //2
	// contains filtered or unexported fields
}

Opts is the main class, it contains all parsing state for a single set of arguments

func New

func New(config interface{}) *Opts

New creates a new Opts instance

func Parse

func Parse(config interface{}) *Opts

Creates a new Opts instance and Parses it

func (*Opts) Author

func (o *Opts) Author(author string) *Opts

func (*Opts) ConfigPath

func (o *Opts) ConfigPath(path string) *Opts

ConfigPath defines a path to a JSON file which matches the structure of the provided config. Environment variables override JSON Config variables.

func (*Opts) DocAfter

func (o *Opts) DocAfter(target, newid, template string) *Opts

DocAfter inserts a text block after the specified template

func (*Opts) DocBefore

func (o *Opts) DocBefore(target, newid, template string) *Opts

DocBefore inserts a text block before the specified template

func (*Opts) DocSet

func (o *Opts) DocSet(id, template string) *Opts

DecSet replaces the specified template

func (*Opts) Help

func (o *Opts) Help() string

func (*Opts) Name

func (o *Opts) Name(name string) *Opts

func (*Opts) Parse

func (o *Opts) Parse() *Opts

Parse with os.Args

func (*Opts) ParseArgs

func (o *Opts) ParseArgs(args []string) *Opts

ParseArgs with the provided arguments

func (*Opts) PkgAuthor

func (o *Opts) PkgAuthor() *Opts

func (*Opts) PkgRepo

func (o *Opts) PkgRepo() *Opts

func (*Opts) Process

func (o *Opts) Process(args []string) error

Process is the same as ParseArgs except it returns an error on failure

func (*Opts) Repo

func (o *Opts) Repo(repo string) *Opts

func (*Opts) UseEnv

func (o *Opts) UseEnv() *Opts

ConfigPath defines a path to a JSON file which matches the structure of the provided config. Environment variables override JSON Config variables.

func (*Opts) Version

func (o *Opts) Version(version string) *Opts

Directories

Path Synopsis
example
arg
env

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL