class Options
{
[Option('p', "port", Required = false, HelpText = "port number")]
public int Port { get;set; }
}
If i change the type of Port to ushort or short I get an error when starting the program:
"ERROR(S):
Error setting value to option 'p, port': Check if Option or Value attribute values are set
properly for the given type."
Expected behavior:
short and ushort are supported.
Can you provide test case to show this issue and what version you used.
Hm, okay. I'll try to reproduce and send you some code.
Ok, I found it:
class Options
{
[Option("port", Default = 5001)]
public ushort Port { get; set; }
}
The error seems to be that I set Default to a value not of type string or uint. An explicit cast (ushort)5001 is necessary. It would be helpful if the error message on the terminal would not say:
Error setting value to option 'port': Check if Option or Value attribute values are set properly for the given type.
but more something like:
...: Expected type 'ushort', DefaultValue type 'int'
Which by the way could easily be solved, but I understand that since even dotnet needs a cast, it is more a question of if it should be done like this or not.
Maybe the parser could have an option: strict. If that is set to false the int would be converted (use Convert class from dotnet) otherwise the error message should appear.
Right now, I just came across the same case. Would be nice if we could use e.g. ushort or decimal with a default value. @danielbisar: I would answer the question with "yes", due I expect that this just works, without a strict option. Would be interesting what the community thinks about this case...
Default is object and need to be casted.
Parser fail when trying to cast object value to short because Impossible cast from object to short
More cast details
class Options
{
[Option("port", Default = (ushort)5001)]
public ushort Port { get; set; }
[Option(Default=(short)100 )]
public short ShortVal { get; set; }
[Option( Default = (bool)true)]
public bool? Vsible { get; set; }
[Option( Default = (double)5001.4)]
public double DoubleValue { get; set; }
[Option( Default = (float)401.3)]
public float FloatValue { get; set; }
}
For decimal values, it can not be casted like
//this is not allowed
//Compiler Error: CS0182 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
[Option( Default = (decimal)401.3)]
public decimal DecimalValue { get; set; }
decimal type is not a valid Attribute Parameter (c# language constraint).
First time using this library and ran into this issue, not knowing it was specifically to do with ushorts, so it took a while to track this down.
Most helpful comment
Defaultis object and need to be casted.Parser fail when trying to cast object value to short because Impossible cast from object to short
More cast details
For decimal values, it can not be casted like
decimal type is not a valid Attribute Parameter (c# language constraint).