commandlineparser / commandlineparser/commandline

Consider custom parsers/converters/handler for types?

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

描述

I would love to be able to do:

```c#
public class Options
{
[Option('o', "out", Required = true, HelpText = "Output file")]
public string OutFile { get; set; }
[Option('d', "duration", Required = false, Default = "1:00:00", HelpText = "Duration")]
public TimeSpan Duration { get; set; }
[Option('e', "endpoint", Required = true, HelpText = "IP Endpoint (:)")]
public IPEndPoint Endpoint { get; set; }
}
```

Where I can use types like `TimeSpan` and `IPEndPoint` or any other type. Ofcourse, we would need to be able to specify a parser/converter/handler/whatever that takes the value and converts it to the desired type. I imagine it be like:

```c#
public class Options
{
[Option('o', "out", Required = true, HelpText = "Output file")]
public string OutFile { get; set; }
[Option('d', "duration", Required = false, Default = "1:00:00", HelpText = "Duration", Converter = typeof(MyTimeSpanConverter))]
public TimeSpan Duration { get; set; }
[Option('e', "endpoint", Required = true, HelpText = "IP Endpoint (:)", Converter = typeof(MyIPEndpointConverter))]
public IPEndPoint Endpoint { get; set; }
}
```

Next these 'converters' should implement something like an IConvert that defines a method `T Convert(string value)` where they take a string (the commandline argument) and convert to the desired type. Another option would be to not use the attributes but be able to specify handlers like:

```c#
Parser.Default
.Register()
.Register()
.ParseArguments(args)
....
```

Would that be something that you would consider? (Maybe not exactly following the idea given above, but the broader idea of being able to handle custom types).

贡献指南

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

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

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