Skip to content

Commit

Permalink
Implement conversions to enum options
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp committed Aug 21, 2023
1 parent 3d306d5 commit 4a23922
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Src/IronPython/Runtime/PythonOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public PythonOptions(IDictionary<string, object> options)
Arguments = GetStringCollectionOption(options, "Arguments") ?? EmptyStringCollection;
WarningFilters = GetStringCollectionOption(options, "WarningFilters", ';', ',') ?? EmptyStringCollection;

BytesWarning = GetOption(options, "BytesWarning", Severity.Ignore);
BytesWarning = GetEnumOption(options, "BytesWarning", Severity.Ignore);
Debug = GetOption(options, "Debug", false);
Inspect = GetOption(options, "Inspect", false);
NoUserSite = GetOption(options, "NoUserSite", false);
Expand All @@ -165,7 +165,7 @@ public PythonOptions(IDictionary<string, object> options)
NoImportLib = GetOption(options, "NoImportLib", false);
Isolated = GetOption(options, "Isolated", false);
Utf8Mode = GetOption(options, "Utf8Mode", false);
ConsoleSupportLevel = GetOption(options, "ConsoleSupportLevel", SharedIO.SupportLevel.Full);
ConsoleSupportLevel = GetEnumOption(options, "ConsoleSupportLevel", SharedIO.SupportLevel.Full);
}

private static IDictionary<string, object> EnsureSearchPaths(IDictionary<string, object> options) {
Expand All @@ -176,5 +176,19 @@ private static IDictionary<string, object> EnsureSearchPaths(IDictionary<string,
}
return options;
}

private static T GetEnumOption<T>(IDictionary<string, object> options, string name, T defaultValue) where T : struct, Enum {
if (options != null && options.TryGetValue(name, out object value)) {
if (value is T variable) {
return variable;
}
Type rettype = typeof(T);
if (value is string strval) {
return (T)Enum.Parse(rettype, strval, ignoreCase: false);
}
return (T)Convert.ChangeType(value, Enum.GetUnderlyingType(rettype), CultureInfo.CurrentCulture);
}
return defaultValue;
}
}
}

0 comments on commit 4a23922

Please sign in to comment.