docopt

package module
v0.0.0-...-d2978e3 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2013 License: MIT Imports: 6 Imported by: 0

README

docopt.go

Golang implementation of docopt 0.6.1+fix

Installation

import "github.com/docopt/docopt.go" and then run go get.

API

func docopt.Parse(doc string, argv []string, help bool, version string, optionsFirst bool)
(args map[string]interface{}, err error)

Parse argv based on command-line interface described in doc.

docopt creates your command-line interface based on its description that you pass as doc. Such description can contain --options, , commands, which could be [optional], (required), (mutually | exclusive) or repeated...

arguments

doc Description of your command-line interface.

argv Argument vector to be parsed. os.Args[1:] is used if nil.

help Set to false to disable automatic help on -h or --help options..

version If set to something besides an empty string, the string will be printed if --version is in argv.

optionsFirst Set to true to require options precede positional arguments, i.e. to forbid options and positional arguments intermix..

return values

args, map[string]interface{}. A map, where keys are names of command-line elements such as e.g. "--verbose" and "", and values are the parsed values of those elements. interface{} can be bool, int, string, []string.

err, error. Either *docopt.LanguageError, *docopt.UserError or nil

Example

package main

import (
    "fmt"
    "github.com/docopt/docopt.go"
)

func main() {
usage := `Naval Fate.

Usage:
  naval_fate ship new <name>...
  naval_fate ship <name> move <x> <y> [--speed=<kn>]
  naval_fate ship shoot <x> <y>
  naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate -h | --help
  naval_fate --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.`

    arguments, _ := docopt.Parse(usage, nil, true, "Naval Fate 2.0", false)
    fmt.Println(arguments)
}

Testing

All tests from the python version have been implemented and all are passing.

New language agnostic tests have been added to test_golang.docopt.

To run them use go test.

Documentation

Overview

Package docopt creates beautiful command-line interfaces based on the command help message

Port of docopt for python https://github.com/docopt/docopt http://docopt.org

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(doc string, argv []string, help bool, version string, optionsFirst bool) (map[string]interface{}, error)

Parse `argv` based on command-line interface described in `doc`.

docopt creates your command-line interface based on its description that you pass as `doc`. Such description can contain --options, <positional-argument>, commands, which could be [optional], (required), (mutually | exclusive) or repeated...

Parameters:

doc : string
 Description of your command-line interface.

argv : list of string or nil
 Argument vector to be parsed. os.Args[1:] is used if nil.

help : bool (default: true)
 Set to false to disable automatic help on -h or --help options.

version : string
 If set to something besides an empty string, the string will be printed
 if --version is in argv

optionsFirst : bool (default: false)
 Set to true to require options precede positional arguments,
 i.e. to forbid options and positional arguments intermix.

Returns:

args : map[string]interface{}
 A map, where keys are names of command-line elements
 such as e.g. "--verbose" and "<path>", and values are the
 parsed values of those elements.

err : error, *docopt.LanguageError, *docopt.UserError

os.Exit on user error or help

Example:

package main

import (
    "fmt"
    "github.com/docopt/docopt.go"
)

func main() {
    doc := `
Usage:
  my_program tcp <host> <port> [--timeout=<seconds>]
  my_program serial <port> [--baud=<n>] [--timeout=<seconds>]
  my_program (-h | --help | --version)

Options:
  -h, --help Show this screen and exit.
  --baud=<n> Baudrate [default: 9600]
`
    argv := []string{"tcp", "127.0.0.1", "80", "--timeout", "30"}
    fmt.Println(docopt.Parse(doc, argv, true, "", false))
}

Example output:

{"--baud": "9600",
 "--help": false,
 "--timeout": "30",
 "--version": false,
 "<host>": "127.0.0.1",
 "<port>": "8"',
 "serial": false,
 "tcp": true}

Types

type LanguageError

type LanguageError struct {
	// contains filtered or unexported fields
}

LanguageError records an error with the doc string.

func (LanguageError) Error

func (e LanguageError) Error() string

type UserError

type UserError struct {
	Usage string
	// contains filtered or unexported fields
}

UserError records an error with program arguments.

func (UserError) Error

func (e UserError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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