├── test_golang.docopt ├── examples ├── odd_even_example.go ├── quick_example.go ├── counted_example.go ├── calculator_example.go ├── options_shortcut_example.go ├── type_assert_example.go ├── naval_fate.go ├── arguments_example.go ├── git │ ├── git_remote.go │ ├── git_add.go │ ├── git_push.go │ ├── git_checkout.go │ ├── git_clone.go │ ├── git_branch.go │ └── git.go ├── options_example.go └── config_file_example.go ├── .gitignore ├── .travis.yml ├── example_test.go ├── LICENSE ├── README.md ├── testcases.docopt ├── docopt.go └── docopt_test.go /test_golang.docopt: -------------------------------------------------------------------------------- 1 | r"""usage: prog [NAME_-2]...""" 2 | $ prog 10 20 3 | {"NAME_-2": ["10", "20"]} 4 | 5 | $ prog 10 6 | {"NAME_-2": ["10"]} 7 | 8 | $ prog 9 | {"NAME_-2": []} 10 | -------------------------------------------------------------------------------- /examples/odd_even_example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func main() { 9 | usage := `Usage: odd_even_example [-h | --help] (ODD EVEN)... 10 | 11 | Example, try: 12 | odd_even_example 1 2 3 4 13 | 14 | Options: 15 | -h, --help` 16 | 17 | arguments, _ := docopt.Parse(usage, nil, true, "", false) 18 | fmt.Println(arguments) 19 | } 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | 24 | # coverage droppings 25 | profile.cov 26 | -------------------------------------------------------------------------------- /examples/quick_example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func main() { 9 | usage := `Usage: 10 | quick_example tcp [--timeout=] 11 | quick_example serial [--baud=9600] [--timeout=] 12 | quick_example -h | --help | --version` 13 | 14 | arguments, _ := docopt.Parse(usage, nil, true, "0.1.1rc", false) 15 | fmt.Println(arguments) 16 | } 17 | -------------------------------------------------------------------------------- /examples/counted_example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func main() { 9 | usage := `Usage: counted_example --help 10 | counted_example -v... 11 | counted_example go [go] 12 | counted_example (--path=)... 13 | counted_example 14 | 15 | Try: counted_example -vvvvvvvvvv 16 | counted_example go go 17 | counted_example --path ./here --path ./there 18 | counted_example this.txt that.txt` 19 | 20 | arguments, _ := docopt.Parse(usage, nil, true, "", false) 21 | fmt.Println(arguments) 22 | } 23 | -------------------------------------------------------------------------------- /examples/calculator_example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func main() { 9 | usage := `Not a serious example. 10 | 11 | Usage: 12 | calculator_example ( ( + | - | * | / ) )... 13 | calculator_example [( , )]... 14 | calculator_example (-h | --help) 15 | 16 | Examples: 17 | calculator_example 1 + 2 + 3 + 4 + 5 18 | calculator_example 1 + 2 '*' 3 / 4 - 5 # note quotes around '*' 19 | calculator_example sum 10 , 20 , 30 , 40 20 | 21 | Options: 22 | -h, --help 23 | ` 24 | arguments, _ := docopt.Parse(usage, nil, true, "", false) 25 | fmt.Println(arguments) 26 | } 27 | -------------------------------------------------------------------------------- /examples/options_shortcut_example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func main() { 9 | usage := `Example of program which uses [options] shortcut in pattern. 10 | 11 | Usage: 12 | options_shortcut_example [options] 13 | 14 | Options: 15 | -h --help show this help message and exit 16 | --version show version and exit 17 | -n, --number N use N as a number 18 | -t, --timeout TIMEOUT set timeout TIMEOUT seconds 19 | --apply apply changes to database 20 | -q operate in quiet mode` 21 | 22 | arguments, _ := docopt.Parse(usage, nil, true, "1.0.0rc2", false) 23 | fmt.Println(arguments) 24 | } 25 | -------------------------------------------------------------------------------- /examples/type_assert_example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func main() { 9 | usage := `usage: foo [-x] [-y]` 10 | 11 | arguments, err := docopt.Parse(usage, nil, true, "", false) 12 | if err != nil { 13 | fmt.Println(err) 14 | } 15 | 16 | fmt.Println(arguments) 17 | 18 | var x = arguments["-x"].(bool) // type assertion required 19 | if x == true { 20 | fmt.Println("x is true") 21 | } 22 | 23 | y := arguments["-y"] // no type assertion needed 24 | if y == true { 25 | fmt.Println("y is true") 26 | } 27 | y2 := arguments["-y"] 28 | if y2 == 10 { // this will never be true, a type assertion would have produced a build error 29 | fmt.Println("y is 10") 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /examples/naval_fate.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func main() { 9 | usage := `Naval Fate. 10 | 11 | Usage: 12 | naval_fate ship new ... 13 | naval_fate ship move [--speed=] 14 | naval_fate ship shoot 15 | naval_fate mine (set|remove) [--moored|--drifting] 16 | naval_fate -h | --help 17 | naval_fate --version 18 | 19 | Options: 20 | -h --help Show this screen. 21 | --version Show version. 22 | --speed= Speed in knots [default: 10]. 23 | --moored Moored (anchored) mine. 24 | --drifting Drifting mine.` 25 | 26 | arguments, _ := docopt.Parse(usage, nil, true, "Naval Fate 2.0", false) 27 | fmt.Println(arguments) 28 | } 29 | -------------------------------------------------------------------------------- /examples/arguments_example.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func main() { 9 | usage := `Usage: arguments_example [-vqrh] [FILE] ... 10 | arguments_example (--left | --right) CORRECTION FILE 11 | 12 | Process FILE and optionally apply correction to either left-hand side or 13 | right-hand side. 14 | 15 | Arguments: 16 | FILE optional input file 17 | CORRECTION correction angle, needs FILE, --left or --right to be present 18 | 19 | Options: 20 | -h --help 21 | -v verbose mode 22 | -q quiet mode 23 | -r make report 24 | --left use left-hand side 25 | --right use right-hand side` 26 | 27 | arguments, _ := docopt.Parse(usage, nil, true, "", false) 28 | fmt.Println(arguments) 29 | } 30 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Travis CI (http://travis-ci.org/) is a continuous integration 2 | # service for open source projects. This file configures it 3 | # to run unit tests for docopt-go. 4 | 5 | language: go 6 | 7 | go: 8 | - 1.2.2 9 | - tip 10 | 11 | matrix: 12 | fast_finish: true 13 | 14 | before_install: 15 | - go get code.google.com/p/go.tools/cmd/vet 16 | - go get -v github.com/golang/lint/golint 17 | - go get -v code.google.com/p/go.tools/cmd/cover 18 | - go get -v github.com/mattn/goveralls 19 | 20 | install: 21 | - go get -d -v ./... && go build -v . 22 | 23 | script: 24 | - go vet -x ./... 25 | - $HOME/gopath/bin/golint . 26 | - go test -covermode=count -coverprofile=profile.cov -v . 27 | 28 | after_script: 29 | - $HOME/gopath/bin/goveralls -coverprofile=profile.cov -service=travis-ci 30 | -------------------------------------------------------------------------------- /examples/git/git_remote.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func main() { 9 | usage := `usage: git remote [-v | --verbose] 10 | git remote add [-t ] [-m ] [-f] [--mirror] 11 | git remote rename 12 | git remote rm 13 | git remote set-head (-a | -d | ) 14 | git remote [-v | --verbose] show [-n] 15 | git remote prune [-n | --dry-run] 16 | git remote [-v | --verbose] update [-p | --prune] [( | )...] 17 | git remote set-branches [--add] ... 18 | git remote set-url [] 19 | git remote set-url --add 20 | git remote set-url --delete 21 | 22 | options: 23 | -v, --verbose be verbose; must be placed before a subcommand 24 | ` 25 | 26 | args, _ := docopt.Parse(usage, nil, true, "", false) 27 | fmt.Println(args) 28 | } 29 | -------------------------------------------------------------------------------- /example_test.go: -------------------------------------------------------------------------------- 1 | package docopt 2 | 3 | import ( 4 | "fmt" 5 | "sort" 6 | ) 7 | 8 | func ExampleParse() { 9 | usage := `Usage: 10 | config_example tcp [] [--force] [--timeout=] 11 | config_example serial [--baud=] [--timeout=] 12 | config_example -h | --help | --version` 13 | // parse the command line `comfig_example tcp 127.0.0.1 --force` 14 | argv := []string{"tcp", "127.0.0.1", "--force"} 15 | arguments, _ := Parse(usage, argv, true, "0.1.1rc", false) 16 | // sort the keys of the arguments map 17 | var keys []string 18 | for k := range arguments { 19 | keys = append(keys, k) 20 | } 21 | sort.Strings(keys) 22 | // print the argument keys and values 23 | for _, k := range keys { 24 | fmt.Printf("%9s %v\n", k, arguments[k]) 25 | } 26 | // output: 27 | // --baud 28 | // --force true 29 | // --help false 30 | // --timeout 31 | // --version false 32 | // -h false 33 | // 127.0.0.1 34 | // 35 | // serial false 36 | // tcp true 37 | } 38 | -------------------------------------------------------------------------------- /examples/git/git_add.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func cmdAdd(argv []string) (err error) { 9 | usage := `usage: git add [options] [--] [...] 10 | 11 | options: 12 | -h, --help 13 | -n, --dry-run dry run 14 | -v, --verbose be verbose 15 | -i, --interactive interactive picking 16 | -p, --patch select hunks interactively 17 | -e, --edit edit current diff and apply 18 | -f, --force allow adding otherwise ignored files 19 | -u, --update update tracked files 20 | -N, --intent-to-add record only the fact that the path will be added later 21 | -A, --all add all, noticing removal of tracked files 22 | --refresh don't add, only refresh the index 23 | --ignore-errors just skip files which cannot be added because of errors 24 | --ignore-missing check if - even missing - files are ignored in dry run 25 | ` 26 | 27 | args, _ := docopt.Parse(usage, nil, true, "", false) 28 | fmt.Println(args) 29 | return 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Keith Batten 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /examples/git/git_push.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func main() { 9 | usage := `usage: git push [options] [ [...]] 10 | 11 | options: 12 | -h, --help 13 | -v, --verbose be more verbose 14 | -q, --quiet be more quiet 15 | --repo repository 16 | --all push all refs 17 | --mirror mirror all refs 18 | --delete delete refs 19 | --tags push tags (can't be used with --all or --mirror) 20 | -n, --dry-run dry run 21 | --porcelain machine-readable output 22 | -f, --force force updates 23 | --thin use thin pack 24 | --receive-pack 25 | receive pack program 26 | --exec 27 | receive pack program 28 | -u, --set-upstream set upstream for git pull/status 29 | --progress force progress reporting 30 | ` 31 | 32 | args, _ := docopt.Parse(usage, nil, true, "", false) 33 | fmt.Println(args) 34 | } 35 | -------------------------------------------------------------------------------- /examples/git/git_checkout.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/docopt/docopt-go" 6 | ) 7 | 8 | func main() { 9 | usage := `usage: git checkout [options] 10 | git checkout [options] -- ... 11 | 12 | options: 13 | -q, --quiet suppress progress reporting 14 | -b create and checkout a new branch 15 | -B create/reset and checkout a branch 16 | -l create reflog for new branch 17 | -t, --track set upstream info for new branch 18 | --orphan 19 | new unparented branch 20 | -2, --ours checkout our version for unmerged files 21 | -3, --theirs checkout their version for unmerged files 22 | -f, --force force checkout (throw away local modifications) 23 | -m, --merge perform a 3-way merge with the new branch 24 | --conflict