/// <summary> /// Parses porting flag options from a <see cref="List{T}"/> of <see cref="string"/>. /// </summary> /// <param name="args"></param> private void ParsePortingOptions(List <string> args) { Flags = PortingFlags.Default; var flagNames = Enum.GetNames(typeof(PortingFlags)).Select(name => name.ToLower()); var flagValues = Enum.GetValues(typeof(PortingFlags)) as PortingFlags[]; for (var a = 0; a < args.Count(); a++) { var arg = args[a].ToLower(); // Support legacy arguments arg = arg.Replace("single", $"!{nameof(PortingFlags.Recursive)}"); arg = arg.Replace("noshaders", $"!{nameof(PortingFlags.MatchShaders)}"); arg = arg.Replace("silent", $"!{nameof(PortingFlags.Print)}"); arg = arg.ToLower(); // do this again incase the argument was replaced // Use '!' or 'No' to negate an argument. var toggleOn = !(arg.StartsWith("!") || arg.StartsWith("no")); if (!toggleOn && arg.StartsWith("!")) { arg = arg.Remove(0, 1); } else if (!toggleOn && arg.StartsWith("no")) { arg = arg.Remove(0, 2); } // Throw exceptiosn at clumsy typers. if (!flagNames.Contains(arg)) { throw new FormatException($"Invalid {typeof(PortingFlags).FullName}: {args[0]}"); } // Add/remove flags based on if they appeared as arguments, // and whether they were negated with '!' or 'No' for (var i = 0; i < flagNames.Count(); i++) { if (arg == flagNames.ElementAt(i)) { if (toggleOn) { SetFlags(flagValues[i]); } else { RemoveFlags(flagValues[i]); } } } } }
static bool ParsePortingFlag(string value, out PortingFlags flag, out bool isSet) { flag = 0; isSet = true; string[] portingFlagNames = Enum.GetNames(typeof(PortingFlags)); Array portingFlagValues = Enum.GetValues(typeof(PortingFlags)); if (value[0] == '!') { value = value.Substring(1); isSet = false; } for (int i = 0; i < portingFlagNames.Length; i++) { if (portingFlagNames[i].ToLower() == value.ToLower()) { flag = (PortingFlags)portingFlagValues.GetValue(i); return(true); } } return(false); }
/// <summary> /// Toggles flags on or off (<see cref="PortTagCommand.Flags"/> ^= <see cref="PortingFlags"/>). /// </summary> /// <param name="flags">The <see cref="PortingFlags"/> to toggle.</param> public PortingFlags ToggleFlags(PortingFlags flags) => Flags ^= flags;
/// <summary> /// Removes flags explicitly (<see cref="PortTagCommand.Flags"/> &= ~<see cref="PortingFlags"/>). /// </summary> /// <param name="flags">The <see cref="PortingFlags"/> to remove.</param> public PortingFlags RemoveFlags(PortingFlags flags) => Flags &= ~flags;
/// <summary> /// Sets flags explicitly (<see cref="PortTagCommand.Flags"/> |= <see cref="PortingFlags"/>). /// </summary> /// <param name="flags">The <see cref="PortingFlags"/> to set.</param> public PortingFlags SetFlags(PortingFlags flags) => Flags |= flags;
/// <summary> /// True if ANY of the supplied <see cref="PortingFlags"/> are set, false if none are: /// (<see cref="PortTagCommand.Flags"/> & flags) != 0 /// </summary> /// <param name="flags">The <see cref="PortingFlags"/> to check.</param> public bool FlagsAnySet(PortingFlags flags) => (Flags & flags) != 0;
/// <summary> /// True if the flag is set (this is 100% the same as <see cref="FlagsAnySet(PortingFlags)"/>, /// other than the name. /// </summary> /// <param name="flag"></param> /// <returns></returns> public bool FlagIsSet(PortingFlags flag) => (Flags & flag) != 0;
/// <summary> /// True if ALL of the supplied <see cref="PortingFlags"/> are set, false if any aren't: /// (<see cref="PortTagCommand.Flags"/> & flags) == flags /// </summary> /// <param name="flags">The <see cref="PortingFlags"/> to check.</param> public bool FlagsAllSet(PortingFlags flags) => (Flags & flags) == flags;
/// <summary> /// Toggles flags on or off (<see cref="PortTagCommand.Flags"/> ^= <see cref="PortingFlags"/>). /// </summary> /// <param name="flags">The <see cref="PortingFlags"/> to toggle.</param> private PortingFlags ToggleFlags(PortingFlags flags) => Flags ^= flags;
/// <summary> /// Removes flags explicitly (<see cref="PortTagCommand.Flags"/> &= ~<see cref="PortingFlags"/>). /// </summary> /// <param name="flags">The <see cref="PortingFlags"/> to remove.</param> private PortingFlags RemoveFlags(PortingFlags flags) => Flags &= ~flags;
/// <summary> /// Sets flags explicitly (<see cref="PortTagCommand.Flags"/> |= <see cref="PortingFlags"/>). /// </summary> /// <param name="flags">The <see cref="PortingFlags"/> to set.</param> private PortingFlags SetFlags(PortingFlags flags) => Flags |= flags;
/// <summary> /// True if ANY of the supplied <see cref="PortingFlags"/> are set, false if none are: /// (<see cref="PortTagCommand.Flags"/> & flags) != 0 /// </summary> /// <param name="flags">The <see cref="PortingFlags"/> to check.</param> private bool FlagsAnySet(PortingFlags flags) => (Flags & flags) != 0;
/// <summary> /// True if the flag is set (this is 100% the same as <see cref="FlagsAnySet(PortingFlags)"/>, /// other than the name. /// </summary> /// <param name="flag"></param> /// <returns></returns> private bool FlagIsSet(PortingFlags flag) => (Flags & flag) != 0;
/// <summary> /// True if ALL of the supplied <see cref="PortingFlags"/> are set, false if any aren't: /// (<see cref="PortTagCommand.Flags"/> & flags) == flags /// </summary> /// <param name="flags">The <see cref="PortingFlags"/> to check.</param> private bool FlagsAllSet(PortingFlags flags) => (Flags & flags) == flags;