-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCli.cs
More file actions
199 lines (174 loc) · 10.8 KB
/
Cli.cs
File metadata and controls
199 lines (174 loc) · 10.8 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Threading;
using System.Threading.Tasks;
#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
namespace DotMake.CommandLine
{
/// <summary>
/// Provides methods for parsing command line input and running an indicated command.
/// </summary>
/// <example>
/// <code id="gettingStartedDelegate" source="../TestApp/CliExamples.cs" region="CliRunDelegate" language="cs" />
/// <code id="gettingStartedClass">
/// <code source="../TestApp/Commands/RootCliCommand.cs" region="RootCliCommand" language="cs" />
/// <code source="../TestApp/CliExamples.cs" region="CliRun" language="cs" />
/// <code source="../TestApp/CliExamples.cs" region="CliParse" language="cs" />
/// </code>
/// <code id="gettingStartedClass2" source="../TestApp/Commands/RootHelpOnEmptyCliCommand.cs" region="RootHelpOnEmptyCliCommand" language="cs" />
/// <code>
/// <code source="../TestApp/CliExamples.cs" region="CliRunWithReturn" language="cs" />
/// <code source="../TestApp/CliExamples.cs" region="CliRunAsync" language="cs" />
/// <code source="../TestApp/CliExamples.cs" region="CliRunAsyncWithReturn" language="cs" />
/// <code source="../TestApp/CliExamples.cs" region="CliParseWithResult" language="cs" />
/// </code>
/// <code source="../TestApp/CliExamples.cs" region="CliRunExceptions" language="cs" />
/// <code>
/// <code source="../TestApp.NugetDI/Program.cs" region="Namespace" language="cs" />
/// <code source="../TestApp.NugetDI/Program.cs" region="ConfigureServices" language="cs" />
/// <code source="../TestApp.NugetDI/Commands/RootCliCommand.cs" region="RootCliCommand" language="cs" />
/// </code>
/// </example>
public static class Cli
{
/// <summary>
/// <inheritdoc cref="CliExtensions" path="/summary/node()" />
/// </summary>
public static CliExtensions Ext { get; } = new CliExtensions();
/// <summary>
/// Gets a CLI parser configured for the indicated command.
/// </summary>
/// <typeparam name="TDefinition">The definition class for the command. A command builder for this class should be automatically generated by the source generator.</typeparam>
/// <param name="settings">The settings for the parser's grammar and behaviors.</param>
/// <returns>A <see cref="CliParser"/> for the indicated command with grammar and behaviors.</returns>
public static CliParser GetParser<TDefinition>(CliSettings settings = null)
{
var definitionType = typeof(TDefinition);
return GetParser(definitionType, settings);
}
/// <inheritdoc cref="GetParser{TDefinition}" />
/// <param name="definitionType">The definition class type for the command. A command builder for this class should be automatically generated by the source generator.</param>
public static CliParser GetParser(Type definitionType, CliSettings settings = null)
{
return new CliParser(definitionType, settings);
}
/// <inheritdoc cref="CliParser.GetArgs" />
public static string[] GetArgs()
{
return CliParser.GetArgs();
}
/// <inheritdoc cref="CliParser.Parse(string[])" />
/// <typeparam name="TDefinition"><inheritdoc cref="GetParser{TDefinition}" path="/typeparam[@name='TDefinition']/node()" /></typeparam>
/// <param name="settings"><inheritdoc cref="GetParser{TDefinition}" path="/param[@name='settings']/node()" /></param>
/// <example>
/// <code source="../TestApp/CliExamples.cs" region="CliParseWithResult" language="cs" />
/// </example>
public static CliRunnableResult Parse<TDefinition>(string[] args = null, CliSettings settings = null)
{
var parser = GetParser<TDefinition>(settings);
return parser.Parse(args);
}
/// <inheritdoc cref="CliParser.Parse(string)" />
/// <typeparam name="TDefinition"><inheritdoc cref="GetParser{TDefinition}" path="/typeparam[@name='TDefinition']/node()" /></typeparam>
/// <param name="settings"><inheritdoc cref="GetParser{TDefinition}" path="/param[@name='settings']/node()" /></param>
/// <example>
/// <code source="../TestApp/CliExamples.cs" region="CliParseStringWithResult" language="cs" />
/// </example>
public static CliRunnableResult Parse<TDefinition>(string commandLine, CliSettings settings = null)
{
var parser = GetParser<TDefinition>(settings);
return parser.Parse(commandLine);
}
/// <inheritdoc cref="CliParser.Run(string[])" />
/// <typeparam name="TDefinition"><inheritdoc cref="GetParser{TDefinition}" path="/typeparam[@name='TDefinition']/node()" /></typeparam>
/// <param name="settings"><inheritdoc cref="GetParser{TDefinition}" path="/param[@name='settings']/node()" /></param>
/// <example>
/// <code source="../TestApp/CliExamples.cs" region="CliRun" language="cs" />
/// <code source="../TestApp/CliExamples.cs" region="CliRunWithReturn" language="cs" />
/// </example>
public static int Run<TDefinition>(string[] args = null, CliSettings settings = null)
{
var parser = GetParser<TDefinition>(settings);
return parser.Run(args);
}
/// <inheritdoc cref="CliParser.Run(string)" />
/// <typeparam name="TDefinition"><inheritdoc cref="GetParser{TDefinition}" path="/typeparam[@name='TDefinition']/node()" /></typeparam>
/// <param name="settings"><inheritdoc cref="GetParser{TDefinition}" path="/param[@name='settings']/node()" /></param>
/// <example>
/// <code source="../TestApp/CliExamples.cs" region="CliRunString" language="cs" />
/// <code source="../TestApp/CliExamples.cs" region="CliRunStringWithReturn" language="cs" />
/// </example>
public static int Run<TDefinition>(string commandLine, CliSettings settings = null)
{
var parser = GetParser<TDefinition>(settings);
return parser.Run(commandLine);
}
/// <inheritdoc cref="CliParser.RunAsync(string[], CancellationToken)" />
/// <typeparam name="TDefinition"><inheritdoc cref="GetParser{TDefinition}" path="/typeparam[@name='TDefinition']/node()" /></typeparam>
/// <param name="settings"><inheritdoc cref="GetParser{TDefinition}" path="/param[@name='settings']/node()" /></param>
/// <example>
/// <code source="../TestApp/CliExamples.cs" region="CliRunAsync" language="cs" />
/// <code source="../TestApp/CliExamples.cs" region="CliRunAsyncWithReturn" language="cs" />
/// </example>
public static async Task<int> RunAsync<TDefinition>(string[] args = null, CliSettings settings = null, CancellationToken cancellationToken = default)
{
var parser = GetParser<TDefinition>(settings);
return await parser.RunAsync(args, cancellationToken);
}
/// <inheritdoc cref="CliParser.RunAsync(string, CancellationToken)" />
/// <typeparam name="TDefinition"><inheritdoc cref="GetParser{TDefinition}" path="/typeparam[@name='TDefinition']/node()" /></typeparam>
/// <param name="settings"><inheritdoc cref="GetParser{TDefinition}" path="/param[@name='settings']/node()" /></param>
/// <example>
/// <code source="../TestApp/CliExamples.cs" region="CliRunAsyncString" language="cs" />
/// <code source="../TestApp/CliExamples.cs" region="CliRunAsyncStringWithReturn" language="cs" />
/// </example>
public static async Task<int> RunAsync<TDefinition>(string commandLine, CliSettings settings = null, CancellationToken cancellationToken = default)
{
var parser = GetParser<TDefinition>(settings);
return await parser.RunAsync(commandLine, cancellationToken);
}
/// <summary>
/// Parses the command line arguments and runs the indicated command as delegate.
/// </summary>
/// <param name="cliCommandAsDelegate">
/// The command as delegate.
/// <code>
/// ([CliArgument] string argument1, bool option1) => { }
///
/// ([CliArgument] string argument1, bool option1) => { return 0; }
///
/// async ([CliArgument] string argument1, bool option1) => { await Task.Delay(1000); }
///
/// MethodReference
/// </code>
/// </param>
/// <param name="settings"><inheritdoc cref="GetParser{TDefinition}" path="/param[@name='settings']/node()" /></param>
/// <returns><inheritdoc cref="Run{TDefinition}(string[], CliSettings)" path="/returns/node()" /></returns>
/// <example>
/// <code source="../TestApp/CliExamples.cs" region="CliRunDelegate" language="cs" />
/// <code source="../TestApp/CliExamples.cs" region="CliRunDelegateWithReturn" language="cs" />
/// </example>
public static int Run(Delegate cliCommandAsDelegate, CliSettings settings = null)
{
var definitionType = CliCommandAsDelegate.Get(cliCommandAsDelegate);
var parser = GetParser(definitionType, settings);
return parser.Run();
}
/// <summary>
/// Parses the command line arguments and runs the indicated command as delegate.
/// </summary>
/// <param name="cliCommandAsDelegate"><inheritdoc cref="Run(Delegate, CliSettings)" path="/param[@name='cliCommandAsDelegate']/node()" /></param>
/// <param name="settings"><inheritdoc cref="GetParser{TDefinition}" path="/param[@name='settings']/node()" /></param>
/// <param name="cancellationToken"><inheritdoc cref="RunAsync{TDefinition}(string[], CliSettings, CancellationToken)" path="/param[@name='cancellationToken']/node()" /></param>
/// <returns><inheritdoc cref="Run{TDefinition}(string[], CliSettings)" path="/returns/node()" /></returns>
/// <example>
/// <code source="../TestApp/CliExamples.cs" region="CliRunAsyncDelegate" language="cs" />
/// <code source="../TestApp/CliExamples.cs" region="CliRunAsyncDelegateWithReturn" language="cs" />
/// </example>
public static async Task<int> RunAsync(Delegate cliCommandAsDelegate, CliSettings settings = null, CancellationToken cancellationToken = default)
{
var definitionType = CliCommandAsDelegate.Get(cliCommandAsDelegate);
var parser = GetParser(definitionType, settings);
return await parser.RunAsync(cancellationToken: cancellationToken);
}
}
}