commandlineparser / commandlineparser/commandline

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

未关闭
#340 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
C#
星标
4.8k
派生
478
PR 合并指标
30 天内没有已合并 PR

描述

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 `"`.

贡献指南

这个仓库没有索引到贡献指南

调研方向

使用链接的 Complete TestCase 重现该故障,尤其是 Default = false 且未提供任何值的选项。跟踪默认值如何分配给选项属性;完成的实现应包括数据类型验证,以及一个能够标识受影响选项的异常,而不是无法追踪的转换错误。

由索引模型根据 Issue 内容生成。

评估

技术栈
csharp
领域
cli
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。