Enable nullability everywhere

This commit is contained in:
Matt Nadareski
2024-02-28 19:19:50 -05:00
parent 11d024bd16
commit 823a9ca7b7
145 changed files with 1545 additions and 1260 deletions

View File

@@ -10,17 +10,17 @@ namespace SabreTools.Help
protected ParameterType _featureType;
protected bool _foundOnce = false;
protected object _value = null;
protected object? _value = null;
#endregion
#region Publicly facing variables
public string Name { get; protected set; }
public string? Name { get; protected set; }
public List<string> Flags { get; protected set; }
public string Description { get; protected set; }
public string LongDescription { get; protected set; }
public Dictionary<string, Feature> Features { get; protected set; }
public string? Description { get; protected set; }
public string? LongDescription { get; protected set; }
public Dictionary<string, Feature?> Features { get; protected set; }
#endregion
@@ -29,34 +29,34 @@ namespace SabreTools.Help
public Feature()
{
this.Name = null;
this.Flags = new List<string>();
this.Flags = [];
this.Description = null;
this.LongDescription = null;
this._featureType = ParameterType.Flag;
this.Features = new Dictionary<string, Feature>();
this.Features = [];
}
public Feature(string name, string flag, string description, ParameterType featureType, string longDescription = null)
public Feature(string name, string flag, string description, ParameterType featureType, string? longDescription = null)
{
this.Name = name;
this.Flags = new List<string>
{
this.Flags =
[
flag
};
];
this.Description = description;
this.LongDescription = longDescription;
this._featureType = featureType;
this.Features = new Dictionary<string, Feature>();
this.Features = [];
}
public Feature(string name, List<string> flags, string description, ParameterType featureType, string longDescription = null)
public Feature(string name, List<string> flags, string description, ParameterType featureType, string? longDescription = null)
{
this.Name = name;
this.Flags = flags;
this.Description = description;
this.LongDescription = longDescription;
this._featureType = featureType;
this.Features = new Dictionary<string, Feature>();
this.Features = [];
}
#endregion
@@ -66,7 +66,7 @@ namespace SabreTools.Help
/// <summary>
/// Directly address a given subfeature
/// </summary>
public Feature this[string name]
public Feature? this[string name]
{
get { return this.Features.ContainsKey(name) ? this.Features[name] : null; }
set { this.Features[name] = value; }
@@ -75,10 +75,10 @@ namespace SabreTools.Help
/// <summary>
/// Directly address a given subfeature
/// </summary>
public Feature this[Feature subfeature]
public Feature? this[Feature? subfeature]
{
get { return this.Features.ContainsKey(subfeature.Name) ? this.Features[subfeature.Name] : null; }
set { this.Features[subfeature.Name] = value; }
get { return this.Features.ContainsKey(subfeature?.Name ?? string.Empty) ? this.Features[subfeature?.Name ?? string.Empty] : null; }
set { this.Features[subfeature?.Name ?? string.Empty] = value; }
}
/// <summary>
@@ -87,10 +87,10 @@ namespace SabreTools.Help
/// <param name="feature"></param>
public void AddFeature(Feature feature)
{
this.Features ??= new Dictionary<string, Feature>();
this.Features ??= [];
lock (this.Features)
{
this.Features[feature.Name] = feature;
this.Features[feature.Name ?? string.Empty] = feature;
}
}
@@ -100,7 +100,7 @@ namespace SabreTools.Help
/// <param name="flag">Flag to add for this feature</param>
public void AddFlag(string flag)
{
this.Flags ??= new List<string>();
this.Flags ??= [];
lock (this.Flags)
{
this.Flags.Add(flag);
@@ -113,7 +113,7 @@ namespace SabreTools.Help
/// <param name="flags">List of flags to add to this feature</param>
public void AddFlags(List<string> flags)
{
this.Flags ??= new List<string>();
this.Flags ??= [];
lock (this.Flags)
{
this.Flags.AddRange(flags);
@@ -153,7 +153,7 @@ namespace SabreTools.Help
public List<string> Output(int pre = 0, int midpoint = 0, bool includeLongDescription = false)
{
// Create the output list
List<string> outputList = new();
List<string> outputList = [];
// Build the output string first
string output = string.Empty;
@@ -206,7 +206,10 @@ namespace SabreTools.Help
output = CreatePadding(pre + 4);
// Now split the input description and start processing
string[] split = this.LongDescription.Split(' ');
string[]? split = this.LongDescription?.Split(' ');
if (split == null)
return outputList;
for (int i = 0; i < split.Length; i++)
{
// If we have a newline character, reset the line and continue
@@ -340,7 +343,10 @@ namespace SabreTools.Help
output = CreatePadding(preAdjusted + 4);
// Now split the input description and start processing
string[] split = this.LongDescription.Split(' ');
string[]? split = this.LongDescription?.Split(' ');
if (split == null)
return outputList;
for (int i = 0; i < split.Length; i++)
{
// If we have a newline character, reset the line and continue
@@ -392,7 +398,7 @@ namespace SabreTools.Help
// Now let's append all subfeatures
foreach (string feature in this.Features.Keys)
{
outputList.AddRange(this.Features[feature].OutputRecursive(tabLevel + 1, pre, midpoint, includeLongDescription));
outputList.AddRange(this.Features[feature]!.OutputRecursive(tabLevel + 1, pre, midpoint, includeLongDescription));
}
return outputList;
@@ -472,7 +478,7 @@ namespace SabreTools.Help
if (valid)
{
_value ??= new List<string>();
(_value as List<string>).Add(input.Split('=')[1]);
(_value as List<string>)?.Add(input.Split('=')[1]);
}
break;
@@ -495,7 +501,7 @@ namespace SabreTools.Help
// If we haven't found a valid flag and we're not looking for just this feature, check to see if any of the subfeatures are valid
if (!valid && !exact)
valid = this.Features.Keys.Any(k => this.Features[k].ValidateInput(input));
valid = this.Features.Keys.Any(k => this.Features[k]!.ValidateInput(input));
return valid;
}
@@ -514,12 +520,12 @@ namespace SabreTools.Help
/// <summary>
/// Get the string value associated with this feature
/// </summary>
public string GetStringValue()
public string? GetStringValue()
{
if (_featureType != ParameterType.String)
throw new ArgumentException("Feature is not a string");
return (_value as string);
return _value as string;
}
/// <summary>
@@ -552,7 +558,7 @@ namespace SabreTools.Help
if (_featureType != ParameterType.List)
throw new ArgumentException("Feature is not a list");
return (_value as List<string>) ?? new List<string>();
return (_value as List<string>) ?? [];
}
/// <summary>
@@ -565,8 +571,8 @@ namespace SabreTools.Help
{
ParameterType.Flag => (_value as bool?) == true,
ParameterType.String => (_value as string) != null,
ParameterType.Int32 => (_value as int?).HasValue && (_value as int?).Value != int.MinValue,
ParameterType.Int64 => (_value as long?).HasValue && (_value as long?).Value != long.MinValue,
ParameterType.Int32 => (_value as int?).HasValue && (_value as int?)!.Value != int.MinValue,
ParameterType.Int64 => (_value as long?).HasValue && (_value as long?)!.Value != long.MinValue,
ParameterType.List => (_value as List<string>) != null,
_ => false,
};

View File

@@ -9,7 +9,7 @@ namespace SabreTools.Help
#region Private variables
private readonly List<string> _header;
private Dictionary<string, Feature> _features;
private Dictionary<string, Feature?> _features;
private const string _barrier = "-----------------------------------------";
#endregion
@@ -18,25 +18,25 @@ namespace SabreTools.Help
public FeatureSet()
{
_header = new List<string>();
_features = new Dictionary<string, Feature>();
_header = [];
_features = [];
}
public FeatureSet(List<string> header)
{
_header = header;
_features = new Dictionary<string, Feature>();
_features = [];
}
#endregion
#region Accessors
public Feature this[string name]
public Feature? this[string name]
{
get
{
_features ??= new Dictionary<string, Feature>();
_features ??= [];
if (!_features.ContainsKey(name))
return null;
@@ -44,16 +44,19 @@ namespace SabreTools.Help
}
set
{
_features ??= new Dictionary<string, Feature>();
_features ??= [];
_features[name] = value;
}
}
public Feature this[Feature subfeature]
public Feature? this[Feature subfeature]
{
get
{
_features ??= new Dictionary<string, Feature>();
if (subfeature.Name == null)
return null;
_features ??= [];
if (!_features.ContainsKey(subfeature.Name))
return null;
@@ -61,8 +64,9 @@ namespace SabreTools.Help
}
set
{
_features ??= new Dictionary<string, Feature>();
_features[subfeature.Name] = value;
_features ??= [];
if (subfeature.Name != null)
_features[subfeature.Name] = value;
}
}
@@ -72,7 +76,10 @@ namespace SabreTools.Help
/// <param name="feature">Feature object to map to</param>
public void Add(Feature feature)
{
_features ??= new Dictionary<string, Feature>();
if (feature.Name == null)
return;
_features ??= [];
lock (_features)
{
_features.Add(feature.Name, feature);
@@ -89,7 +96,7 @@ namespace SabreTools.Help
/// <returns>Feature name</returns>
public string GetFeatureName(string name)
{
return _features.Keys.FirstOrDefault(f => _features[f].ValidateInput(name, exact: true, ignore: true)) ?? string.Empty;
return _features.Keys.FirstOrDefault(f => _features[f]?.ValidateInput(name, exact: true, ignore: true) ?? false) ?? string.Empty;
}
/// <summary>
@@ -98,7 +105,7 @@ namespace SabreTools.Help
public void OutputGenericHelp()
{
// Start building the output list
List<string> output = new();
List<string> output = [];
// Append the header first
output.AddRange(_header);
@@ -107,7 +114,9 @@ namespace SabreTools.Help
output.Add("Available options:");
foreach (string feature in _features.Keys)
{
output.AddRange(_features[feature].Output(pre: 2, midpoint: 30));
var outputs = _features[feature]?.Output(pre: 2, midpoint: 30);
if (outputs != null)
output.AddRange(outputs);
}
// And append the generic ending
@@ -124,7 +133,7 @@ namespace SabreTools.Help
public void OutputAllHelp()
{
// Start building the output list
List<string> output = new();
List<string> output = [];
// Append the header first
output.AddRange(_header);
@@ -133,7 +142,9 @@ namespace SabreTools.Help
output.Add("Available options:");
foreach (string feature in _features.Keys)
{
output.AddRange(_features[feature].OutputRecursive(0, pre: 2, midpoint: 30, includeLongDescription: true));
var outputs = _features[feature]?.OutputRecursive(0, pre: 2, midpoint: 30, includeLongDescription: true);
if (outputs != null)
output.AddRange(outputs);
}
// Now write out everything in a staged manner
@@ -145,8 +156,8 @@ namespace SabreTools.Help
/// </summary>
public static void OutputCredits()
{
List<string> credits = new()
{
List<string> credits =
[
_barrier,
"Credits",
_barrier,
@@ -156,7 +167,7 @@ namespace SabreTools.Help
"Testing: emuLOAD, @tractivo, Kludge, Obiwantje, edc",
"Suggestions: edc, AcidX, Amiga12, EliUmniCk",
"Based on work by: The Wizard of DATz"
};
];
WriteOutWithPauses(credits);
}
@@ -165,10 +176,10 @@ namespace SabreTools.Help
/// </summary>
/// <param name="featurename">Name of the feature to output information for, if possible</param>
/// <param name="includeLongDescription">True if the long description should be formatted and output, false otherwise</param>
public void OutputIndividualFeature(string featurename, bool includeLongDescription = false)
public void OutputIndividualFeature(string? featurename, bool includeLongDescription = false)
{
// Start building the output list
List<string> output = new();
List<string> output = [];
// If the feature name is null, empty, or just consisting of `-` characters, just show everything
if (string.IsNullOrEmpty(featurename?.TrimStart('-')))
@@ -178,8 +189,8 @@ namespace SabreTools.Help
}
// Now try to find the feature that has the name included
string realname = null;
List<string> startsWith = new();
string? realname = null;
List<string> startsWith = [];
foreach (string feature in _features.Keys)
{
// If we have a match to the feature name somehow
@@ -189,15 +200,21 @@ namespace SabreTools.Help
break;
}
// If we have an invalid feature
else if (!_features.ContainsKey(feature) || _features[feature] == null)
{
startsWith.Add(feature);
}
// If we have a match within the flags
else if (_features[feature].ContainsFlag(featurename))
else if (_features[feature]!.ContainsFlag(featurename))
{
realname = feature;
break;
}
// Otherwise, we want to get features with the same start
else if (_features[feature].StartsWith(featurename.TrimStart('-')[0]))
else if (_features[feature]!.StartsWith(featurename.TrimStart('-')[0]))
{
startsWith.Add(feature);
}
@@ -207,7 +224,7 @@ namespace SabreTools.Help
if (realname != null)
{
output.Add($"Available options for {realname}:");
output.AddRange(_features[realname].OutputRecursive(0, pre: 2, midpoint: 30, includeLongDescription: includeLongDescription));
output.AddRange(_features[realname]!.OutputRecursive(0, pre: 2, midpoint: 30, includeLongDescription: includeLongDescription));
}
// If no name was found but we have possible matches, show them
@@ -216,7 +233,7 @@ namespace SabreTools.Help
output.Add($"\"{featurename}\" not found. Did you mean:");
foreach (string possible in startsWith)
{
output.AddRange(_features[possible].Output(pre: 2, midpoint: 30, includeLongDescription: includeLongDescription));
output.AddRange(_features[possible]!.Output(pre: 2, midpoint: 30, includeLongDescription: includeLongDescription));
}
}
@@ -231,22 +248,22 @@ namespace SabreTools.Help
/// <returns>True if the feature was found, false otherwise</returns>
public bool TopLevelFlag(string flag)
{
return _features.Keys.Any(f => _features[f].ValidateInput(flag, exact: true));
return _features.Keys.Any(f => _features[f]?.ValidateInput(flag, exact: true) ?? false);
}
/// <summary>
/// Retrieve a list of enabled features
/// </summary>
/// <returns>List of Features representing what is enabled</returns>
public Dictionary<string, Feature> GetEnabledFeatures()
public Dictionary<string, Feature?> GetEnabledFeatures()
{
Dictionary<string, Feature> enabled = new();
Dictionary<string, Feature?> enabled = [];
// Loop through the features
foreach (KeyValuePair<string, Feature> feature in _features)
foreach (var feature in _features)
{
Dictionary<string, Feature> temp = GetEnabledSubfeatures(feature.Key, feature.Value);
foreach (KeyValuePair<string, Feature> tempfeat in temp)
var temp = GetEnabledSubfeatures(feature.Key, feature.Value);
foreach (var tempfeat in temp)
{
if (!enabled.ContainsKey(tempfeat.Key))
enabled.Add(tempfeat.Key, null);
@@ -264,19 +281,23 @@ namespace SabreTools.Help
/// <param name="key">Name that should be assigned to the feature</param>
/// <param name="feature">Feature with possible subfeatures to test</param>
/// <returns>List of Features representing what is enabled</returns>
private Dictionary<string, Feature> GetEnabledSubfeatures(string key, Feature feature)
private Dictionary<string, Feature?> GetEnabledSubfeatures(string key, Feature? feature)
{
Dictionary<string, Feature> enabled = new();
Dictionary<string, Feature?> enabled = [];
// If the feature is invalid
if (feature == null)
return enabled;
// First determine if the current feature is enabled
if (feature.IsEnabled())
enabled.Add(key, feature);
// Now loop through the subfeatures recursively
foreach (KeyValuePair<string, Feature> sub in feature.Features)
foreach (KeyValuePair<string, Feature?> sub in feature.Features)
{
Dictionary<string, Feature> temp = GetEnabledSubfeatures(sub.Key, sub.Value);
foreach (KeyValuePair<string, Feature> tempfeat in temp)
var temp = GetEnabledSubfeatures(sub.Key, sub.Value);
foreach (var tempfeat in temp)
{
if (!enabled.ContainsKey(tempfeat.Key))
enabled.Add(tempfeat.Key, null);

View File

@@ -128,15 +128,15 @@ namespace SabreTools.Help
protected static List<string> GetList(Dictionary<string, Feature> features, string key)
{
if (!features.ContainsKey(key))
return new List<string>();
return [];
return features[key].GetListValue() ?? new List<string>();
return features[key].GetListValue() ?? [];
}
/// <summary>
/// Get string value from nullable feature
/// </summary>
protected static string GetString(Dictionary<string, Feature> features, string key)
protected static string? GetString(Dictionary<string, Feature> features, string key)
{
if (!features.ContainsKey(key))
return null;