commandlineparser / commandlineparser/commandline

Invalid DataTypes for Default values may cause fatal error that can't be discovered or traced.

Đang mở
#340 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
C#
Star
4.8k
Fork
478
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

The following class have option with default value false :

public class Options
{
[Option('r', "read", MetaValue = "FILE", Required = false, HelpText = "Input file with data to process.")]
public string InputFile { get; set; }

[Option('w', "write", MetaValue = "FILE", HelpText = "Output FILE with processed data (otherwise standard output).")]
public string OutputFile { get; set; }

[Option('c', "calculate", HelpText = "calculate")]
public bool Calculate { get; set; }


//@@@@@ error here, Default = false for IEnumerable
//sure Default is wrong in that place, but discovered with code review after hours :)
[Option('g', "generators", Default = false, HelpText = "generator")]
public IEnumerable Generators { get; set; }



[Option('n', MetaValue = "INT", HelpText = "navigate.")]
public bool navigation { get; set; }
}

- No compilation error
- No error if commandline contain values for the option g like: `-r xyz.txt -c -g gen1`
- Exception for this commandline (the option g has no values) : `-r xyz.txt -c `

>Exception: Object of type 'System.Boolean' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[System.String]'

The strange is that the exception can't help to determine where the source of error
It's discovered by accident when i don't pass values for the option g

[Complete TestCase](https://dotnetfiddle.net/h1e5S6)
Sure, i removed the Default from the option and the exception disappeared.

**Suggested Solution**

- Validate the match of the Datatype of the default values before setting it.
- Raise an exception ,e.g, "Parser Error: Invalid Default values for the option `"`.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.