|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace Https |
| 5 | +{ |
| 6 | + class Options |
| 7 | + { |
| 8 | + public ContentType RequestContentType { get; } |
| 9 | + public string XmlRootName { get; } |
| 10 | + public bool IgnoreCertificate { get; } |
| 11 | + public TimeSpan? Timeout { get; } |
| 12 | + public bool Version { get; } |
| 13 | + public bool Help { get; } |
| 14 | + public bool StopAutoRedirects { get; } |
| 15 | + |
| 16 | + public bool RequiresHandler => IgnoreCertificate || StopAutoRedirects; |
| 17 | + |
| 18 | + public Options(ContentType requestContentType, string xmlRootName, bool ignoreCertificate, TimeSpan? timeout, bool version, bool help, bool stopAutoRedirects) |
| 19 | + { |
| 20 | + RequestContentType = requestContentType; |
| 21 | + XmlRootName = xmlRootName; |
| 22 | + IgnoreCertificate = ignoreCertificate; |
| 23 | + Timeout = timeout; |
| 24 | + Version = version; |
| 25 | + Help = help; |
| 26 | + StopAutoRedirects = stopAutoRedirects; |
| 27 | + } |
| 28 | + |
| 29 | + public static IEnumerable<string> GetOptionHelp() |
| 30 | + { |
| 31 | + yield return "--form Renders the content arguments as application/x-www-form-urlencoded"; |
| 32 | + yield return "--help Show command line help."; |
| 33 | + yield return "--ignore-certificate Prevents server certificate validation."; |
| 34 | + yield return "--json Renders the content arguments as application/json."; |
| 35 | + yield return "--timeout=<VALUE> Sets the timeout of the request using System.TimeSpan.TryParse (https://docs.microsoft.com/en-us/dotnet/api/system.timespan.parse)"; |
| 36 | + yield return "--version Displays the application verison."; |
| 37 | + yield return "--xml=<ROOT_NAME> Renders the content arguments as application/xml using the optional xml root name."; |
| 38 | + yield return "--stop-auto-redirects Prevents redirects from automatically being processed."; |
| 39 | + } |
| 40 | + |
| 41 | + static int GetArgValueIndex(string arg) |
| 42 | + { |
| 43 | + var equalsIndex = arg.IndexOf('='); |
| 44 | + var spaceIndex = arg.IndexOf(' '); |
| 45 | + var index = equalsIndex > -1 && spaceIndex > -1 |
| 46 | + ? Math.Min(equalsIndex, spaceIndex) |
| 47 | + : Math.Max(equalsIndex, spaceIndex); |
| 48 | + |
| 49 | + return index == -1 ? index : index + 1; |
| 50 | + } |
| 51 | + |
| 52 | + public static Options Parse(IEnumerable<string> args) |
| 53 | + { |
| 54 | + var requestContentType = ContentType.Json; |
| 55 | + var xmlRootName = default(string); |
| 56 | + var ignoreCertificate = false; |
| 57 | + var timeout = default(TimeSpan?); |
| 58 | + var help = false; |
| 59 | + var version = false; |
| 60 | + var stopAutoRedirects = false; |
| 61 | + foreach (var arg in args) |
| 62 | + { |
| 63 | + if (arg.StartsWith("--json")) |
| 64 | + { |
| 65 | + requestContentType = ContentType.Json; |
| 66 | + } |
| 67 | + else if (arg.StartsWith("--xml")) |
| 68 | + { |
| 69 | + var index = GetArgValueIndex(arg); |
| 70 | + if (index == -1) |
| 71 | + { |
| 72 | + xmlRootName = "xml"; |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + xmlRootName = arg.Substring(index).Trim(); |
| 77 | + if (string.IsNullOrEmpty(xmlRootName)) |
| 78 | + { |
| 79 | + xmlRootName = "xml"; |
| 80 | + } |
| 81 | + } |
| 82 | + requestContentType = ContentType.Xml; |
| 83 | + } |
| 84 | + else if (arg.StartsWith("--form")) |
| 85 | + { |
| 86 | + requestContentType = ContentType.FormUrlEncoded; |
| 87 | + } |
| 88 | + else if (arg.StartsWith("--ignore-certificate")) |
| 89 | + { |
| 90 | + ignoreCertificate = true; |
| 91 | + } |
| 92 | + else if (arg.StartsWith("--timeout")) |
| 93 | + { |
| 94 | + var index = GetArgValueIndex(arg); |
| 95 | + if (index > -1) |
| 96 | + { |
| 97 | + var s = arg.Substring(index).Trim(); |
| 98 | + if (TimeSpan.TryParse(s, out var to) && to > TimeSpan.Zero) |
| 99 | + { |
| 100 | + timeout = to; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + else if (arg.StartsWith("--version")) |
| 105 | + { |
| 106 | + version = true; |
| 107 | + } |
| 108 | + else if (arg.StartsWith("--help") || arg.StartsWith("-?") || arg.StartsWith("help")) |
| 109 | + { |
| 110 | + help = true; |
| 111 | + } |
| 112 | + else if (arg.StartsWith("--stop-auto-redirects")) |
| 113 | + { |
| 114 | + stopAutoRedirects = true; |
| 115 | + } |
| 116 | + } |
| 117 | + return new Options(requestContentType, xmlRootName, ignoreCertificate, timeout, version, help, stopAutoRedirects); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments