You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.5 KiB

  1. docopt-go
  2. =========
  3. [![Build Status](https://travis-ci.org/docopt/docopt.go.svg?branch=master)](https://travis-ci.org/docopt/docopt.go)
  4. [![Coverage Status](https://coveralls.io/repos/docopt/docopt.go/badge.png)](https://coveralls.io/r/docopt/docopt.go)
  5. [![GoDoc](https://godoc.org/github.com/docopt/docopt.go?status.png)](https://godoc.org/github.com/docopt/docopt.go)
  6. An implementation of [docopt](http://docopt.org/) in the
  7. [Go](http://golang.org/) programming language.
  8. **docopt** helps you create *beautiful* command-line interfaces easily:
  9. ```go
  10. package main
  11. import (
  12. "fmt"
  13. "github.com/docopt/docopt-go"
  14. )
  15. func main() {
  16. usage := `Naval Fate.
  17. Usage:
  18. naval_fate ship new <name>...
  19. naval_fate ship <name> move <x> <y> [--speed=<kn>]
  20. naval_fate ship shoot <x> <y>
  21. naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
  22. naval_fate -h | --help
  23. naval_fate --version
  24. Options:
  25. -h --help Show this screen.
  26. --version Show version.
  27. --speed=<kn> Speed in knots [default: 10].
  28. --moored Moored (anchored) mine.
  29. --drifting Drifting mine.`
  30. arguments, _ := docopt.Parse(usage, nil, true, "Naval Fate 2.0", false)
  31. fmt.Println(arguments)
  32. }
  33. ```
  34. **docopt** parses command-line arguments based on a help message. Don't
  35. write parser code: a good help message already has all the necessary
  36. information in it.
  37. ## Installation
  38. ⚠ Use the alias “docopt-go”. To use docopt in your Go code:
  39. ```go
  40. import "github.com/docopt/docopt-go"
  41. ```
  42. To install docopt according to your `$GOPATH`:
  43. ```console
  44. $ go get github.com/docopt/docopt-go
  45. ```
  46. ## API
  47. ```go
  48. func Parse(doc string, argv []string, help bool, version string,
  49. optionsFirst bool, exit ...bool) (map[string]interface{}, error)
  50. ```
  51. Parse `argv` based on the command-line interface described in `doc`.
  52. Given a conventional command-line help message, docopt creates a parser and
  53. processes the arguments. See
  54. https://github.com/docopt/docopt#help-message-format for a description of the
  55. help message format. If `argv` is `nil`, `os.Args[1:]` is used.
  56. docopt returns a map of option names to the values parsed from `argv`, and an
  57. error or `nil`.
  58. More documentation for docopt is available at
  59. [GoDoc.org](https://godoc.org/github.com/docopt/docopt.go).
  60. ## Testing
  61. All tests from the Python version are implemented and passing
  62. at [Travis CI](https://travis-ci.org/docopt/docopt.go). New
  63. language-agnostic tests have been added
  64. to [test_golang.docopt](test_golang.docopt).
  65. To run tests for docopt-go, use `go test`.