Skip to content

Commit b500b12

Browse files
committed
Make $ valid for all values again
1 parent bd052ec commit b500b12

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/docopt.nim

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,18 @@ proc len*(v: Value): int =
4949
if v.kind == vkInt: v.int_v
5050
else: v.list_v.len
5151

52+
proc str(v: Value): string
53+
5254
proc `$`*(v: Value): string =
53-
## Return the string of a vkStr Value
54-
## or the only item of a vkList value.
55-
## It is an error to use it on other kinds of Values.
56-
case v.kind
57-
of vkList:
58-
if v.list_v.is_nil or v.list_v.len != 1:
59-
raise new_exception(ValueError,
60-
"Must have exactly 1 item to convert to string")
55+
## Return the string of a vkStr Value,
56+
## or the item of a vkList Value, if there is exactly one,
57+
## or a string representation of any other kind of Value.
58+
if v.kind == vkStr:
59+
v.str_v
60+
elif v.kind == vkList and
61+
not v.list_v.is_nil and v.list_v.len == 1:
6162
v.list_v[0]
62-
else: v.str_v
63+
else: v.str
6364

6465
proc `@`*(v: Value): seq[string] =
6566
## Return the seq of a vkList Value.

0 commit comments

Comments
 (0)