-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CommandBase.cs
55 lines (43 loc) · 1.54 KB
/
CommandBase.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.NET.Sdk.Razor.Tool.CommandLineUtils;
namespace Microsoft.NET.Sdk.Razor.Tool
{
internal abstract class CommandBase : CommandLineApplication
{
public const int ExitCodeSuccess = 0;
public const int ExitCodeFailure = 1;
public const int ExitCodeFailureRazorError = 2;
protected CommandBase(Application parent, string name)
: base(throwOnUnexpectedArg: true)
{
if (parent == null)
{
throw new ArgumentNullException(nameof(parent));
}
base.Parent = parent;
Name = name;
Out = parent.Out ?? Out;
Error = parent.Error ?? Error;
Help = HelpOption("-?|-h|--help");
OnExecute((Func<Task<int>>)ExecuteAsync);
}
protected new Application Parent => (Application)base.Parent;
protected CancellationToken Cancelled => Parent?.CancellationToken ?? default;
protected CommandOption Help { get; }
protected virtual bool ValidateArguments()
{
return true;
}
protected abstract Task<int> ExecuteCoreAsync();
private async Task<int> ExecuteAsync()
{
if (!ValidateArguments())
{
ShowHelp();
return ExitCodeFailureRazorError;
}
return await ExecuteCoreAsync();
}
}
}