Skip to content

Commit d513d45

Browse files
committed
Add examples
1 parent 3b9b0ec commit d513d45

File tree

6 files changed

+99
-15
lines changed

6 files changed

+99
-15
lines changed

.gitignore

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
*
22
!.gitignore
3-
3+
!*.nim
4+
/*.nim
45
!*.nimble
56
!*.docopt
67
!*.md
78
!nim.cfg
89
!README*
910
!LICENSE
1011
!/src
11-
!/src/*.nim
1212
!/src/private
13-
!/src/private/*.nim
1413
!/test
15-
!/test/*.nim
14+
!/examples
1615
!/circle.yml

README.md

+13-11
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ Options:
2525
--drifting Drifting mine.
2626
"""
2727
28-
import tables, strutils
28+
import strutils
2929
import docopt
3030
3131
let args = docopt(doc, version = "Naval Fate 2.0")
3232
3333
if args["move"]:
34-
echo "Move ship $# to ($#, $#) at $# kn".format(
34+
echo "Moving ship $# to ($#, $#) at $# kn".format(
3535
args["<name>"], args["<x>"], args["<y>"], args["--speed"])
36+
ships[$args["<name>"]].move(
37+
parseFloat($args["<x>"]), parseFloat($args["<y>"]),
38+
speed = parseFloat($args["--speed"]))
3639
```
3740

3841
The option parser is generated based on the docstring above that is passed to `docopt` function. `docopt` parses the usage pattern (`"Usage: ..."`) and option descriptions (lines starting with dash "`-`") and ensures that the program invocation matches the usage pattern; it parses options, arguments and commands based on that. The basic idea is that *a good help message has all necessary information in it to make a parser*.
@@ -116,6 +119,14 @@ Note that you can use any kind of value in a boolean context and convert any val
116119
Look [in the source code](src/private/value.nim) to find out more about these conversions.
117120

118121

122+
Examples
123+
--------
124+
125+
See [examples](examples) folder.
126+
127+
For more examples of docopt language see [docopt.py examples][].
128+
129+
119130
Installation
120131
------------
121132

@@ -124,16 +135,7 @@ Installation
124135
This library has no dependencies outside the standard library. An impure [`re`][re] library is used.
125136

126137

127-
Testing
128-
-------
129-
130-
See [test](test) folder.
131-
132-
133-
Examples
134-
--------
135138

136-
See [docopt.py examples][]
137139

138140

139141
[docopt.org]: http://docopt.org/

examples/counted.nim

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
let doc = """
2+
Usage: counted --help
3+
counted -v...
4+
counted go [go]
5+
counted (--path=<path>)...
6+
counted <file> <file>
7+
8+
Try: counted -vvvvvvvvvv
9+
counted go go
10+
counted --path ./here --path ./there
11+
counted this.txt that.txt
12+
"""
13+
14+
import strutils
15+
import docopt
16+
17+
18+
let args = docopt(doc)
19+
echo args
20+
21+
if args["-v"]:
22+
echo capitalize(repeat("very ", args["-v"].len - 1) & "verbose")
23+
24+
for path in @(args["--path"]):
25+
echo read_file(path)

examples/cycle.nim

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
let doc = """
2+
Repeatedly output the arguments.
3+
4+
Usage:
5+
cycle [options] <what>...
6+
7+
Options:
8+
-h --help show this help message and exit
9+
-n <times> output all the arguments N times [default: infinite]
10+
--interval <seconds> wait after every line of output
11+
"""
12+
13+
import strutils, os, math
14+
import docopt
15+
16+
17+
let args = docopt(doc)
18+
echo args
19+
20+
let infinite = ($args["-n"] == "infinite")
21+
var n: int
22+
if not infinite:
23+
n = parse_int($args["-n"])
24+
25+
var interval = 0
26+
if args["--interval"]:
27+
interval = round(parse_float($args["--interval"])*1000)
28+
29+
while true:
30+
for s in @(args["<what>"]):
31+
echo s
32+
33+
if interval > 0:
34+
sleep interval
35+
36+
if not infinite:
37+
dec n
38+
if n <= 0:
39+
break

examples/nim.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
path: "$project_path/../src"

examples/odd_even.nim

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let doc = """
2+
Usage: odd_even [-h | --help] (ODD EVEN)...
3+
4+
Example, try:
5+
odd_even 1 2 3 4
6+
7+
Options:
8+
-h, --help
9+
"""
10+
11+
import docopt
12+
13+
14+
let args = docopt(doc)
15+
echo args
16+
17+
for i in 0 .. <args["ODD"].len:
18+
echo args["ODD"][i] & " " & args["EVEN"][i]

0 commit comments

Comments
 (0)