mirror of
https://github.com/dlang/phobos.git
synced 2025-04-27 21:51:40 +03:00
Name argument if data conversion fails getopt (#10593)
Fixes #9662 whitespace
This commit is contained in:
parent
b085fd70e0
commit
ffb70c5580
1 changed files with 102 additions and 9 deletions
111
std/getopt.d
111
std/getopt.d
|
@ -610,6 +610,23 @@ private template optionValidator(A...)
|
||||||
alias optionValidator = message;
|
alias optionValidator = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleConversion(R)(string option, string value, R* receiver,
|
||||||
|
size_t idx, string file = __FILE__, size_t line = __LINE__)
|
||||||
|
{
|
||||||
|
import std.conv : to, ConvException;
|
||||||
|
import std.format : format;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
*receiver = to!(typeof(*receiver))(value);
|
||||||
|
}
|
||||||
|
catch (ConvException e)
|
||||||
|
{
|
||||||
|
throw new ConvException(format("Argument '%s' at position '%u' could "
|
||||||
|
~ "not be converted to type '%s' as required by option '%s'.",
|
||||||
|
value, idx, R.stringof, option), e, file, line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@safe pure unittest
|
@safe pure unittest
|
||||||
{
|
{
|
||||||
alias P = void*;
|
alias P = void*;
|
||||||
|
@ -864,7 +881,7 @@ private bool handleOption(R)(string option, R receiver, ref string[] args,
|
||||||
if (val.length)
|
if (val.length)
|
||||||
{
|
{
|
||||||
// parse '--b=true/false'
|
// parse '--b=true/false'
|
||||||
*receiver = to!(typeof(*receiver))(val);
|
handleConversion(option, val, receiver, i);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -888,15 +905,22 @@ private bool handleOption(R)(string option, R receiver, ref string[] args,
|
||||||
val = args[i];
|
val = args[i];
|
||||||
args = args[0 .. i] ~ args[i + 1 .. $];
|
args = args[0 .. i] ~ args[i + 1 .. $];
|
||||||
}
|
}
|
||||||
static if (is(typeof(*receiver) == enum))
|
static if (is(typeof(*receiver) == enum) ||
|
||||||
|
is(typeof(*receiver) == string))
|
||||||
{
|
{
|
||||||
*receiver = to!(typeof(*receiver))(val);
|
handleConversion(option, val, receiver, i);
|
||||||
}
|
}
|
||||||
else static if (is(typeof(*receiver) : real))
|
else static if (is(typeof(*receiver) : real))
|
||||||
{
|
{
|
||||||
// numeric receiver
|
// numeric receiver
|
||||||
if (incremental) ++*receiver;
|
if (incremental)
|
||||||
else *receiver = to!(typeof(*receiver))(val);
|
{
|
||||||
|
++*receiver;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
handleConversion(option, val, receiver, i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else static if (is(typeof(*receiver) == string))
|
else static if (is(typeof(*receiver) == string))
|
||||||
{
|
{
|
||||||
|
@ -936,12 +960,18 @@ private bool handleOption(R)(string option, R receiver, ref string[] args,
|
||||||
|
|
||||||
if (arraySep == "")
|
if (arraySep == "")
|
||||||
{
|
{
|
||||||
*receiver ~= to!E(val);
|
E tmp;
|
||||||
|
handleConversion(option, val, &tmp, i);
|
||||||
|
*receiver ~= tmp;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
foreach (elem; val.splitter(arraySep).map!(a => to!E(a))())
|
foreach (elem; val.splitter(arraySep))
|
||||||
*receiver ~= elem;
|
{
|
||||||
|
E tmp;
|
||||||
|
handleConversion(option, elem, &tmp, i);
|
||||||
|
*receiver ~= tmp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else static if (isAssociativeArray!(typeof(*receiver)))
|
else static if (isAssociativeArray!(typeof(*receiver)))
|
||||||
|
@ -961,7 +991,14 @@ private bool handleOption(R)(string option, R receiver, ref string[] args,
|
||||||
~ to!string(assignChar) ~ "' in argument '" ~ input ~ "'.");
|
~ to!string(assignChar) ~ "' in argument '" ~ input ~ "'.");
|
||||||
auto key = input[0 .. j];
|
auto key = input[0 .. j];
|
||||||
auto value = input[j + 1 .. $];
|
auto value = input[j + 1 .. $];
|
||||||
return tuple(to!K(key), to!V(value));
|
|
||||||
|
K k;
|
||||||
|
handleConversion("", key, &k, 0);
|
||||||
|
|
||||||
|
V v;
|
||||||
|
handleConversion("", value, &v, 0);
|
||||||
|
|
||||||
|
return tuple(k,v);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setHash(Range)(R receiver, Range range)
|
static void setHash(Range)(R receiver, Range range)
|
||||||
|
@ -1946,3 +1983,59 @@ void defaultGetoptFormatter(Output)(Output output, string text, Option[] opt, st
|
||||||
~ "information.\n";
|
~ "information.\n";
|
||||||
assert(wanted == helpMsg);
|
assert(wanted == helpMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@safe unittest
|
||||||
|
{
|
||||||
|
import std.conv : ConvException;
|
||||||
|
import std.string : indexOf;
|
||||||
|
|
||||||
|
enum UniqueIdentifer {
|
||||||
|
a,
|
||||||
|
b
|
||||||
|
}
|
||||||
|
|
||||||
|
UniqueIdentifer a;
|
||||||
|
|
||||||
|
auto args = ["prog", "--foo", "HELLO"];
|
||||||
|
try
|
||||||
|
{
|
||||||
|
auto t = getopt(args, "foo|f", &a);
|
||||||
|
assert(false, "Must not be reached, as \"HELLO\" cannot be converted"
|
||||||
|
~ " to enum A.");
|
||||||
|
}
|
||||||
|
catch (ConvException e)
|
||||||
|
{
|
||||||
|
string str = () @trusted { return e.toString(); }();
|
||||||
|
assert(str.indexOf("HELLO") != -1);
|
||||||
|
assert(str.indexOf("UniqueIdentifer") != -1);
|
||||||
|
assert(str.indexOf("foo") != -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@safe unittest
|
||||||
|
{
|
||||||
|
import std.conv : ConvException;
|
||||||
|
import std.string : indexOf;
|
||||||
|
|
||||||
|
int a;
|
||||||
|
|
||||||
|
auto args = ["prog", "--foo", "HELLO"];
|
||||||
|
try
|
||||||
|
{
|
||||||
|
auto t = getopt(args, "foo|f", &a);
|
||||||
|
assert(false, "Must not be reached, as \"HELLO\" cannot be converted"
|
||||||
|
~ " to an int");
|
||||||
|
}
|
||||||
|
catch (ConvException e)
|
||||||
|
{
|
||||||
|
string str = () @trusted { return e.toString(); }();
|
||||||
|
assert(str.indexOf("HELLO") != -1);
|
||||||
|
assert(str.indexOf("int") != -1);
|
||||||
|
assert(str.indexOf("foo") != -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
args = ["prog", "--foo", "1337"];
|
||||||
|
getopt(args, "foo|f", &a);
|
||||||
|
assert(a == 1337);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue