commandlineparser / commandlineparser/commandline
Load options dynamically by System.Type only working for verbs.
- 主要言語
- 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