commandlineparser / commandlineparser/commandline
Load options dynamically by System.Type only working for verbs.
- 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ả
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; }
}
}
}
```
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
Hướng nghiên cứu
Bắt đầu từ Parser.ParseArguments(IEnumerable, params Type[]) và so sánh cách xử lý của nó với việc phân tích verb. Tái hiện mẫu bằng cách sử dụng OptionsBase và MyClassOptions, sau đó xác minh liệu các kiểu được cung cấp động có phân tích các tùy chọn kế thừa và tùy chọn cụ thể như dự kiến hay không.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- csharp
- Lĩnh vực
- cli
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 25/100