Skip to content
/ ox Public

A context-based, generics enabled, zero dependency Go (and TinyGo!) command-line flag and argument parsing library

License

Notifications You must be signed in to change notification settings

xo/ox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xo/ox

xo/ox is a Go and TinyGo package for command-line argument and flag parsing.

Using | Example | Applications | About | License

Unit Tests Go Reference Discord Discussion

Features

  • 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 all scalars:
    • []byte, string, []rune, byte, rune
    • int64, int32, int16, int8, int
    • uint64, uint32, uint16, uint8, uint
    • float64, float32
    • complex128, complex64
  • Support for standard library types:
    • time.Time, time.Duration
    • *big.Int, *big.Float, *big.Rat
    • *url.URL
    • *netip.Addr, *netip.AddrPort, *netip.Prefix
  • 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 types:
    • ox.FormattedTime - formatted time values, set to a specific time.Layout
    • ox.CountT / type:count - incrementing counter, such as for verbosity -vvvv
    • ox.PathT / type:path - a file system path
    • ox.HookT - argument func hook
  • 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
  • Suggestions for command names, aliases, and suggested names
  • Argument validation and advanced shell completion support
  • TinyGo compatible

Using

Add to a Go project in the usual way:

$ go get -u github.com/xo/ox@latest

Example

Examples are available in the package overview examples, as well as in the _examples directory.

Applications

The following applications make use of the xo/ox package for command-line parsing:

  • usql - a universal command-line interface for SQL databases
  • xo - a templated code generator for databases
  • iv - a command-line terminal graphics image viewer
  • fv - a command-line terminal graphics font viewer
  • wallgrab - a Apple Aerial wallpaper downloader

About

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/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, 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/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:

Articles:

License

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.

About

A context-based, generics enabled, zero dependency Go (and TinyGo!) command-line flag and argument parsing library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published