[Feature] Variable cleanup

This commit is contained in:
Matt Nadareski
2019-02-08 13:47:44 -08:00
parent 4fdd4ea44f
commit b8cd0a10bb

View File

@@ -10,12 +10,7 @@ namespace SabreTools.Library.Help
{ {
#region Private instance variables #region Private instance variables
private string _name;
private List<string> _flags;
private string _description;
private string _longDescription; // TODO: Use this to generate README.1ST?
private FeatureType _featureType; private FeatureType _featureType;
private Dictionary<string, Feature> _features;
private bool _foundOnce = false; private bool _foundOnce = false;
// Specific value types // Specific value types
@@ -29,26 +24,11 @@ namespace SabreTools.Library.Help
#region Publicly facing variables #region Publicly facing variables
public string Name public string Name { get; private set; }
{ public List<string> Flags { get; private set; }
get { return _name; } public string Description { get; private set; }
} public string LongDescription { get; private set; } // TODO: Use this to generate README.1ST?
public List<string> Flags public Dictionary<string, Feature> Features { get; private set; }
{
get { return _flags; }
}
public string Description
{
get { return _description; }
}
public string LongDescription
{
get { return _longDescription; }
}
public Dictionary<string, Feature> Features
{
get { return _features; }
}
#endregion #endregion
@@ -56,34 +36,33 @@ namespace SabreTools.Library.Help
public Feature() public Feature()
{ {
_name = null; this.Name = null;
_flags = new List<string>(); this.Flags = new List<string>();
_description = null; this.Description = null;
_longDescription = null; this.LongDescription = null;
_featureType = FeatureType.Flag; this._featureType = FeatureType.Flag;
_features = new Dictionary<string, Feature>(); this.Features = new Dictionary<string, Feature>();
} }
public Feature(string name, string flag, string description, FeatureType featureType, string longDescription = null) public Feature(string name, string flag, string description, FeatureType featureType, string longDescription = null)
{ {
_name = name; this.Name = name;
List<string> flags = new List<string>(); this.Flags = new List<string>();
flags.Add(flag); this.Flags.Add(flag);
_flags = flags; this.Description = description;
_description = description; this.LongDescription = longDescription;
_longDescription = longDescription; this._featureType = featureType;
_featureType = featureType; this.Features = new Dictionary<string, Feature>();
_features = new Dictionary<string, Feature>();
} }
public Feature(string name, List<string> flags, string description, FeatureType featureType, string longDescription = null) public Feature(string name, List<string> flags, string description, FeatureType featureType, string longDescription = null)
{ {
_name = name; this.Name = name;
_flags = flags; this.Flags = flags;
_description = description; this.Description = description;
_longDescription = longDescription; this.LongDescription = longDescription;
_featureType = featureType; this._featureType = featureType;
_features = new Dictionary<string, Feature>(); this.Features = new Dictionary<string, Feature>();
} }
#endregion #endregion
@@ -95,8 +74,8 @@ namespace SabreTools.Library.Help
/// </summary> /// </summary>
public Feature this[string name] public Feature this[string name]
{ {
get { return _features[name]; } get { return this.Features[name]; }
set { _features[name] = value; } set { this.Features[name] = value; }
} }
/// <summary> /// <summary>
@@ -104,8 +83,8 @@ namespace SabreTools.Library.Help
/// </summary> /// </summary>
public Feature this[Feature subfeature] public Feature this[Feature subfeature]
{ {
get { return _features[subfeature.Name]; } get { return this.Features[subfeature.Name]; }
set { _features[subfeature.Name] = value; } set { this.Features[subfeature.Name] = value; }
} }
/// <summary> /// <summary>
@@ -114,21 +93,12 @@ namespace SabreTools.Library.Help
/// <param name="feature"></param> /// <param name="feature"></param>
public void AddFeature(Feature feature) public void AddFeature(Feature feature)
{ {
if (_features == null) if (this.Features == null)
{ this.Features = new Dictionary<string, Feature>();
_features = new Dictionary<string, Feature>();
}
lock(_features) lock(this.Features)
{ {
if (!_features.ContainsKey(feature.Name)) this.Features[feature.Name] = feature;
{
_features.Add(feature.Name, feature);
}
else
{
_features[feature.Name] = feature;
}
} }
} }
@@ -138,14 +108,12 @@ namespace SabreTools.Library.Help
/// <param name="flag">Flag to add for this feature</param> /// <param name="flag">Flag to add for this feature</param>
public void AddFlag(string flag) public void AddFlag(string flag)
{ {
if (_flags == null) if (this.Flags == null)
{ this.Flags = new List<string>();
_flags = new List<string>();
}
lock (_flags) lock (this.Flags)
{ {
_flags.Add(flag); this.Flags.Add(flag);
} }
} }
@@ -155,14 +123,12 @@ namespace SabreTools.Library.Help
/// <param name="flags">List of flags to add to this feature</param> /// <param name="flags">List of flags to add to this feature</param>
public void AddFlags(List<string> flags) public void AddFlags(List<string> flags)
{ {
if (_flags == null) if (this.Flags == null)
{ this.Flags = new List<string>();
_flags = new List<string>();
}
lock (_flags) lock (this.Flags)
{ {
_flags.AddRange(flags); this.Flags.AddRange(flags);
} }
} }
@@ -176,7 +142,7 @@ namespace SabreTools.Library.Help
bool success = false; bool success = false;
// Loop through the flags // Loop through the flags
foreach (string flag in _flags) foreach (string flag in this.Flags)
{ {
if (flag == name) if (flag == name)
{ {
@@ -203,7 +169,7 @@ namespace SabreTools.Library.Help
bool success = false; bool success = false;
// Loop through the flags // Loop through the flags
foreach (string flag in _flags) foreach (string flag in this.Flags)
{ {
if (flag.TrimStart('-').ToLowerInvariant()[0] == c) if (flag.TrimStart('-').ToLowerInvariant()[0] == c)
{ {
@@ -237,8 +203,8 @@ namespace SabreTools.Library.Help
output += CreatePadding(pre); output += CreatePadding(pre);
// Preprocess the flags, if necessary // Preprocess the flags, if necessary
string[] newflags = new string[_flags.Count]; string[] newflags = new string[this.Flags.Count];
_flags.CopyTo(newflags); this.Flags.CopyTo(newflags);
switch (_featureType) switch (_featureType)
{ {
case FeatureType.Int32: case FeatureType.Int32:
@@ -270,7 +236,7 @@ namespace SabreTools.Library.Help
} }
// Append the description // Append the description
output += _description; output += this.Description;
// Now append it to the list // Now append it to the list
outputList.Add(output); outputList.Add(output);
@@ -285,7 +251,7 @@ namespace SabreTools.Library.Help
output = CreatePadding(pre + 4); output = CreatePadding(pre + 4);
// Now split the input description and start processing // Now split the input description and start processing
string[] split = _longDescription.Split(' '); string[] split = this.LongDescription.Split(' ');
for (int i = 0; i < split.Length; i++) for (int i = 0; i < split.Length; i++)
{ {
// If we have a newline character, reset the line and continue // If we have a newline character, reset the line and continue
@@ -380,8 +346,8 @@ namespace SabreTools.Library.Help
output += CreatePadding(preAdjusted); output += CreatePadding(preAdjusted);
// Preprocess the flags, if necessary // Preprocess the flags, if necessary
string[] newflags = new string[_flags.Count]; string[] newflags = new string[this.Flags.Count];
_flags.CopyTo(newflags); this.Flags.CopyTo(newflags);
switch (_featureType) switch (_featureType)
{ {
case FeatureType.Int32: case FeatureType.Int32:
@@ -413,7 +379,7 @@ namespace SabreTools.Library.Help
} }
// Append the description // Append the description
output += _description; output += this.Description;
// Now append it to the list // Now append it to the list
outputList.Add(output); outputList.Add(output);
@@ -428,7 +394,7 @@ namespace SabreTools.Library.Help
output = CreatePadding(preAdjusted + 4); output = CreatePadding(preAdjusted + 4);
// Now split the input description and start processing // Now split the input description and start processing
string[] split = _longDescription.Split(' '); string[] split = this.LongDescription.Split(' ');
for (int i = 0; i < split.Length; i++) for (int i = 0; i < split.Length; i++)
{ {
// If we have a newline character, reset the line and continue // If we have a newline character, reset the line and continue
@@ -478,9 +444,9 @@ namespace SabreTools.Library.Help
} }
// Now let's append all subfeatures // Now let's append all subfeatures
foreach (string feature in _features.Keys) foreach (string feature in this.Features.Keys)
{ {
outputList.AddRange(_features[feature].OutputRecursive(tabLevel + 1, pre, midpoint, includeLongDescription)); outputList.AddRange(this.Features[feature].OutputRecursive(tabLevel + 1, pre, midpoint, includeLongDescription));
} }
return outputList; return outputList;
@@ -502,7 +468,7 @@ namespace SabreTools.Library.Help
{ {
// If we have a flag, make sure it doesn't have an equal sign in it // If we have a flag, make sure it doesn't have an equal sign in it
case FeatureType.Flag: case FeatureType.Flag:
valid = !input.Contains("=") && _flags.Contains(input); valid = !input.Contains("=") && this.Flags.Contains(input);
if (valid) if (valid)
{ {
_valueBool = true; _valueBool = true;
@@ -518,7 +484,7 @@ namespace SabreTools.Library.Help
break; break;
// If we have an Int32, try to parse it if at all possible // If we have an Int32, try to parse it if at all possible
case FeatureType.Int32: case FeatureType.Int32:
valid = input.Contains("=") && _flags.Contains(input.Split('=')[0]); valid = input.Contains("=") && this.Flags.Contains(input.Split('=')[0]);
if (valid) if (valid)
{ {
if (!Int32.TryParse(input.Split('=')[1], out int value)) if (!Int32.TryParse(input.Split('=')[1], out int value))
@@ -538,7 +504,7 @@ namespace SabreTools.Library.Help
break; break;
// If we have an Int32, try to parse it if at all possible // If we have an Int32, try to parse it if at all possible
case FeatureType.Int64: case FeatureType.Int64:
valid = input.Contains("=") && _flags.Contains(input.Split('=')[0]); valid = input.Contains("=") && this.Flags.Contains(input.Split('=')[0]);
if (valid) if (valid)
{ {
if (!Int64.TryParse(input.Split('=')[1], out long value)) if (!Int64.TryParse(input.Split('=')[1], out long value))
@@ -558,7 +524,7 @@ namespace SabreTools.Library.Help
break; break;
// If we have an input, make sure it has an equals sign in it // If we have an input, make sure it has an equals sign in it
case FeatureType.List: case FeatureType.List:
valid = input.Contains("=") && _flags.Contains(input.Split('=')[0]); valid = input.Contains("=") && this.Flags.Contains(input.Split('=')[0]);
if (valid) if (valid)
{ {
if (_valueList == null) if (_valueList == null)
@@ -570,7 +536,7 @@ namespace SabreTools.Library.Help
} }
break; break;
case FeatureType.String: case FeatureType.String:
valid = input.Contains("=") && _flags.Contains(input.Split('=')[0]); valid = input.Contains("=") && this.Flags.Contains(input.Split('=')[0]);
if (valid) if (valid)
{ {
_valueString = input.Split('=')[1]; _valueString = input.Split('=')[1];
@@ -589,9 +555,9 @@ namespace SabreTools.Library.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 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) if (!valid && !exact)
{ {
foreach (string feature in _features.Keys) foreach (string feature in this.Features.Keys)
{ {
valid = _features[feature].ValidateInput(input); valid = this.Features[feature].ValidateInput(input);
// If we've found a valid feature, we break out // If we've found a valid feature, we break out
if (valid) if (valid)