Skip to content

Commit 49a90e9

Browse files
committed
Adds better help documentation
1 parent 174f4f8 commit 49a90e9

File tree

1 file changed

+90
-9
lines changed

1 file changed

+90
-9
lines changed

src/https/Program.cs

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,37 @@ static IEnumerable<Content> ParseContents(IEnumerable<string> args)
127127
}
128128
}
129129

130-
void Help()
130+
void Version()
131131
{
132132
var stream = _stdout();
133133
var writer = new StreamWriter(stream) { AutoFlush = true };
134134
writer.Write("dotnet-https ");
135135
writer.WriteLine(typeof(Program).Assembly.GetName().Version);
136-
writer.WriteLine("https [method] [uri] [options] [content]");
137-
writer.WriteLine("For example https put httpbin.org/put hello=world");
136+
writer.Flush();
137+
}
138+
139+
void Help()
140+
{
141+
var stream = _stdout();
142+
var writer = new StreamWriter(stream) { AutoFlush = true };
143+
writer.WriteLine("Usage: https <METHOD> <URI> [options] [content]");
144+
writer.WriteLine("");
145+
writer.WriteLine("Submits HTTP requests. For example https put httpbin.org/put hello=world");
146+
writer.WriteLine("");
147+
writer.WriteLine("Arguments:");
148+
writer.WriteLine(" <METHOD> HTTP method, i.e., get, head, post");
149+
writer.WriteLine(" <URI> URI to send the request to. Leaving the protocol off the URI defaults to https://");
150+
writer.WriteLine("");
151+
writer.WriteLine("Options:");
152+
foreach (var option in Options.GetOptionHelp())
153+
{
154+
writer.Write(" ");
155+
writer.WriteLine(option);
156+
}
157+
writer.WriteLine("");
158+
writer.WriteLine("Content:");
159+
writer.WriteLine("Repeat as many content arguments to create content sent with the HTTP request. Alternatively pipe raw content send as the HTTP request content.");
160+
writer.WriteLine(" <KEY>=<VALUE>");
138161
writer.WriteLine("");
139162

140163
writer.Flush();
@@ -175,6 +198,24 @@ public Program(Func<Stream> stderr, Func<Stream> stdin, Func<Stream> stdout, boo
175198
public static Task<int> Main(string[] args) =>
176199
new Program().RunAsync(args);
177200

201+
int HandleOptionsOnly(string[] args)
202+
{
203+
var options = Options.Parse(args);
204+
if (options.Help)
205+
{
206+
Help();
207+
return 0;
208+
}
209+
else if(options.Version)
210+
{
211+
Version();
212+
return 0;
213+
}
214+
215+
Help();
216+
return 1;
217+
}
218+
178219
public async Task<int> RunAsync(string[] args)
179220
{
180221
if (!args.Any())
@@ -190,19 +231,27 @@ public async Task<int> RunAsync(string[] args)
190231
{
191232
if (!Command.TryParse(args[0], out command))
192233
{
193-
Help();
194-
return 1;
234+
return HandleOptionsOnly(args);
195235
}
196236
}
197237
}
198238
else if (!Command.TryParse(args[0], out command))
199239
{
200-
Help();
201-
return 1;
240+
return HandleOptionsOnly(args);
202241
}
203242

204243
var optionArgs = args.Skip(2).TakeWhile(x => x.Length > 0 && x[0] == '-');
205244
var options = Options.Parse(optionArgs);
245+
if (options.Help)
246+
{
247+
Help();
248+
return 0;
249+
}
250+
else if (options.Version)
251+
{
252+
Version();
253+
return 0;
254+
}
206255

207256
var contentArgs = args.Skip(2).SkipWhile(x => x.Length > 0 && x[0] == '-');
208257

@@ -302,20 +351,39 @@ class Options
302351
public string XmlRootName { get; }
303352
public bool IgnoreCertificate { get; }
304353
public TimeSpan? Timeout { get; }
354+
public bool Version { get; }
355+
public bool Help { get; }
356+
305357

306-
public Options(ContentType requestContentType, string xmlRootName, bool ignoreCertificate, TimeSpan? timeout)
358+
public Options(ContentType requestContentType, string xmlRootName, bool ignoreCertificate, TimeSpan? timeout, bool version, bool help)
307359
{
308360
RequestContentType = requestContentType;
309361
XmlRootName = xmlRootName;
310362
IgnoreCertificate = ignoreCertificate;
311363
Timeout = timeout;
364+
Version = version;
365+
Help = help;
312366
}
367+
368+
public static IEnumerable<string> GetOptionHelp()
369+
{
370+
yield return "--form Renders the content arguments as application/x-www-form-urlencoded";
371+
yield return "--help Show command line help.";
372+
yield return "--ignore-certificate Prevents server certificate validation.";
373+
yield return "--json Renders the content arguments as application/json.";
374+
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)";
375+
yield return "--version Displays the application verison.";
376+
yield return "--xml <ROOT_NAME> Renders the content arguments as application/xml using the optional xml root name.";
377+
}
378+
313379
public static Options Parse(IEnumerable<string> args)
314380
{
315381
var requestContentType = ContentType.Json;
316382
var xmlRootName = default(string);
317383
var ignoreCertificate = false;
318384
var timeout = default(TimeSpan?);
385+
var help = false;
386+
var version = false;
319387
foreach (var arg in args)
320388
{
321389
if (arg.StartsWith("--json"))
@@ -359,8 +427,16 @@ public static Options Parse(IEnumerable<string> args)
359427
}
360428
}
361429
}
430+
else if (arg.StartsWith("--version"))
431+
{
432+
version = true;
433+
}
434+
else if (arg.StartsWith("--help"))
435+
{
436+
help = true;
437+
}
362438
}
363-
return new Options(requestContentType, xmlRootName, ignoreCertificate, timeout);
439+
return new Options(requestContentType, xmlRootName, ignoreCertificate, timeout, version, help);
364440
}
365441
}
366442

@@ -621,6 +697,11 @@ public static bool TryParse(string methodText, string uriText, out Command comma
621697
public static bool TryParse(string s, out Command command)
622698
{
623699
s = s.Trim();
700+
if (s.StartsWith('-'))
701+
{
702+
command = default;
703+
return false;
704+
}
624705

625706
var index = s.IndexOf(' ');
626707
if (index == -1)

0 commit comments

Comments
 (0)