Skip to content

Commit 786e9c0

Browse files
Markus SchmidtMarkus Schmidt
authored andcommitted
CommandRunner Example first version
1 parent 90991e8 commit 786e9c0

15 files changed

Lines changed: 315 additions & 22 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>default</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="MSPro.CLArgs" Version="1.2007.28.2" />
10+
<PackageReference Include="NLog" Version="4.7.2" />
11+
</ItemGroup>
12+
13+
</Project>

CommandRunner/CRun/SayHello/Command.cs renamed to CommandRunner/CLArgs.Command.Command01/SayHelloCommand.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44

55

6-
namespace CLArgs.CommandRunner.SayHello
6+
namespace CLArgs.Command.Command01
77
{
8-
public class Command : CommandBase<CommandParameters>
8+
[Command("SayHello")]
9+
public class SayHelloCommand : CommandBase<SayHelloParameters>
910
{
10-
protected override void Execute(CommandParameters ps)
11+
protected override void Execute(SayHelloParameters ps)
1112
{
1213
for (int i = 0; i < ps.Count; i++)
1314
{

CommandRunner/CRun/SayHello/CommandParameters.cs renamed to CommandRunner/CLArgs.Command.Command01/SayHelloParameters.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33

44

5-
namespace CLArgs.CommandRunner.SayHello
5+
namespace CLArgs.Command.Command01
66
{
7-
public class CommandParameters
7+
public class SayHelloParameters
88
{
99
[OptionDescriptor("Country", Required = true)]
1010
public string Country { get; set; }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>default</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="MSPro.CLArgs" Version="1.2007.28.2" />
10+
<PackageReference Include="NLog" Version="4.7.2" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<Folder Include="XmlToDBCommand" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System.Collections.Generic;
2+
using MSPro.CLArgs;
3+
using NLog;
4+
5+
6+
7+
namespace CLArgs.Command.CommandCollection.JsonToDBCommand
8+
{
9+
[Command("JsonToDatabase")]
10+
class JsonToDbCommand : CommandBase<JsonToDbParameters>
11+
{
12+
private readonly ILogger _log = LogManager.GetCurrentClassLogger();
13+
14+
/// <summary>
15+
/// Do parameter validation.
16+
/// </summary>
17+
/// <param name="parameters"></param>
18+
/// <param name="unresolvedPropertyNames"></param>
19+
/// <param name="errors"></param>
20+
protected override void BeforeExecute(JsonToDbParameters ps, HashSet<string> unresolvedPropertyNames, ErrorDetailList errors)
21+
{
22+
UsernamePassword up = ps.UsernamePassword;
23+
if (up.WindowsAuthentication)
24+
{
25+
if( up.Password!=null || up.UserName!=null)
26+
errors.AddError("UsernamePassword", "You can specify either WindowsAuthentication OR Username/Password.");
27+
}
28+
else // NO Windows Authentication ==> Username AND Password required
29+
{
30+
if( up.UserName==null)
31+
errors.AddError("UsernamePassword", "You must specify a Username.");
32+
if( up.Password==null )
33+
errors.AddError("Password", "You must specify a Password.");
34+
}
35+
36+
//if (errors.HasErrors()) return;
37+
// Command will NOT be executed when errors contains messages
38+
}
39+
40+
41+
42+
/// <summary>
43+
/// Custom error-handler to avoid Aggregate Exception in case of errors.
44+
/// </summary>
45+
/// <param name="errors"></param>
46+
/// <param name="handled"></param>
47+
protected override void OnError(ErrorDetailList errors, bool handled)
48+
{
49+
_log.Error(errors.ToString);
50+
handled = true;
51+
}
52+
53+
54+
55+
/// <summary>
56+
/// Execute Commands Functionality
57+
/// </summary>
58+
/// <param name="ps"></param>
59+
protected override void Execute(JsonToDbParameters ps)
60+
{
61+
_log.Info($">><Execute ENTER");
62+
// do something
63+
_log.Info($"Username={ps.UsernamePassword.UserName}");
64+
_log.Info($"Password={ps.UsernamePassword.Password}");
65+
_log.Info($"WindowsAuthentication={ps.UsernamePassword.WindowsAuthentication}");
66+
_log.Info($"Filename={ps.Filename}");
67+
_log.Info($"MaxItems={ps.MaxItems}");
68+
69+
_log.Info($"<<<Finished EXIT");
70+
}
71+
}
72+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using MSPro.CLArgs;
2+
3+
4+
5+
namespace CLArgs.Command.CommandCollection.JsonToDBCommand
6+
{
7+
internal class JsonToDbParameters
8+
{
9+
[OptionSet]
10+
public UsernamePassword UsernamePassword { get; set; }
11+
12+
[OptionDescriptor("Filename", "f", Required = true)]
13+
public string Filename { get; set; }
14+
15+
[OptionDescriptor("MaxItems", "m", Default = 10)]
16+
public int MaxItems { get; set; }
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using MSPro.CLArgs;
2+
3+
4+
5+
namespace CLArgs.Command.CommandCollection
6+
{
7+
/// <summary>
8+
/// Reusable Username / Password class for many Parameters
9+
/// </summary>
10+
internal class UsernamePassword
11+
{
12+
[OptionDescriptor("UserName", "u")]
13+
public string UserName { get; set; }
14+
15+
[OptionDescriptor("Password", "p")]
16+
public string Password { get; set; }
17+
18+
[OptionDescriptor("WindowsAuthentication", "ad")]
19+
public bool WindowsAuthentication { get; set; }
20+
}
21+
}

CommandRunner/CLArgs.CommandRunner.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CLArgs.CommandRunner", "CRu
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MSPro.CLArgs", "..\src\MSPro.CLArgs\MSPro.CLArgs.csproj", "{DAAA78A8-D9A5-4C01-B658-B77CF6FD034E}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLArgs.Command.Command01", "CLArgs.Command.Command01\CLArgs.Command.Command01.csproj", "{C4D4339D-B24E-4C95-AC7C-A914554FCAAE}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLArgs.Command.CommandCollection", "CLArgs.Command.CommandCollection\CLArgs.Command.CommandCollection.csproj", "{F39AB13F-FB8C-4A4F-9057-62FA3ED6E609}"
13+
EndProject
1014
Global
1115
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1216
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +25,14 @@ Global
2125
{DAAA78A8-D9A5-4C01-B658-B77CF6FD034E}.Debug|Any CPU.Build.0 = Debug|Any CPU
2226
{DAAA78A8-D9A5-4C01-B658-B77CF6FD034E}.Release|Any CPU.ActiveCfg = Release|Any CPU
2327
{DAAA78A8-D9A5-4C01-B658-B77CF6FD034E}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{C4D4339D-B24E-4C95-AC7C-A914554FCAAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{C4D4339D-B24E-4C95-AC7C-A914554FCAAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{C4D4339D-B24E-4C95-AC7C-A914554FCAAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{C4D4339D-B24E-4C95-AC7C-A914554FCAAE}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{F39AB13F-FB8C-4A4F-9057-62FA3ED6E609}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{F39AB13F-FB8C-4A4F-9057-62FA3ED6E609}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{F39AB13F-FB8C-4A4F-9057-62FA3ED6E609}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{F39AB13F-FB8C-4A4F-9057-62FA3ED6E609}.Release|Any CPU.Build.0 = Release|Any CPU
2436
EndGlobalSection
2537
GlobalSection(SolutionProperties) = preSolution
2638
HideSolutionNode = FALSE

CommandRunner/CRun/AssemblyInfo.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Reflection;
4+
5+
6+
7+
namespace CLArgs.CommandRunner
8+
{
9+
internal static class AssemblyInfo
10+
{
11+
public new static string ToString()
12+
{
13+
var assemblyName = Assembly.GetExecutingAssembly().GetName();
14+
Debug.Assert(assemblyName.Version != null, "assemblyName.Version != null");
15+
var buildDate = new DateTime(2000, 1, 1)
16+
.AddDays(assemblyName.Version.Build).AddSeconds(assemblyName.Version.Revision * 2);
17+
return
18+
$"{assemblyName.Name} v{assemblyName.Version} Isx64={Environment.Is64BitProcess} Build={BUILD}, {buildDate.ToUniversalTime()}";
19+
}
20+
21+
22+
23+
#if DEBUG
24+
private const string BUILD = "DEBUG";
25+
public const string ENVIRONMENT_NAME = "Development";
26+
#else
27+
private const string BUILD = "RELEASE";
28+
public const string ENVIRONMENT_NAME = "Production";
29+
#endif
30+
}
31+
}

CommandRunner/CRun/CLArgs.CommandRunner.csproj

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
66
<RootNamespace>CLArgs.CommandRunner</RootNamespace>
77
<AssemblyName>CLArgs.CommandRunner</AssemblyName>
8+
<LangVersion>default</LangVersion>
89
</PropertyGroup>
910

1011
<ItemGroup>
11-
<PackageReference Include="MSPro.CLArgs" Version="1.2007.26" />
12+
<PackageReference Include="MSPro.CLArgs" Version="1.2007.28.2" />
13+
<PackageReference Include="NLog" Version="4.7.2" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<None Remove="nlog.config" />
18+
<Content Include="nlog.config">
19+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
20+
</Content>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\CLArgs.Command.Command01\CLArgs.Command.Command01.csproj" />
25+
<ProjectReference Include="..\CLArgs.Command.CommandCollection\CLArgs.Command.CommandCollection.csproj" />
1226
</ItemGroup>
1327

1428
</Project>

0 commit comments

Comments
 (0)