commandlineparser / commandlineparser/commandline

Load options dynamically by System.Type only working for verbs.

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

描述

I have an architecture where first, an Options base class is parsed and based on one parameter, I need to parse specific Options. These Options are loaded dynamically and since they are loaded dynamically, I can't use the generic method overloads of ParseArguments and the overload with params System.Type[] types, works only for verbs?. This once simple and goal oriented Library, unfortunately became unnecessary complex.

In Version 1.x I could pass the specific instance e.g. MyClassOptions and it would parse just fine.

```cs
using System;
using System.Collections.Generic;
using System.Linq;
using CommandLine;

namespace CmdTest
{
class Program
{
static void Main(string[] args)
{
// args = -c MyClassOptions -s mySpecificOption

//....

// First Parse base options, load specific options depending on parameter of this instance.
// Parse specific options while using type from reflection.

//....

Parser parser = new Parser(with => with.IgnoreUnknownArguments = true);
var result = parser.ParseArguments(args, typeof(MyClassOptions)).
MapResult(x => x, NotParsedFunc);

MyClassOptions options = (MyClassOptions) result;

// Not working, because the public ParserResult ParseArguments(IEnumerable args, params Type[] types);
// overload only parses verbs now...

Console.WriteLine(options.SpecificOption);
}

private static object NotParsedFunc(IEnumerable arg)
{
throw new Exception(arg.First().Tag.ToString()); // Exception is thrown
}

public class OptionsBase
{
[Option('c', "class", Required = true, HelpText = "Class that should be loaded dynamically.")]
public string Classname { get; set; }
}

public class MyClassOptions : OptionsBase
{
[Option('s', "specificoption", Required = true, HelpText = "Specific Option for my class to load.")]
public string SpecificOption { get; set; }
}
}
}
```

贡献指南

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

调研方向

从 Parser.ParseArguments(IEnumerable, params Type[]) 开始,并将其处理方式与 verb parsing 进行比较。使用 OptionsBase 和 MyClassOptions 重现示例,然后验证动态提供的类型是否按预期解析继承的选项和特定选项。

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

评估

技术栈
csharp
领域
cli
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

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