www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getopt default int init and zero

reply Suliman <evermind live.ru> writes:
I would like to check if user specified `0` as getopt parameter. 
But the problem that `int`'s are default in `0`. So if user did 
not specified nothing `int x` will be zero, and all other code 
will work as if it's zero.

In std.typecons I found Nullable that allow init int to zero. I 
tried to do:

Nullable!int dateInterval;

try
{
	auto helpInformation = getopt(args,
	"interval|i",  "Interval of selection in dayes", &dateInterval
	);
}

But I am getting error:

conv.d(194,24): Error: template std.conv.toImpl cannot deduce 
function from argument types !(Nullable!int)(string), candidates 
are:
conv.d(435,11):        std.conv.toImpl(T, S)(S value) if 
(isImplicitlyConvertible!(S, T) && !isEnumStrToStr!(S, T) && 
!isNullToStr!(S, T))
conv.d(549,11):        std.conv.toImpl(T, S)(ref S s) if 
(isStaticArray!S)
conv.d(565,11):        std.conv.toImpl(T, S)(S value) if 
(!isImplicitlyConvertible!(S, T) && is(typeof(S.init.opCast!T()) 
: T) && !isExactSomeString!T && !is(typeof(T(value))))
conv.d(616,11):        std.conv.toImpl(T, S)(S value) if 
(!isImplicitlyConvertible!(S, T) && is(T == struct) && 
is(typeof(T(value))))
conv.d(665,11):        std.conv.toImpl(T, S)(S value) if 
(!isImplicitlyConvertible!(S, T) && is(T == class) && 
is(typeof(new T(value))))
conv.d(194,24):        ... (9 more, -v to show) ...
getopt.d(894,56): Error: template instance 
std.conv.to!(Nullable!int).to!string error instantiating
getopt.d(749,46):        instantiated from here: 
handleOption!(Nullable!int*)
getopt.d(758,23):        instantiated from here: 
getoptImpl!(string, string, Nullable!int*, string, string, bool*)
getopt.d(436,15):        instantiated from here: 
getoptImpl!(string, string, int[]*, string, string, 
Nullable!int*, string, string, bool*)
source\app.d(41,32):        instantiated from here: 
getopt!(string, string, int[]*, string, string, Nullable!int*, 
string, string, bool*)
May 19
next sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Friday, May 19, 2017 12:09:38 PM PDT Suliman via Digitalmars-d-learn 
wrote:
 I would like to check if user specified `0` as getopt parameter.
 But the problem that `int`'s are default in `0`. So if user did
 not specified nothing `int x` will be zero, and all other code
 will work as if it's zero.

 In std.typecons I found Nullable that allow init int to zero. I
 tried to do:

 Nullable!int dateInterval;

 try
 {
   auto helpInformation = getopt(args,
   "interval|i",  "Interval of selection in dayes", &dateInterval
   );
 }

 But I am getting error:
getopt really only supports built-in types. I'd suggest that you try setting the int to a value other than zero that you don't expect the user to enter (e.g. -1 or int.max) and check for that. Alternatively, you could use string, and then convert it using std.conv.to, which certainly isn't as nice, because getopt isn't doing the conversion for you, but whereas you can't check for an empty int, you _can_ check for an empty string. Adding support for Nullable to getopt sounds like a good enhancement request though. This is a very valid use case that is not well-served at the moment, and supporting Nullable doesn't require figuring out how to support aribitrary user-defined types, which is a whole other can of worms. https://issues.dlang.org And of course, if you're brave enough to figure enough out about getopt's internal's to implement it yourself and create a pull request, that would be quite welcome. :) - Jonathan M Davis
May 19
prev sibling parent Jon Degenhardt <jond noreply.com> writes:
On Friday, 19 May 2017 at 12:09:38 UTC, Suliman wrote:
 I would like to check if user specified `0` as getopt 
 parameter. But the problem that `int`'s are default in `0`. So 
 if user did not specified nothing `int x` will be zero, and all 
 other code will work as if it's zero.
One way to do this is the use a callback function or delegate. Have the callback set both the main variable and a boolean tracking whether the option was entered. --Jon
May 20