add more examples from docopt python
diff --git a/examples/arguments_example.go b/examples/arguments_example.go
new file mode 100644
index 0000000..5b2c079
--- /dev/null
+++ b/examples/arguments_example.go
@@ -0,0 +1,29 @@
+package main
+
+import (
+ "fmt"
+ "github.com/docopt/docopt.go"
+)
+
+func main() {
+ usage := `Usage: arguments_example [-vqrh] [FILE] ...
+ arguments_example (--left | --right) CORRECTION FILE
+
+Process FILE and optionally apply correction to either left-hand side or
+right-hand side.
+
+Arguments:
+ FILE optional input file
+ CORRECTION correction angle, needs FILE, --left or --right to be present
+
+Options:
+ -h --help
+ -v verbose mode
+ -q quiet mode
+ -r make report
+ --left use left-hand side
+ --right use right-hand side`
+
+ arguments, _ := docopt.Parse(usage, nil, true, "", false)
+ fmt.Println(arguments)
+}
diff --git a/examples/calculator_example.go b/examples/calculator_example.go
new file mode 100644
index 0000000..1eb56c0
--- /dev/null
+++ b/examples/calculator_example.go
@@ -0,0 +1,26 @@
+package main
+
+import (
+ "fmt"
+ "github.com/docopt/docopt.go"
+)
+
+func main() {
+ usage := `Not a serious example.
+
+Usage:
+ calculator_example <value> ( ( + | - | * | / ) <value> )...
+ calculator_example <function> <value> [( , <value> )]...
+ calculator_example (-h | --help)
+
+Examples:
+ calculator_example 1 + 2 + 3 + 4 + 5
+ calculator_example 1 + 2 '*' 3 / 4 - 5 # note quotes around '*'
+ calculator_example sum 10 , 20 , 30 , 40
+
+Options:
+ -h, --help
+`
+ arguments, _ := docopt.Parse(usage, nil, true, "", false)
+ fmt.Println(arguments)
+}
diff --git a/examples/config_file_example.go b/examples/config_file_example.go
new file mode 100644
index 0000000..e039fa7
--- /dev/null
+++ b/examples/config_file_example.go
@@ -0,0 +1,76 @@
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "github.com/docopt/docopt.go"
+ "strings"
+)
+
+func loadJsonConfig() map[string]interface{} {
+ var result map[string]interface{}
+ jsonData := []byte(`{"--force": true, "--timeout": "10", "--baud": "9600"}`)
+ json.Unmarshal(jsonData, &result)
+ return result
+}
+
+func loadIniConfig() map[string]interface{} {
+ iniData := `
+[default-arguments]
+--force
+--baud=19200
+<host>=localhost`
+ // trivial ini parser
+ // default value for an item is bool: true (for --force)
+ // otherwise the value is a string
+ iniParsed := make(map[string]map[string]interface{})
+ var section string
+ for _, line := range strings.Split(iniData, "\n") {
+ if strings.HasPrefix(line, "[") {
+ section = line
+ iniParsed[section] = make(map[string]interface{})
+ } else if section != "" {
+ kv := strings.SplitN(line, "=", 2)
+ if len(kv) == 1 {
+ iniParsed[section][kv[0]] = true
+ } else if len(kv) == 2 {
+ iniParsed[section][kv[0]] = kv[1]
+ }
+ }
+ }
+ return iniParsed["[default-arguments]"]
+}
+
+// merge combines two maps.
+// truthiness takes priority over falsiness
+// mapA takes priority over mapB
+func merge(mapA, mapB map[string]interface{}) map[string]interface{} {
+ result := make(map[string]interface{})
+ for k, v := range mapA {
+ result[k] = v
+ }
+ for k, v := range mapB {
+ if _, ok := result[k]; !ok || result[k] == nil || result[k] == false {
+ result[k] = v
+ }
+ }
+ return result
+}
+
+func main() {
+ usage := `Usage:
+ config_file_example tcp [<host>] [--force] [--timeout=<seconds>]
+ config_file_example serial <port> [--baud=<rate>] [--timeout=<seconds>]
+ config_file_example -h | --help | --version`
+
+ jsonConfig := loadJsonConfig()
+ iniConfig := loadIniConfig()
+ arguments, _ := docopt.Parse(usage, nil, true, "0.1.1rc", false)
+
+ // Arguments take priority over INI, INI takes priority over JSON
+ result := merge(arguments, merge(iniConfig, jsonConfig))
+
+ fmt.Println("JSON config: ", jsonConfig)
+ fmt.Println("INI config: ", iniConfig)
+ fmt.Println("Result: ", result)
+}
diff --git a/examples/counted_example.go b/examples/counted_example.go
new file mode 100644
index 0000000..95877a9
--- /dev/null
+++ b/examples/counted_example.go
@@ -0,0 +1,22 @@
+package main
+
+import (
+ "fmt"
+ "github.com/docopt/docopt.go"
+)
+
+func main() {
+ usage := `Usage: counted_example --help
+ counted_example -v...
+ counted_example go [go]
+ counted_example (--path=<path>)...
+ counted_example <file> <file>
+
+Try: counted_example -vvvvvvvvvv
+ counted_example go go
+ counted_example --path ./here --path ./there
+ counted_example this.txt that.txt`
+
+ arguments, _ := docopt.Parse(usage, nil, true, "", false)
+ fmt.Println(arguments)
+}
diff --git a/examples/odd_even_example.go b/examples/odd_even_example.go
new file mode 100644
index 0000000..9cc021b
--- /dev/null
+++ b/examples/odd_even_example.go
@@ -0,0 +1,19 @@
+package main
+
+import (
+ "fmt"
+ "github.com/docopt/docopt.go"
+)
+
+func main() {
+ usage := `Usage: odd_even_example [-h | --help] (ODD EVEN)...
+
+Example, try:
+ odd_even_example 1 2 3 4
+
+Options:
+ -h, --help`
+
+ arguments, _ := docopt.Parse(usage, nil, true, "", false)
+ fmt.Println(arguments)
+}
diff --git a/examples/options_example.go b/examples/options_example.go
new file mode 100644
index 0000000..87367e8
--- /dev/null
+++ b/examples/options_example.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+ "fmt"
+ "github.com/docopt/docopt.go"
+)
+
+func main() {
+ usage := `Example of program with many options using docopt.
+
+Usage:
+ options_example [-hvqrf NAME] [--exclude=PATTERNS]
+ [--select=ERRORS | --ignore=ERRORS] [--show-source]
+ [--statistics] [--count] [--benchmark] PATH...
+ options_example (--doctest | --testsuite=DIR)
+ options_example --version
+
+Arguments:
+ PATH destination path
+
+Options:
+ -h --help show this help message and exit
+ --version show version and exit
+ -v --verbose print status messages
+ -q --quiet report only file names
+ -r --repeat show all occurrences of the same error
+ --exclude=PATTERNS exclude files or directories which match these comma
+ separated patterns [default: .svn,CVS,.bzr,.hg,.git]
+ -f NAME --file=NAME when parsing directories, only check filenames matching
+ these comma separated patterns [default: *.go]
+ --select=ERRORS select errors and warnings (e.g. E,W6)
+ --ignore=ERRORS skip errors and warnings (e.g. E4,W)
+ --show-source show source code for each error
+ --statistics count errors and warnings
+ --count print total number of errors and warnings to standard
+ error and set exit code to 1 if total is not null
+ --benchmark measure processing speed
+ --testsuite=DIR run regression tests from dir
+ --doctest run doctest on myself`
+
+ arguments, _ := docopt.Parse(usage, nil, true, "1.0.0rc2", false)
+ fmt.Println(arguments)
+}
diff --git a/examples/options_shortcut_example.go b/examples/options_shortcut_example.go
new file mode 100644
index 0000000..2369f3b
--- /dev/null
+++ b/examples/options_shortcut_example.go
@@ -0,0 +1,24 @@
+package main
+
+import (
+ "fmt"
+ "github.com/docopt/docopt.go"
+)
+
+func main() {
+ usage := `Example of program which uses [options] shortcut in pattern.
+
+Usage:
+ options_shortcut_example [options] <port>
+
+Options:
+ -h --help show this help message and exit
+ --version show version and exit
+ -n, --number N use N as a number
+ -t, --timeout TIMEOUT set timeout TIMEOUT seconds
+ --apply apply changes to database
+ -q operate in quiet mode`
+
+ arguments, _ := docopt.Parse(usage, nil, true, "1.0.0rc2", false)
+ fmt.Println(arguments)
+}
diff --git a/examples/quick_example.go b/examples/quick_example.go
new file mode 100644
index 0000000..6d34959
--- /dev/null
+++ b/examples/quick_example.go
@@ -0,0 +1,16 @@
+package main
+
+import (
+ "fmt"
+ "github.com/docopt/docopt.go"
+)
+
+func main() {
+ usage := `Usage:
+ quick_example tcp <host> <port> [--timeout=<seconds>]
+ quick_example serial <port> [--baud=9600] [--timeout=<seconds>]
+ quick_example -h | --help | --version`
+
+ arguments, _ := docopt.Parse(usage, nil, true, "0.1.1rc", false)
+ fmt.Println(arguments)
+}