1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-24 19:29:36 +01:00

First hit at providing support for floats in the language.

This commit is contained in:
Christian Theune 2016-01-05 00:40:40 +01:00
parent b8258a4475
commit 14ebde5289
16 changed files with 207 additions and 23 deletions

View file

@ -66,6 +66,30 @@ template<class N> N getIntArg(const string & opt,
return n * multiplier;
}
template<class N> N getFloatArg(const string & opt,
Strings::iterator & i, const Strings::iterator & end, bool allowUnit)
{
++i;
if (i == end) throw UsageError(format("%1% requires an argument") % opt);
string s = *i;
N multiplier = 1;
if (allowUnit && !s.empty()) {
char u = std::toupper(*s.rbegin());
if (std::isalpha(u)) {
if (u == 'K') multiplier = 1ULL << 10;
else if (u == 'M') multiplier = 1ULL << 20;
else if (u == 'G') multiplier = 1ULL << 30;
else if (u == 'T') multiplier = 1ULL << 40;
else throw UsageError(format("invalid unit specifier %1%") % u);
s.resize(s.size() - 1);
}
}
N n;
if (!string2Float(s, n))
throw UsageError(format("%1% requires a float argument") % opt);
return n * multiplier;
}
/* Show the manual page for the specified program. */
void showManPage(const string & name);