commandlineparser / commandlineparser/commandline

How can I assign TypeConverter to Enum in `Options`?

Open
#944 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.8k
Forks
478
PR merge metrics
No merged PRs in 30d

Description

I've declared `Options` which will be used for parsing command line.
```csharp
public class Options {
// ...
[Option('o', "operation", HelpText = "")]
[TypeConverter(typeof(OperationConverter))]
public Operation Operation { get; set; }
}
```
And the `Operation` looks like this:
```csharp
[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`
```csharp
public class OperationConverter : EnumConverter {
// ... candidates for other enum values
private static readonly List 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](https://github.com/commandlineparser/commandline/issues/83#issuecomment-671916065) 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`](https://github.com/commandlineparser/commandline/wiki/T_CommandLine_Core_TypeDescriptor). [this](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.typedescriptor?view=netframework-4.8.1#methods) has `.AddAttributes()`, but this doesn't seem to work.
```csharp
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?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the Options.Operation property and the TypeDescriptor.AddAttributes call, then trace the command-line parser's conversion path to determine whether property-level or enum-level converters are used. Done means documenting or correcting the supported registration path so the custom converter maps cd to Operation.CreateDirectory.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.