Skip to content

Commit 80f78e3

Browse files
committed
Merge pull request #28 from mrkline/string-value-to-long
value::asLong will convert from string to long
2 parents 4e6eae5 + 3abe10a commit 80f78e3

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

docopt_value.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ namespace docopt {
257257
inline
258258
long value::asLong() const
259259
{
260+
// Attempt to convert a string to a long
261+
if (kind == Kind::String) {
262+
const std::string& str = variant.strValue;
263+
std::size_t pos;
264+
const long ret = stol(str, &pos); // Throws if it can't convert
265+
if (pos != str.length()) {
266+
// The string ended in non-digits.
267+
throw std::runtime_error( str + " contains non-numeric characters.");
268+
}
269+
return ret;
270+
}
260271
throwIfNotKind(Kind::Long);
261272
return variant.longValue;
262273
}

0 commit comments

Comments
 (0)