How can I assign TypeConverter to Enum in `Options`?
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 35/100
Rechercherichtung
Beginne bei der Eigenschaft Options.Operation und dem Aufruf TypeDescriptor.AddAttributes und verfolge dann den Konvertierungspfad des Kommandozeilen-Parsers, um festzustellen, ob Konverter auf Eigenschafts- oder Enum-Ebene verwendet werden. Als erledigt gilt die Dokumentation oder Korrektur des unterstützten Registrierungspfads, sodass der benutzerdefinierte Konverter cd auf Operation.CreateDirectory abbildet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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?
- Vorherrschende Sprache
- C#
- Sterne
- 4.8k
- Forks
- 478
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus commandlineparser/commandline
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
commandlineparser/commandline#951 · 1 Kommentar ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 70/100
commandlineparser/commandline#880 · 2 Reaktionen ·
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 45/100
commandlineparser/commandline#952 ·
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 35/100
commandlineparser/commandline#948 ·
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 35/100
commandlineparser/commandline#947 ·
Alle Issues in commandlineparser/commandline
Ähnliche Issues
-
bug
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
-
Type: enhancement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
apache/arrow-adbc#4809 ·
-
type/automation type/tech-debt
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
microsoft/vscode-azurefunctions#5197 · 1 Kommentar ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 85/100
microsoft/microsoft-ui-reactor#1274 ·