| | 1 | | using System.Reflection; |
| | 2 | |
|
| | 3 | | namespace Towel; |
| | 4 | |
|
| | 5 | | /// <summary>Contains static helpers for handling command line input and output.</summary> |
| | 6 | | public static class CommandLine |
| | 7 | | { |
| | 8 | | /// <summary>Handles the command line arguments by invoking the relative <see cref="CommandAttribute"/> method in the |
| | 9 | | /// <param name="args">The command line arguments.</param> |
| | 10 | | public static void HandleArguments(string[]? args = null) |
| 24 | 11 | | { |
| 24 | 12 | | Assembly assembly = Assembly.GetCallingAssembly(); |
| 24 | 13 | | args ??= Environment.GetCommandLineArgs(); |
| 24 | 14 | | if (args.Length < 1) |
| 0 | 15 | | { |
| 0 | 16 | | Console.Error.WriteLine("No command provided."); |
| 0 | 17 | | return; |
| | 18 | | } |
| 24 | 19 | | ListArray<MethodInfo> commandMatches = new(); |
| 360 | 20 | | foreach (MethodInfo possibleCommand in assembly.GetMethodInfosWithAttribute<CommandAttribute>()) |
| 144 | 21 | | { |
| 144 | 22 | | string command = possibleCommand.Name; |
| 144 | 23 | | if (command == args[0]) |
| 24 | 24 | | { |
| 24 | 25 | | commandMatches.Add(possibleCommand); |
| 24 | 26 | | } |
| 144 | 27 | | } |
| 24 | 28 | | if (commandMatches.Count > 1) |
| 0 | 29 | | { |
| 0 | 30 | | throw new Exception("syntax error: multiple matching commands"); |
| | 31 | | } |
| 24 | 32 | | else if (commandMatches.Count <= 0) |
| 0 | 33 | | { |
| 0 | 34 | | string arg = args[0].ToLower().Replace("-", string.Empty); |
| 0 | 35 | | if (arg is "help" || arg is "h") |
| 0 | 36 | | { |
| 0 | 37 | | if (args.Length is 2) |
| 0 | 38 | | { |
| 0 | 39 | | DefaultHelp(assembly, args[1]); |
| 0 | 40 | | } |
| | 41 | | else |
| 0 | 42 | | { |
| 0 | 43 | | DefaultHelp(assembly); |
| 0 | 44 | | } |
| 0 | 45 | | return; |
| | 46 | | } |
| 0 | 47 | | if (arg is "version" || |
| 0 | 48 | | arg is "v") |
| 0 | 49 | | { |
| 0 | 50 | | DefaultVersion(assembly); |
| 0 | 51 | | return; |
| | 52 | | } |
| 0 | 53 | | Console.Error.WriteLine("command not found"); |
| 0 | 54 | | return; |
| | 55 | | } |
| 24 | 56 | | if ((args.Length - 1) % 2 != 0) |
| 0 | 57 | | { |
| 0 | 58 | | Console.Error.WriteLine($"Invalid argument count."); |
| 0 | 59 | | return; |
| | 60 | | } |
| 24 | 61 | | MethodInfo methodInfo = commandMatches[0]; |
| 24 | 62 | | if (!methodInfo.IsStatic) |
| 0 | 63 | | { |
| 0 | 64 | | throw new Exception("syntax error: relative command not static"); |
| | 65 | | } |
| | 66 | |
|
| 24 | 67 | | MapHashLinked<int, string, StringEquate, StringHash> parameterMap = new(); |
| 24 | 68 | | int parameterCount = 0; |
| 24 | 69 | | ParameterInfo[] parameterInfos = methodInfo.GetParameters(); |
| 140 | 70 | | foreach (ParameterInfo parameterInfo in parameterInfos) |
| 34 | 71 | | { |
| 34 | 72 | | if (parameterInfo.Name is null) throw new Exception("encountered a null parameter name"); |
| 34 | 73 | | parameterMap.Add(parameterInfo.Name, parameterCount++); |
| 34 | 74 | | } |
| 24 | 75 | | object?[] parameters = new object[parameterCount]; |
| 90 | 76 | | for (int i = 1; i < args.Length; i += 2) |
| 26 | 77 | | { |
| 26 | 78 | | string arg = args[i]; |
| 26 | 79 | | if (arg.Length < 3 || arg[0] is not '-' || arg[1] is not '-') |
| 0 | 80 | | { |
| 0 | 81 | | Console.Error.WriteLine($"Invalid parameter {arg} in index {i}."); |
| 0 | 82 | | return; |
| | 83 | | } |
| 26 | 84 | | arg = arg[2..]; |
| 26 | 85 | | var (success, _, index) = parameterMap.TryGet(arg); |
| 26 | 86 | | if (!success) |
| 5 | 87 | | { |
| 5 | 88 | | Console.Error.WriteLine($"Invalid parameter --{arg} in index {i}."); |
| 5 | 89 | | return; |
| | 90 | | } |
| 21 | 91 | | if (parameters[index] is not null) |
| 0 | 92 | | { |
| 0 | 93 | | Console.Error.WriteLine($"Duplicate parameters provided --{arg}."); |
| 0 | 94 | | return; |
| | 95 | | } |
| 21 | 96 | | Type parameterType = parameterInfos[index].ParameterType; |
| 21 | 97 | | if (parameterType == typeof(string)) |
| 0 | 98 | | { |
| 0 | 99 | | parameters[index] = args[i + 1]; |
| 0 | 100 | | } |
| | 101 | | else |
| 21 | 102 | | { |
| | 103 | | MethodInfo? tryParse; |
| | 104 | | ConstructorInfo? constuctor; |
| 21 | 105 | | if ((tryParse = Meta.GetTryParseMethod(parameterType)) is not null) |
| 20 | 106 | | { |
| 20 | 107 | | object[] tryParseParameters = new object[2]; |
| 20 | 108 | | tryParseParameters[0] = args[i + 1]; |
| 20 | 109 | | object? result = tryParse.Invoke(null, tryParseParameters); |
| 20 | 110 | | if (result is not bool resultBool || !resultBool) |
| 0 | 111 | | { |
| 0 | 112 | | Console.Error.WriteLine($"Could not parse parameter value --{arg} {args[i + 1]}."); |
| 0 | 113 | | return; |
| | 114 | | } |
| 20 | 115 | | parameters[index] = tryParseParameters[1]; |
| 20 | 116 | | } |
| 1 | 117 | | else if ((constuctor = parameterType.GetConstructor(Ɐ(typeof(string)))) is not null) |
| 1 | 118 | | { |
| 1 | 119 | | parameters[index] = constuctor.Invoke(Ɐ(args[i + 1])); |
| 1 | 120 | | } |
| | 121 | | else |
| 0 | 122 | | { |
| 0 | 123 | | throw new Exception("syntax error: invalid type used (no tryparse found)"); |
| | 124 | | } |
| 21 | 125 | | } |
| 21 | 126 | | } |
| 72 | 127 | | for (int i = 0; i < parameters.Length; i++) |
| 24 | 128 | | { |
| 24 | 129 | | if (parameters[i] is null) |
| 9 | 130 | | { |
| 9 | 131 | | if (!parameterInfos[i].HasDefaultValue) |
| 7 | 132 | | { |
| 7 | 133 | | Console.Error.WriteLine($"Missing required parameter --{parameterInfos[i].Name} {parameterInfos[i].ParameterTy |
| 7 | 134 | | return; |
| | 135 | | } |
| 2 | 136 | | parameters[i] = parameterInfos[i].DefaultValue; |
| 2 | 137 | | } |
| 17 | 138 | | } |
| 12 | 139 | | methodInfo.Invoke(null, parameters); |
| 24 | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary>Gets the default output for the version command.</summary> |
| | 143 | | /// <param name="assembly">The application assembly.</param> |
| | 144 | | public static void DefaultVersion(Assembly? assembly = null) |
| 0 | 145 | | { |
| 0 | 146 | | assembly ??= Assembly.GetCallingAssembly(); |
| 0 | 147 | | AssemblyName assemblyName = assembly.GetName(); |
| 0 | 148 | | Console.WriteLine($"Assembly: {assemblyName.Name}"); |
| 0 | 149 | | Console.WriteLine($"Version: {assemblyName.Version}"); |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | /// <summary>Gets the default output for the help command.</summary> |
| | 153 | | /// <param name="assembly">The application assembly.</param> |
| | 154 | | /// <param name="command">The command to get the default help output for.</param> |
| | 155 | | public static void DefaultHelp(Assembly? assembly = null, string? command = null) |
| 0 | 156 | | { |
| 0 | 157 | | assembly ??= Assembly.GetCallingAssembly(); |
| 0 | 158 | | if (command is null) |
| 0 | 159 | | { |
| 0 | 160 | | DefaultVersion(assembly); |
| 0 | 161 | | Console.WriteLine("Commands:"); |
| 0 | 162 | | foreach (MethodInfo methodInfo in assembly.GetMethodInfosWithAttribute<CommandAttribute>()) |
| 0 | 163 | | { |
| 0 | 164 | | Console.WriteLine(" " + methodInfo.Name); |
| 0 | 165 | | } |
| 0 | 166 | | Console.WriteLine(@"Use ""Help X"" for detailed info per ""X"" command."); |
| 0 | 167 | | } |
| | 168 | | else |
| 0 | 169 | | { |
| 0 | 170 | | ListArray<MethodInfo> commandMatches = new(); |
| 0 | 171 | | foreach (MethodInfo methodInfo in assembly.GetMethodInfosWithAttribute<CommandAttribute>()) |
| 0 | 172 | | { |
| 0 | 173 | | string methodName = methodInfo.Name; |
| 0 | 174 | | if (methodName == command) |
| 0 | 175 | | { |
| 0 | 176 | | commandMatches.Add(methodInfo); |
| 0 | 177 | | } |
| 0 | 178 | | } |
| 0 | 179 | | if (commandMatches.Count > 1) |
| 0 | 180 | | { |
| 0 | 181 | | throw new Exception("syntax error: multiple matching commands"); |
| | 182 | | } |
| 0 | 183 | | else if (commandMatches.Count <= 0) |
| 0 | 184 | | { |
| 0 | 185 | | Console.Error.WriteLine("command not found"); |
| 0 | 186 | | return; |
| | 187 | | } |
| | 188 | | else |
| 0 | 189 | | { |
| 0 | 190 | | MethodInfo methodInfo = commandMatches[0]; |
| 0 | 191 | | string? documentation = Meta.GetDocumentation(methodInfo); |
| 0 | 192 | | if (documentation is null) |
| 0 | 193 | | { |
| 0 | 194 | | Console.WriteLine("Parameters:"); |
| 0 | 195 | | foreach (ParameterInfo parameterInfo in methodInfo.GetParameters()) |
| 0 | 196 | | { |
| 0 | 197 | | Console.WriteLine("--" + parameterInfo.Name); |
| 0 | 198 | | } |
| 0 | 199 | | } |
| | 200 | | else |
| 0 | 201 | | { |
| 0 | 202 | | Console.WriteLine(documentation); |
| 0 | 203 | | } |
| 0 | 204 | | } |
| 0 | 205 | | } |
| 0 | 206 | | } |
| | 207 | |
|
| | 208 | | /// <summary>Indicates that a method is invocable from the command line arguments.</summary> |
| | 209 | | [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] |
| | 210 | | public class CommandAttribute : Attribute { } |
| | 211 | | } |