Skip to content

Commit c35aea3

Browse files
committed
Version 2.6.2
- Changed: Renamed `Cli.GetConfiguration()` to `Cli.GetParser()` and changed return type from `CommandLineConfiguration` to new class `CliParser`. This method returns a CLI parser configured for the indicated command and you can call `Parse` or `Run` methods on it. So you can reuse the same `CliParser` for parsing and running. ```c# var parser = Cli.GetParser<RootCliCommand>(); var result = parser.Parse(args); parser.Run(args); ``` Changed return type of `Cli.Parse` methods from `ParseResult` to new class `CliResult` which describes the results of parsing a command line input based on a specific parser configuration and provides methods for binding the result to definition classes. ```c# var result = Cli.Parse<RootCliCommand>(args); var rootCliCommand = result.Bind<RootCliCommand>(); if (result.ParseResult.Errors.Count > 0) { } ``` - Improved: More robust command binding. Introduced `CliBindingContext` for internally encapsulating binders per command build and configuration. For example, in previous versions, the below code worked incorrectly, i.e. second call to `Cli.Parse` with same definition type, overwrote the binder so the later `Bind` call did not work correctly. ```c# var result = Cli.Parse<RootCliCommand>(args); var result2 = Cli.Parse<RootCliCommand>(args); var rootCommand = result.Bind<RootCliCommand>(); ```
1 parent ef6bcba commit c35aea3

File tree

125 files changed

+1839
-1725
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+1839
-1725
lines changed

docs/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ Cli.Run<RootCliCommand>(args, new CliSettings { EnableDefaultExceptionHandler =
112112
```
113113
If you need to simply parse the command-line arguments without invocation, use this:
114114
```c#
115-
var parseResult = Cli.Parse<RootCliCommand>(args);
116-
var rootCliCommand = parseResult.Bind<RootCliCommand>();
115+
var result = Cli.Parse<RootCliCommand>(args);
116+
var rootCliCommand = result.Bind<RootCliCommand>();
117117
```
118118
If you need to examine the parse result, such as errors:
119119
```c#
120-
var parseResult = Cli.Parse<RootCliCommand>(args);
121-
if (parseResult.Errors.Count > 0)
120+
var result = Cli.Parse<RootCliCommand>(args);
121+
if (result.ParseResult.Errors.Count > 0)
122122
{
123123

124124
}

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<!-- https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props#generateassemblyinfo -->
5-
<VersionPrefix>2.6.0</VersionPrefix>
5+
<VersionPrefix>2.6.2</VersionPrefix>
66
<Product>DotMake Command-Line</Product>
77
<Company>DotMake</Company>
88
<!-- Copyright is also used for NuGet metadata -->

src/DotMake.CommandLine.SourceGeneration/Outputs/CliArgumentOutput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void AppendCSharpCreateString(CodeStringBuilder sb, string varName, strin
114114

115115
if (Input.Parent.HasGetCompletionsInterface)
116116
//sb.AppendLine($"{varDefaultClass}.AddCompletions(\"{Input.Symbol.Name}\", {varName}.CompletionSources);");
117-
sb.AppendLine($"{varName}.CompletionSources.Add(completionContext => GetCompletions(\"{Input.Symbol.Name}\", completionContext));");
117+
sb.AppendLine($"{varName}.CompletionSources.Add(completionContext => GetCompletions(\"{Input.Symbol.Name}\", bindingContext, completionContext));");
118118

119119
}
120120
}

src/DotMake.CommandLine.SourceGeneration/Outputs/CliCommandOutput.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ Instead we will use Bind method to get cached definition instance and call GetCo
151151
if (ReferenceDependantInput.HasMsDependencyInjectionAbstractions || ReferenceDependantInput.HasMsDependencyInjection)
152152
{
153153
sb.AppendLine(ReferenceDependantInput.HasMsDependencyInjection
154-
? "ServiceProvider = DotMake.CommandLine.CliServiceCollectionExtensions.GetServiceProviderOrDefault(null);"
155-
: "ServiceProvider = DotMake.CommandLine.CliServiceProviderExtensions.GetServiceProvider(null);");
156-
sb.AppendLine("if (ServiceProvider != null)");
154+
? "var serviceProvider = DotMake.CommandLine.CliServiceCollectionExtensions.GetServiceProviderOrDefault(null);"
155+
: "var serviceProvider = DotMake.CommandLine.CliServiceProviderExtensions.GetServiceProvider(null);");
156+
sb.AppendLine("if (serviceProvider != null)");
157157
sb.AppendIndent();
158158
sb.AppendLine("return Microsoft.Extensions.DependencyInjection.ActivatorUtilities");
159159
sb.AppendIndent();
160160
sb.AppendIndent();
161-
sb.AppendLine($".CreateInstance<{definitionClass}>(ServiceProvider);");
162-
//in case ServiceProvider is null (i.e. not set with SetServiceProvider)
161+
sb.AppendLine($".CreateInstance<{definitionClass}>(serviceProvider);");
162+
//in case serviceProvider is null (i.e. not set with SetServiceProvider)
163163
//call Activator.CreateInstance which will throw exception if class has no default constructor
164164
//but at least it avoids compile time error in generated code with new()
165165
sb.AppendLine();
@@ -177,22 +177,22 @@ Instead we will use Bind method to get cached definition instance and call GetCo
177177
sb.AppendLine();
178178
sb.AppendLine($"private System.Collections.Generic.IEnumerable<{CompletionsNamespace}.CompletionItem> GetCompletions(");
179179
sb.AppendIndent();
180-
sb.AppendLine($"string propertyName, {CompletionsNamespace}.CompletionContext completionContext)");
180+
sb.AppendLine($"string propertyName, DotMake.CommandLine.CliBindingContext bindingContext, {CompletionsNamespace}.CompletionContext completionContext)");
181181
using (sb.AppendBlockStart())
182182
{
183183
var varTargetClass = "targetClass";
184184

185-
sb.AppendLine($"var {varTargetClass} = ({definitionClass}) Bind(completionContext.ParseResult);");
185+
sb.AppendLine($"var {varTargetClass} = bindingContext.Bind<{definitionClass}>(completionContext.ParseResult);");
186186
sb.AppendLine();
187187

188-
sb.AppendLine("// Call the interface method with property name of option or argument");
188+
sb.AppendLine("// Call the interface method with property name of option or argument");
189189
sb.AppendLine($"return {varTargetClass}.GetCompletions(propertyName, completionContext);");
190190
}
191191
sb.AppendLine();
192192
}
193193

