Skip to content

Commit 37c4f46

Browse files
Merge pull request #90 from ProfessionalCSharp/commandlinebeta2
command line beta 2 updates #89
2 parents 999c71a + 6d9bc4c commit 37c4f46

File tree

11 files changed

+69
-61
lines changed

11 files changed

+69
-61
lines changed

1_CS/LINQ/EnumerableSample/EnumerableSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.21308.1" />
11+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta2.21617.1" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

1_CS/LINQ/EnumerableSample/RegisterCommands.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ public static void Register(RootCommand rootCommand, string commandText, string
1515

1616
foreach (var method in methods)
1717
{
18-
command.AddCommand(new(method.Name.ToLower())
18+
Command featureCommand = new(method.Name.ToLower());
19+
featureCommand.SetHandler(() =>
1920
{
20-
Handler = CommandHandler.Create(() =>
21-
{
22-
MethodInfo m = method;
23-
m.Invoke(null, null);
24-
})
21+
MethodInfo m = method;
22+
m.Invoke(null, null);
2523
});
24+
command.AddCommand(featureCommand);
2625
}
2726

2827
rootCommand.AddCommand(command);

2_Libs/FilesAndStreams/FilesAndFolders/FilesAndFolders.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.21308.1" />
11+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta2.21617.1" />
1212
</ItemGroup>
1313

1414
</Project>

2_Libs/FilesAndStreams/FilesAndFolders/Program.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,34 @@ CommandLineBuilder BuildCommandLine()
1515
IsRequired = true
1616
};
1717
RootCommand rootCommand = new("FilesAndFolders");
18-
Command showDrivesCommand = new("showdrives") { Handler = CommandHandler.Create(ShowDrivesInformation) };
19-
Command specialFoldersCommand = new("specialfolders") { Handler = CommandHandler.Create(ShowSpecialFolders) };
18+
Command showDrivesCommand = new("showdrives");
19+
showDrivesCommand.SetHandler(ShowDrivesInformation);
20+
Command specialFoldersCommand = new("specialfolders");
21+
specialFoldersCommand.SetHandler(ShowSpecialFolders);
2022
Command createFileCommand = new("createfile") { fileOption };
21-
createFileCommand.Handler = CommandHandler.Create<string>(CreateFile);
23+
createFileCommand.SetHandler<string>(CreateFile, fileOption);
2224
Command fileInfoCommand = new("fileinfo") { fileOption };
23-
fileInfoCommand.Handler = CommandHandler.Create<string>(FileInformation);
25+
fileInfoCommand.SetHandler<string>(FileInformation, fileOption);
2426
Command changePropertiesCommand = new("changeprops") { fileOption };
25-
changePropertiesCommand.Handler = CommandHandler.Create<string>(ChangeFileProperties);
27+
changePropertiesCommand.SetHandler<string>(ChangeFileProperties, fileOption);
2628
Command readLinesCommand = new("readlines") { fileOption };
27-
readLinesCommand.Handler = CommandHandler.Create<string>(ReadLineByLine);
28-
Command writeFileCommand = new("writefile") { Handler = CommandHandler.Create(WriteAFile) };
29+
readLinesCommand.SetHandler<string>(ReadLineByLine, fileOption);
30+
Command writeFileCommand = new("writefile");
31+
writeFileCommand.SetHandler(WriteAFile);
32+
Option<string> directoryOption = new("--dir")
33+
{
34+
IsRequired = true
35+
};
36+
Option<bool> checkOnlyOption = new("--checkOnly")
37+
{
38+
IsRequired = false
39+
};
2940
Command deleteDuplicateFilesCommand = new("deleteduplicate")
3041
{
31-
new Option<string>("--dir")
32-
{
33-
IsRequired = true
34-
},
35-
new Option<bool>("--checkOnly")
36-
{
37-
IsRequired = false
38-
}
42+
directoryOption,
43+
checkOnlyOption
3944
};
40-
deleteDuplicateFilesCommand.Handler = CommandHandler.Create<string, bool?>((dir, checkOnly) => DeleteDuplicateFiles(dir, checkOnly ?? true));
45+
deleteDuplicateFilesCommand.SetHandler<string, bool?>((dir, checkOnly) => DeleteDuplicateFiles(dir, checkOnly ?? true), directoryOption, checkOnlyOption);
4146

4247
rootCommand.AddCommand(showDrivesCommand);
4348
rootCommand.AddCommand(specialFoldersCommand);

2_Libs/FilesAndStreams/StreamSamples/Program.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,24 @@ CommandLineBuilder BuildCommandLine()
2424
outputFileOption.AddAlias("-o");
2525
RootCommand rootCommand = new("WorkingWithFilesAndDirectories");
2626
Command readWithStreamCommand = new("readwithstream") { fileOption };
27-
readWithStreamCommand.Handler = CommandHandler.Create<string>(ReadUsingFileStream);
28-
Command writeTextFileCommand = new("writetext") { Handler = CommandHandler.Create(WriteTextFile) };
27+
readWithStreamCommand.SetHandler<string>(file => ReadUsingFileStream(file), inputFileOption);
28+
Command writeTextFileCommand = new("writetext");
29+
writeTextFileCommand.SetHandler(WriteTextFile);
2930
Command copyCommand = new("copy") { inputFileOption, outputFileOption };
30-
copyCommand.Handler = CommandHandler.Create<string, string>(CopyUsingStreams);
31+
copyCommand.SetHandler<string, string>((inputFile, ouptutFile) => CopyUsingStreams(inputFile, ouptutFile), inputFileOption, outputFileOption);
3132
Command copy2Command = new("copy2") { inputFileOption, outputFileOption };
32-
copy2Command.Handler = CommandHandler.Create<string, string>(CopyUsingStreams2);
33+
copy2Command.SetHandler<string, string>((inputFile, outputFile) => CopyUsingStreams2(inputFile, outputFile), inputFileOption, outputFileOption);
34+
Option<int> countOption = new("--count")
35+
{
36+
IsRequired = true
37+
};
3338
Command createSampleFile = new("samplefile")
3439
{
35-
new Option<int>("--count")
36-
{
37-
IsRequired = true
38-
}
40+
countOption
3941
};
40-
createSampleFile.Handler = CommandHandler.Create<int>(CreateSampleFileAsync);
41-
Command randomAccessCommand = new("random") { Handler = CommandHandler.Create(RandomAccessSampleAsync) };
42+
createSampleFile.SetHandler<int>(async count => await CreateSampleFileAsync(count), countOption); ;
43+
Command randomAccessCommand = new("random");
44+
randomAccessCommand.SetHandler(RandomAccessSampleAsync);
4245

4346
rootCommand.AddCommand(readWithStreamCommand);
4447
rootCommand.AddCommand(writeTextFileCommand);

2_Libs/FilesAndStreams/StreamSamples/StreamSamples.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="System.CommandLine.Hosting" Version="0.3.0-alpha.21216.1" />
11+
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.21617.1" />
1212
</ItemGroup>
1313

1414
</Project>

2_Libs/Networking/HttpClientSample/HttpClientSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0-rc.2.21480.5" />
1212
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0-rc.2.21480.5" />
1313
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.0-rc.2.21480.10" />
14-
<PackageReference Include="System.CommandLine.Hosting" Version="0.3.0-alpha.21216.1" />
14+
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.21617.1" />
1515
</ItemGroup>
1616

1717
<ItemGroup>

2_Libs/Networking/HttpClientSample/Program.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,67 +59,66 @@ CommandLineBuilder BuildCommandLine()
5959
{
6060
RootCommand rootCommand = new("HttpClientSample");
6161
Command simpleCommand = new("simple");
62-
simpleCommand.Handler = CommandHandler.Create<IHost>(async (host) =>
62+
simpleCommand.SetHandler<IHost>(async host =>
6363
{
6464
var service = host.Services.GetRequiredService<HttpClientSamples>();
6565
await service.SimpleGetRequestAsync();
6666
});
6767
rootCommand.AddCommand(simpleCommand);
6868

6969
Command httpRequestMessageCommand = new("httprequest");
70-
httpRequestMessageCommand.Handler = CommandHandler.Create<IHost>(async (host) =>
70+
httpRequestMessageCommand.SetHandler<IHost>(async host =>
7171
{
7272
var service = host.Services.GetRequiredService<HttpClientSamples>();
7373
await service.UseHttpRequestMessageAsync();
7474
});
7575
rootCommand.AddCommand(httpRequestMessageCommand);
7676

7777
Command exceptionCommand = new("exception");
78-
exceptionCommand.Handler = CommandHandler.Create<IHost>(async (host) =>
78+
exceptionCommand.SetHandler<IHost>(async host =>
7979
{
8080
var service = host.Services.GetRequiredService<HttpClientSamples>();
8181
await service.ThrowExceptionAsync();
8282
});
8383
rootCommand.AddCommand(exceptionCommand);
8484

8585
Command headerCommand = new("headers");
86-
headerCommand.Handler = CommandHandler.Create<IHost>(async (host) =>
86+
headerCommand.SetHandler<IHost>(async host =>
8787
{
8888
var service = host.Services.GetRequiredService<HttpClientSamples>();
8989
await service.AddHttpHeadersAsync();
9090
});
9191
rootCommand.AddCommand(headerCommand);
9292

9393
Command http2Command = new("http2");
94-
http2Command.Handler = CommandHandler.Create<IHost>(async (host) =>
94+
http2Command.SetHandler<IHost>(async host =>
9595
{
9696
var service = host.Services.GetRequiredService<HttpClientSamples>();
9797
await service.UseHttp2();
9898
});
9999
rootCommand.AddCommand(http2Command);
100100

101101
Command messageHandlerCommand = new("messagehandler");
102-
messageHandlerCommand.Handler = CommandHandler.Create<IHost>(async (host) =>
102+
messageHandlerCommand.SetHandler<IHost>(async host =>
103103
{
104104
var service = host.Services.GetRequiredService<HttpClientSampleWithMessageHandler>();
105105
for (int i = 0; i < 10; i++)
106106
{
107107
await service.UseMessageHandlerAsync();
108108
}
109-
110109
});
111110
rootCommand.AddCommand(messageHandlerCommand);
112111

113112
Command namedClientCommand = new("named");
114-
namedClientCommand.Handler = CommandHandler.Create<IHost>(async host =>
113+
namedClientCommand.SetHandler<IHost>(async host =>
115114
{
116115
var service = host.Services.GetRequiredService<NamedClientSample>();
117116
await service.RunAsync();
118117
});
119118
rootCommand.AddCommand(namedClientCommand);
120119

121120
Command pollyCommand = new("retry");
122-
pollyCommand.Handler = CommandHandler.Create<IHost>(async host =>
121+
pollyCommand.SetHandler<IHost>(async host =>
123122
{
124123
var service = host.Services.GetRequiredService<FaultHandlingSample>();
125124
await service.RunAsync();

2_Libs/Networking/HttpClientSample/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"HttpClient": {
3-
"Url": "https://localhost:5021",
4-
"InvalidUrl": "https://localhost1:5021"
3+
"Url": "https://localhost:5001",
4+
"InvalidUrl": "https://localhost1:5001"
55
},
66
"RateLimit": {
77
"LimitCalls": 5

2_Libs/Networking/Utilities/Program.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,28 @@ await BuildCommandLine()
1212
CommandLineBuilder BuildCommandLine()
1313
{
1414
RootCommand rootCommand = new("Utilities");
15+
Option<string> uriOption = new("--uri")
16+
{
17+
IsRequired = true
18+
};
1519
Command uriCommand = new("uri", "Show the parts of the URI, e.g. www.wrox.com")
1620
{
17-
new Option<string>("--uri")
18-
{
19-
IsRequired = true
20-
}
21+
uriOption
2122
};
22-
uriCommand.Handler = CommandHandler.Create<string>(UriSample);
23-
Command ipCommand = new("ip", "Show the part of the IP address, e.g. ipaddress 234.56.78.9")
23+
uriCommand.SetHandler<string>(UriSample, uriOption);
24+
25+
Option<string> ipOption = new("--ip")
2426
{
25-
new Option<string>("--ip")
26-
{
27-
IsRequired = true
28-
}
27+
IsRequired = true
2928
};
30-
ipCommand.Handler = CommandHandler.Create<string>(ip => IPAddressSample(ip));
31-
Command buildUriCommand = new("builduri", "Build an URI using the UriBuilder")
29+
Command ipCommand = new("ip", "Show the part of the IP address, e.g. ipaddress 234.56.78.9")
3230
{
33-
Handler = CommandHandler.Create(BuildUri)
31+
ipOption
3432
};
33+
ipCommand.SetHandler<string>(ip => IPAddressSample(ip), ipOption);
34+
35+
Command buildUriCommand = new("builduri", "Build an URI using the UriBuilder");
36+
buildUriCommand.SetHandler(BuildUri);
3537

3638
rootCommand.AddCommand(uriCommand);
3739
rootCommand.AddCommand(ipCommand);

2_Libs/Networking/Utilities/Utilities.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="System.CommandLine.Hosting" Version="0.3.0-alpha.21216.1" />
11+
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.21617.1" />
1212
</ItemGroup>
1313

1414
</Project>

0 commit comments

Comments
 (0)