forked from natemcmaster/CommandLineUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
41 lines (34 loc) · 1.42 KB
/
Program.cs
File metadata and controls
41 lines (34 loc) · 1.42 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
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
/// <summary>
/// You can call <see cref="CommandLineApplication.OnExecute(Func{Task{int}})" />
/// with an async lambda to have your application execute asynchronously.
/// </summary>
public class AsyncWithBuilderApi
{
public static int Main(string[] args)
{
var app = new CommandLineApplication();
app.HelpOption("-h|--help");
var optionSubject = app.Option("-s|--subject <SUBJECT>", "The subject", CommandOptionType.SingleValue);
var optionRepeat = app.Option<int>("-n|--count <N>", "Repeat", CommandOptionType.SingleValue);
app.OnExecuteAsync(async cancellationToken =>
{
var subject = optionSubject.HasValue()
? optionSubject.Value()
: "world";
var count = optionRepeat.HasValue() ? optionRepeat.ParsedValue : 1;
for (var i = 0; i < count; i++)
{
Console.Write($"Hello");
// This pause here is just for indication that some awaitable operation could happens here.
await Task.Delay(5000, cancellationToken);
Console.WriteLine($" {subject}!");
}
});
return app.Execute(args);
}
}