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