| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using GDX.Collections.Generic; |
| | 4 | |
|
| | 5 | | namespace GDX.Developer |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// A simplified commandline parser that handles arguments which follow the <c>--FLAG</c> or <c>--KEY=VALUE</c> |
| | 9 | | /// Automatically initialized during normal runtime operations, however can be manually triggered for author tim |
| | 10 | | /// use cases as well. |
| | 11 | | /// </summary> |
| | 12 | | /// <remarks> |
| | 13 | | /// <para> |
| | 14 | | /// The prefix and divider are configurable via the <see cref="Config" />, however the always, the <c>FLAG</ |
| | 15 | | /// and <c>KEY</c> will be Uppercase. |
| | 16 | | /// </para> |
| | 17 | | /// </remarks> |
| | 18 | | public static class CommandLineParser |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// The processed arguments found after parsing the arguments |
| | 22 | | /// </summary> |
| 2 | 23 | | public static StringKeyDictionary<string> Arguments = new StringKeyDictionary<string>(5); |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// The processed flags found in the arguments. |
| | 27 | | /// </summary> |
| 2 | 28 | | public static readonly List<string> Flags = new List<string>(); |
| | 29 | |
|
| | 30 | | static CommandLineParser() |
| 2 | 31 | | { |
| 2 | 32 | | ProcessArguments(Environment.GetCommandLineArgs()); |
| 2 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Process an array of arguments into <see cref="Arguments" /> and <see cref="Flags" />. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="argumentArray">An array of arguments to process.</param> |
| | 39 | | /// <param name="shouldClear">Should the storage containers be cleared.</param> |
| | 40 | | public static void ProcessArguments(string[] argumentArray, bool shouldClear = true) |
| 4 | 41 | | { |
| 4 | 42 | | if (shouldClear) |
| 4 | 43 | | { |
| 4 | 44 | | Arguments.Clear(); |
| 4 | 45 | | Flags.Clear(); |
| 4 | 46 | | } |
| | 47 | |
|
| 4 | 48 | | string argumentPrefix = Config.CommandLineParserArgumentPrefix; |
| 4 | 49 | | int prefixLength = argumentPrefix.Length; |
| 4 | 50 | | string argumentSplit = Config.CommandLineParserArgumentSplit; |
| 4 | 51 | | int argumentLength = argumentArray.Length; |
| 176 | 52 | | for (int i = 0; i < argumentLength; i++) |
| 84 | 53 | | { |
| | 54 | | // Cache current argument |
| 84 | 55 | | string argument = argumentArray[i]; |
| | 56 | |
|
| | 57 | | // Has the starter and has an assignment |
| 84 | 58 | | if (!argument.StartsWith(argumentPrefix)) |
| 80 | 59 | | { |
| 80 | 60 | | continue; |
| | 61 | | } |
| | 62 | |
|
| 4 | 63 | | if (argument.Contains(argumentSplit)) |
| 2 | 64 | | { |
| 2 | 65 | | int keyEnd = argument.IndexOf(argumentSplit, StringComparison.Ordinal); |
| 2 | 66 | | string key = argument.Substring(prefixLength, keyEnd - prefixLength); |
| | 67 | |
|
| 2 | 68 | | int valueStart = argument.IndexOf(argumentSplit, StringComparison.Ordinal) + 1; |
| 2 | 69 | | int valueEnd = argument.Length; |
| 2 | 70 | | string value = argument.Substring(valueStart, valueEnd - valueStart); |
| | 71 | |
|
| | 72 | | // Value parameter |
| 2 | 73 | | Arguments.AddSafe(key.ToUpper(), value); |
| 2 | 74 | | } |
| | 75 | | else |
| 2 | 76 | | { |
| 2 | 77 | | string flag = argument.Substring(prefixLength).ToUpper(); |
| | 78 | |
|
| | 79 | | // Flag parameter, ensure they are unique |
| 2 | 80 | | if (!Flags.Contains(flag)) |
| 2 | 81 | | { |
| 2 | 82 | | Flags.Add(flag); |
| 2 | 83 | | } |
| 2 | 84 | | } |
| 4 | 85 | | } |
| 4 | 86 | | } |
| | 87 | | } |
| | 88 | | } |