Recovering options to getopt after a GetOptException is raised

2015-10-07 Thread Charles McAnany via Digitalmars-d

Friends,

I'm a bit puzzled by the behavior of this code:

import std.getopt;
import std.stdio;

void main(string[] args){
string val;
GetoptResult opts;
try{
opts = getopt(args, std.getopt.config.required, "val", 
"value you need to specify", &val);

}catch(GetOptException e){
writeln("You idiot. You had to give these options:");
writeln(opts.options);
}
}

Expected result:
:) programName --without --correct --argument
   You idiot. You had to give these options:
   --val value you need to specify

Actual result:
:) programName --without --correct --argument
   You idiot. You had to give these options:
   []

It seems that the exception is thrown as soon as a missing option 
is encountered. It would be nice if getopt would populate 
GetoptResult.options before it threw the exception, because it 
would make the above code work smoothly. Alternatively, 
GetOptException could get a field containing the documentation 
that was provided in the call to getopt:


catch(GetOptException e){
writefln("You idiot. You didn't provide the required %s 
argument (%s)", e.optionName, e.optionDocumentation);

}

If there is a way to handle this cleanly, I'd appreciate it. As 
it is, std.getopt.config.required seems like it's not very useful 
because it emits a rather unhelpful error message (containing 
only the name of the missing option) and I can't see a way to 
print a more useful message without checking all the options in 
my own code.


Cheers,
Charles.


Overloading opEquals(T)(T y)

2011-02-07 Thread Charles McAnany
Hi, all. So I'm trying to make a BigRational struct, to get more comfortable
with D. I'm working on the opEquals part now, and I'm having some difficulty.
I'd like to write an equality checker for two BigRationals, and have all other
comparisons make a BigRational of the rhs and then forward that to the first
equality checker.

bool opEquals(Tdummy = void)(BigRational y){
auto temp = this-y;
if (temp.numerator == 0)
return true;
return false;
}

bool opEquals(T)(T y){
return this == BigRational(y);
}

But this is an ambiguity error. Certainly, I could write opEquals(T: int)(T
y), opEquals(T:long)(T y), and so on and so on, but I feel there must be a
more elegant solution. Any ideas?
Thanks,
Charles