xo/ox is a Go and TinyGo package for command-line argument and flag parsing.
Using | Example | Applications | About | License
- Long (
--arg,--arg val) and Short (-a,-a val) flag parsing - POSIX-style/compatible flag parsing (
-vvv,-mfoo=bar-m foo=bar,--map=foo=bar) - Flags can have optional arguments (via
NoArg) and type specific defaults - Full command tree and sub command heirarchy
- Support for
builtintypes:[]byte,string,[]rune,byte,runeint64,int32,int16,int8,intuint64,uint32,uint16,uint8,uintfloat64,float32complex128,complex64
- Support for standard library types:
time.Time,time.Duration*big.Int,*big.Float,*big.Rat*url.URL,*regexp.Regexp*netip.Addr,*netip.AddrPort,*netip.Prefix
- Non-standard types:
ox.Size- a byte size (15 MiB,1 GB, ...)ox.Rate- a byte rate (15 MiB/s,1 GB/h, ...)
- Support for compound types of all above (slices/maps):
[]int,[][]byte,[]string,[]float64,[]*big.Int, etc.map[string]string,map[int]string,map[float64]*url.URL, etc.
- Additional type support:
ox.DateTimeT,ox.DateT,ox.TimeT/type:datetime,type:date,type:time- standard dates and timesox.FormattedTime- anytime.Timevalue using anytime.Layoutformatox.CountT/type:count- incrementing counter, such as for verbosity-vvvvox.Base64T- a base64 encoded stringox.HexT- a hex encoded stringox.PathT/type:path- a file system pathox.HookT- argumentfunchook, for hooking flags
- Optional, common types, available with optional import:
*github.com/google/uuid.UUID- standard UUID's*github.com/kenshaw/colors.Color- named and css style colors (white,black,#ffffff,RGBA(...), ...)*github.com/kenshaw/glob.Glob- a file path globbing type
- Registerable user defined types, which work with all API styles
- Testable commands/sub-commands
- Simple/flexible APIs for Reflection, Bind, and Context style use cases
- Generics used where it makes sense
- Fast
- Environment, YAML, TOML, HCL config loading
- Deferred default value expansion
- Standard help, version and shell completion
- Command, argument, and flag completion
- Suggestions for command names, aliases, and suggested names
- Argument validation and advanced shell completion support
- TinyGo compatible
Add to a Go project in the usual way:
$ go get -u github.com/xo/ox@latestExamples are available in the package overview examples, as
well as in the _examples directory.
The following applications make use of the xo/ox package for command-line
parsing:
usql- a universal command-line interface for SQL databasesxo- a templated code generator for databasesiv- a command-line terminal graphics image viewerfv- a command-line terminal graphics font viewerwallgrab- a Apple Aerial wallpaper downloader
ox aims to provide a robust and simple command-line package for the most
common command-line use-cases in Go.
ox was built to streamline/simplify complexity found in the powerful (and
popular!) cobra/ pflag/viper combo. ox is
written in pure Go, with no non-standard package dependencies, and provides a
robust, extensible type system, as well as configuration loaders for
YAML, TOML, HCL that can be optionally enabled/disabled
through imports.
ox avoids "magic", and has sane, sensible defaults. No interfaces, type
members or other internal logic is hidden or obscured. When using ox, the
user can manually build commands and flags however they see fit.
Wherever a non-standard package has been used, such as for the YAML,
TOML, or HCL loaders, or for the built-in support for
colors, globs, and UUIDs, the external dependencies
are optional, requiring a import of a xo/ox subpackage, for example:
import (
// base package
"github.com/xo/ox"
// import config loaders
_ "github.com/xo/ox/hcl"
_ "github.com/xo/ox/toml"
_ "github.com/xo/ox/yaml"
// well-known types
_ "github.com/xo/ox/color"
_ "github.com/xo/ox/glob"
_ "github.com/xo/ox/uuid"
)ox has been designed to use generics, and is built with Go 1.23+ applications
in mind and works with TinyGo.
Specific design considerations of the ox package:
- Constrained "good enough" feature set, no ambition to support every use case/scenario
- No magic, sane defaults, overrideable defaults
- Functional option and interface smuggling
- Use generics, iterators and other go1.23+ features where prudent
- Work with TinyGo out of the box
- Minimal use of reflection (unless TinyGo supports it)
- Case sensitive
- Enable registration for config file loaders, types with minimal hassle
- Man page generation
- Optional support for common use-cases, via package dependencies
Other command-line packages:
- spf13/cobra + spf13/viper + spf13/pflag
- urfave/cli
- alecthomas/kong
- alecthomas/kingpin
- jessevdk/go-flags
- mow.cli
- peterbourgon/ff
Articles:
xo/ox is licensed under the MIT License. ox's completion scripts
are originally cribbed from the cobra project, and are made available
under the Apache License.