Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A flexible parser for command line options

goptions implements a flexible parser for command line options.

Key targets were the support for both long and short flag versions, mutually exclusive flags, and verbs. Flags and their corresponding variables are defined by the tags in a (possibly anonymous) struct.


package main

import (

func main() {
    options := struct {
        Server   string        `goptions:"-s, --server, obligatory, description='Server to connect to'"`
        Password string        `goptions:"-p, --password, description='Don\\'t prompt for password'"`
        Timeout  time.Duration `goptions:"-t, --timeout, description='Connection timeout in seconds'"`
        Help     goptions.Help `goptions:"-h, --help, description='Show this help'"`

        Execute struct {
            Command string   `goptions:"--command, mutexgroup='input', description='Command to exectute', obligatory"`
            Script  *os.File `goptions:"--script, mutexgroup='input', description='Script to exectute', rdonly"`
        } `goptions:"execute"`
        Delete struct {
            Path  string `goptions:"-n, --name, obligatory, description='Name of the entity to be deleted'"`
            Force bool   `goptions:"-f, --force, description='Force removal'"`
        } `goptions:"delete"`
    }{ // Default values goes here
        Timeout: 10 * time.Second,
$ go run examples/readme_example.go --help
Usage: a.out [global options] <verb> [verb options]

Global options:
        -s, --server   Server to connect to (*)
        -p, --password Don't prompt for password
        -t, --timeout  Connection timeout in seconds (default: 10s)
        -h, --help     Show this help

        -n, --name     Name of the entity to be deleted (*)
        -f, --force    Force removal
            --command  Command to exectute (*)
            --script   Script to exectute

Version 2.5.6

Something went wrong with that request. Please try again.