194194
sb.AppendLine("/// <inheritdoc />");
195-
using (sb.AppendBlockStart($"protected override {CommandClassNamespace}.{CommandClassName} DoBuild()"))
195+
using (sb.AppendBlockStart($"protected override {CommandClassNamespace}.{CommandClassName} DoBuild(DotMake.CommandLine.CliBindingContext bindingContext)"))
196196
{
197197
var varNamer = "Namer";
198198
var varCommand = "command";
@@ -242,7 +242,7 @@ Instead we will use Bind method to get cached definition instance and call GetCo
242242
}
243243

244244
/*
245-
From now on, we will handle this in Cli.GetConfiguration where Build() is called and command is created.
245+
From now on, we will handle this in Cli.GetParser() where Build() is called and command is created.
246246
We don't want it to be recursive here, because we will also create the parents.
247247
248248
sb.AppendLine();
@@ -255,23 +255,21 @@ Instead we will use Bind method to get cached definition instance and call GetCo
255255

256256
sb.AppendLine();
257257
var varParseResult = "parseResult";
258-
using (sb.AppendBlockStart($"Binder = ({varParseResult}) =>", ";"))
258+
using (sb.AppendBlockStart($"bindingContext.Binders[DefinitionType] = ({varParseResult}) =>", ";"))
259259
{
260260
var varTargetClass = "targetClass";
261261

262262
sb.AppendLine($"var {varTargetClass} = CreateInstance();");
263-
263+
264264
sb.AppendLine();
265-
sb.AppendLine("// Set the values for the parent command accessors");
265+
sb.AppendLine("// Set the values for the parent command accessors");
266266
foreach (var cliParentCommandAccessorInfo in parentCommandAccessorsWithoutProblem)
267267
{
268-
sb.AppendLine($"{varTargetClass}.{cliParentCommandAccessorInfo.Symbol.Name} = DotMake.CommandLine.ParseResultExtensions");
269-
sb.AppendIndent();
270-
sb.AppendLine($".Bind<{cliParentCommandAccessorInfo.Symbol.Type.ToReferenceString()}>({varParseResult});");
268+
sb.AppendLine($"{varTargetClass}.{cliParentCommandAccessorInfo.Symbol.Name} = bindingContext.Bind<{cliParentCommandAccessorInfo.Symbol.Type.ToReferenceString()}>({varParseResult});");
271269
}
272-
270+
273271
sb.AppendLine();
274-
sb.AppendLine("// Set the parsed or default values for the directives");
272+
sb.AppendLine("// Set the parsed or default values for the directives");
275273
for (var index = 0; index < directivesWithoutProblem.Length; index++)
276274
{
277275
var cliDirectiveInput = directivesWithoutProblem[index];
@@ -280,7 +278,7 @@ Instead we will use Bind method to get cached definition instance and call GetCo
280278
}
281279

282280
sb.AppendLine();
283-
sb.AppendLine("// Set the parsed or default values for the options");
281+
sb.AppendLine("// Set the parsed or default values for the options");
284282
for (var index = 0; index < optionsWithoutProblem.Length; index++)
285283
{
286284
var cliOptionInfo = optionsWithoutProblem[index];
@@ -289,7 +287,7 @@ Instead we will use Bind method to get cached definition instance and call GetCo
289287
}
290288

291289
sb.AppendLine();
292-
sb.AppendLine("// Set the parsed or default values for the arguments");
290+
sb.AppendLine("// Set the parsed or default values for the arguments");
293291
for (var index = 0; index < argumentsWithoutProblem.Length; index++)
294292
{
295293
var cliArgumentInfo = argumentsWithoutProblem[index];
@@ -312,13 +310,13 @@ Instead we will use Bind method to get cached definition instance and call GetCo
312310
{
313311
var varTargetClass = "targetClass";
314312

315-
sb.AppendLine($"var {varTargetClass} = ({definitionClass}) Bind({varParseResult});");
313+
sb.AppendLine($"var {varTargetClass} = bindingContext.Bind<{definitionClass}>({varParseResult});");
316314
sb.AppendLine();
317315

318-
sb.AppendLine("// Call the command handler");
316+
sb.AppendLine("// Call the command handler");
319317
sb.AppendLine(isAsync
320-
? $"var {varCliContext} = new DotMake.CommandLine.CliContext({varParseResult}, {varCancellationToken});"
321-
: $"var {varCliContext} = new DotMake.CommandLine.CliContext({varParseResult});");
318+
? $"var {varCliContext} = new DotMake.CommandLine.CliContext(bindingContext, {varParseResult}, {varCancellationToken});"
319+
: $"var {varCliContext} = new DotMake.CommandLine.CliContext(bindingContext, {varParseResult});");
322320
sb.AppendLine("var exitCode = 0;");
323321
if (handlerWithoutProblem != null)
324322
{

src/DotMake.CommandLine.SourceGeneration/Outputs/CliOptionOutput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void AppendCSharpCreateString(CodeStringBuilder sb, string varName, strin
122122

123123
if (Input.Parent.HasGetCompletionsInterface)
124124
//sb.AppendLine($"{varDefaultClass}.AddCompletions(\"{Input.Symbol.Name}\", {varName}.CompletionSources);");
125-
sb.AppendLine($"{varName}.CompletionSources.Add(completionContext => GetCompletions(\"{Input.Symbol.Name}\", completionContext));");
125+
sb.AppendLine($"{varName}.CompletionSources.Add(completionContext => GetCompletions(\"{Input.Symbol.Name}\", bindingContext, completionContext));");
126126
}
127127
}
128128
}

0 commit comments

Comments
 (0)