commit | 581273ae7a285cb880499d787ce5b298b49430aa | [log] [tgz] |
---|---|---|
author | Matt Boersma <matt@sprout.org> | Sat Apr 19 10:01:48 2014 -0600 |
committer | Matt Boersma <matt@sprout.org> | Sat Apr 19 12:50:18 2014 -0600 |
tree | fa680f8465e765c71538a95bacfe8ff4fc981814 | |
parent | f091e9eb02a6cbcfb1cafa388c6707aa845afdc9 [diff] |
fix(exit): add optional "exit" final arg to Parse() Users of docopt.go should be able to handle their own return codes and os.Exit() processing. Calling the Parse() func with an extra last arg of "false" means docopt.go will not call os.Exit(). Additionally, the docopt_test.go suite was changed to call the exported Parse() func instead of the parse() implementation. Fixes #7.
Golang implementation of docopt 0.6.1+fix
import “github.com/docopt/docopt-go” and then run go get
.
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...
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..
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
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)
}
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
.