How can I assign TypeConverter to Enum in `Options`?
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Bắt đầu từ thuộc tính Options.Operation và lệnh gọi TypeDescriptor.AddAttributes, sau đó lần theo đường dẫn chuyển đổi của trình phân tích cú pháp dòng lệnh để xác định liệu các bộ chuyển đổi ở cấp thuộc tính hay cấp enum có được sử dụng hay không. Hoàn tất nghĩa là ghi lại tài liệu hoặc sửa đường dẫn đăng ký được hỗ trợ để bộ chuyển đổi tùy chỉnh ánh xạ cd thành Operation.CreateDirectory.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
I've declared Options which will be used for parsing command line.
public class Options {
// ...
[Option('o', "operation", HelpText = "")]
[TypeConverter(typeof(OperationConverter))]
public Operation Operation { get; set; }
}
And the Operation looks like this:
[TypeConverter(typeof(OperationConverter))]
public enum Operation {
// ...
CreateDirectory,
// ...
}
Since CreateDirectory is quite long to type in command line, I want to allow cd as valid Operation.CreateDirectory
public class OperationConverter : EnumConverter {
// ... candidates for other enum values
private static readonly List<string> OperationCandidate = ["createdirectory", "cd"];
// ...
public override object ConvertFrom(ITypeDescriptorContext? ctx, CultureInfo? ci, object value) {
if (value is string s) {
string l = s.Trim().ToLowerInvariant();
// ...
if (OperationCandidate.Contains(l)) return Operation.CreateDirectory;
// ...
}
}
}
But I can't assign this OperationConverter to Options.Operation. I don't see anything that might be related to this.
#83 says it is resolved, but at the bottom of that issue, someone said:
I dont think this works in the latest version. I tried annotating my option's property, but the converter is not invoked. Instead I had to register it using
TypeDescriptor.AddAttributes().
But I don't see any methods named .AddAttributes() in TypeDescriptor. this has .AddAttributes(), but this doesn't seem to work.
private static void Main(string[] args) {
// ...
TypeDescriptor.AddAttributes(typeof(Operation), new TypeConverterAttribute(typeof(OperationConverter)));
// other logic + command line parsing
}
Now I'm completely lost. Wiki doesn't provide any useful information that I can easily access.
How can I assign TypeConverter to Enum in Options?
- 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
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
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của commandlineparser/commandline
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
commandlineparser/commandline#951 · 1 bình luận ·
-
Code example misses iEnumerable Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 70/100
commandlineparser/commandline#880 · 2 reaction ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 45/100
commandlineparser/commandline#952 ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
commandlineparser/commandline#948 ·
-
How to add examples for verbs? Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
commandlineparser/commandline#947 ·
Tất cả issue của commandlineparser/commandline
Issue tương tự
-
CS0162 "Unreachable code detected" warning from a MSBuildTemp .tmp file in every game project Đang mởbug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
Type: enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
apache/arrow-adbc#4809 ·
-
type/automation type/tech-debt
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
microsoft/vscode-azurefunctions#5197 · 1 bình luận ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
microsoft/microsoft-ui-reactor#1274 ·