File: example_test.go

package info (click to toggle)
golang-github-docopt-docopt-go 0.6.2+git20160216.0.784ddc5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 220 kB
  • ctags: 180
  • sloc: makefile: 11
file content (37 lines) | stat: -rw-r--r-- 891 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package docopt

import (
	"fmt"
	"sort"
)

func ExampleParse() {
	usage := `Usage:
  config_example tcp [<host>] [--force] [--timeout=<seconds>]
  config_example serial <port> [--baud=<rate>] [--timeout=<seconds>]
  config_example -h | --help | --version`
	// parse the command line `comfig_example tcp 127.0.0.1 --force`
	argv := []string{"tcp", "127.0.0.1", "--force"}
	arguments, _ := Parse(usage, argv, true, "0.1.1rc", false)
	// sort the keys of the arguments map
	var keys []string
	for k := range arguments {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	// print the argument keys and values
	for _, k := range keys {
		fmt.Printf("%9s %v\n", k, arguments[k])
	}
	// output:
	//    --baud <nil>
	//   --force true
	//    --help false
	// --timeout <nil>
	// --version false
	//        -h false
	//    <host> 127.0.0.1
	//    <port> <nil>
	//    serial false
	//       tcp true
}