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