CommandLine NuGet

This commit is contained in:
christianwade 2017-01-08 20:02:38 -08:00
parent 206f4c0bf8
commit 3f310a2402
5 changed files with 140 additions and 47 deletions

View File

@ -0,0 +1,30 @@
using System;
using CommandLine;
using CommandLine.Text;
namespace AsPartitionProcessing.SampleClient
{
class ArgumentOptions
{
[Option('m', "ExecutionMode", HelpText = "Execution mode of SampleClient. Possible values are InitializeInline, InitializeFromDatabase, MergePartitions, DefragPartitionedTables.")]
public string ExecutionMode { get; set; }
[Option('t', "MergeTable", HelpText = "When ExecutionMode=MergePartitions, name of the partitioned table in the tabular model.")]
public string MergeTable { get; set; }
[Option('g', "TargetGranularity", HelpText = "When ExecutionMode=MergePartitions, granularity of the newly created partition. Possible values are Yearly or Monthly.")]
public string TargetGranularity { get; set; }
[Option('k', "MergePartitionKey", HelpText = "When ExecutionMode=MergePartitions, target partition key. If year, follow yyyy; if month follow yyyymm.")]
public string MergePartitionKey { get; set; }
[ParserState]
public IParserState LastParserState { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
}

View File

@ -34,6 +34,10 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="CommandLine, Version=1.9.71.2, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL">
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.AnalysisServices, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL"> <Reference Include="Microsoft.AnalysisServices, Version=13.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\Program Files (x86)\Microsoft SQL Server\130\SDK\Assemblies\Microsoft.AnalysisServices.DLL</HintPath> <HintPath>..\..\..\..\..\..\..\Program Files (x86)\Microsoft SQL Server\130\SDK\Assemblies\Microsoft.AnalysisServices.DLL</HintPath>
@ -56,6 +60,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ArgumentOptions.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Settings.Designer.cs"> <Compile Include="Settings.Designer.cs">
@ -66,6 +71,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
<None Include="packages.config" />
<None Include="Settings.settings"> <None Include="Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator> <Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput> <LastGenOutput>Settings.Designer.cs</LastGenOutput>

View File

@ -5,18 +5,40 @@ using System.Diagnostics;
namespace AsPartitionProcessing.SampleClient namespace AsPartitionProcessing.SampleClient
{ {
enum SampleExecutionMode #region enum ExecutionMode
/// <summary>
/// Execution mode of the SampleClient application.
/// </summary>
enum ExecutionMode
{ {
/// <summary>
/// Initialize configuration inline using sample values.
/// </summary>
InitializeInline, InitializeInline,
/// <summary>
/// Initialize from configuration and logging database.
/// </summary>
InitializeFromDatabase, InitializeFromDatabase,
/// <summary>
/// Merge partitions in a table based on other parameters.
/// </summary>
MergePartitions, MergePartitions,
/// <summary>
/// Defragment partitioned tables in the model. List of partitioned tables defined in the configuration and logging database.
/// </summary>
DefragPartitionedTables DefragPartitionedTables
} }
#endregion
class Program class Program
{ {
//Set sample execution mode here: //Set sample execution mode here:
private static SampleExecutionMode _executionMode = SampleExecutionMode.InitializeInline; private static ExecutionMode _executionMode = ExecutionMode.InitializeInline;
static int Main(string[] args) static int Main(string[] args)
{ {
@ -27,44 +49,16 @@ namespace AsPartitionProcessing.SampleClient
string mergeTable = "Internet Sales"; string mergeTable = "Internet Sales";
Granularity mergeTargetGranuarity = Granularity.Yearly; Granularity mergeTargetGranuarity = Granularity.Yearly;
string mergePartitionKey = "2012"; string mergePartitionKey = "2012";
bool help;
if (args.Length > 0) ParseArgs(args, ref mergeTable, ref mergeTargetGranuarity, ref mergePartitionKey, out help);
{ if (help)
switch (args[0]) return 0; //ERROR_SUCCESS
{
case "InitializeInline":
_executionMode = SampleExecutionMode.InitializeInline;
break;
case "InitializeFromDatabase":
_executionMode = SampleExecutionMode.InitializeFromDatabase;
break;
case "MergePartitions":
_executionMode = SampleExecutionMode.MergePartitions;
if (!(args.Length == 4 && (args[2] == "Yearly" || args[2] == "Monthly")))
{
throw new ArgumentException($"MergePartitions additional arguments not provided or not recognized.");
}
mergeTable = args[1];
mergeTargetGranuarity = args[2] == "Yearly" ? Granularity.Yearly : Granularity.Monthly;
mergePartitionKey = args[3];
break;
case "DefragPartitionedTables":
_executionMode = SampleExecutionMode.DefragPartitionedTables;
break;
default:
throw new ArgumentException($"Command-line argument {args[0]} not recognized.");
//break;
}
}
#endregion #endregion
if (_executionMode == SampleExecutionMode.InitializeInline) if (_executionMode == ExecutionMode.InitializeInline)
{ {
//Perform Processing //Perform Processing
PartitionProcessor.PerformProcessing(InitializeInline(), LogMessage); PartitionProcessor.PerformProcessing(InitializeInline(), LogMessage);
@ -79,17 +73,17 @@ namespace AsPartitionProcessing.SampleClient
switch (_executionMode) switch (_executionMode)
{ {
case SampleExecutionMode.InitializeFromDatabase: case ExecutionMode.InitializeFromDatabase:
//Perform Processing //Perform Processing
PartitionProcessor.PerformProcessing(modelConfig, LogMessage); PartitionProcessor.PerformProcessing(modelConfig, LogMessage);
break; break;
case SampleExecutionMode.MergePartitions: case ExecutionMode.MergePartitions:
//Perform Merging //Perform Merging
PartitionProcessor.MergePartitions(modelConfig, LogMessage, mergeTable, mergeTargetGranuarity, mergePartitionKey); PartitionProcessor.MergePartitions(modelConfig, LogMessage, mergeTable, mergeTargetGranuarity, mergePartitionKey);
break; break;
case SampleExecutionMode.DefragPartitionedTables: case ExecutionMode.DefragPartitionedTables:
//Perform Defrag //Perform Defrag
PartitionProcessor.DefragPartitionedTables(modelConfig, LogMessage); PartitionProcessor.DefragPartitionedTables(modelConfig, LogMessage);
break; break;
@ -104,7 +98,7 @@ namespace AsPartitionProcessing.SampleClient
{ {
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(); Console.WriteLine();
Console.WriteLine(exc.Message, null); Console.WriteLine(exc.Message);
Console.WriteLine(); Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
@ -117,17 +111,76 @@ namespace AsPartitionProcessing.SampleClient
return 1360; //ERROR_GENERIC_NOT_MAPPED return 1360; //ERROR_GENERIC_NOT_MAPPED
} }
} }
finally
{
if (Debugger.IsAttached) if (Debugger.IsAttached)
{ {
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Press any key to exit."); Console.WriteLine("Press any key to exit.");
Console.ReadKey(); Console.ReadKey();
} }
}
return 0; //ERROR_SUCCESS return 0; //ERROR_SUCCESS
} }
private static void ParseArgs(string[] args, ref string mergeTable, ref Granularity mergeTargetGranuarity, ref string mergePartitionKey, out bool help)
{
help = false;
if (args.Length > 0)
{
ArgumentOptions options = new ArgumentOptions();
if (CommandLine.Parser.Default.ParseArguments(args, options))
{
Console.WriteLine($"Argument ExecutionMode: {options.ExecutionMode}");
switch (options.ExecutionMode)
{
case "InitializeInline":
_executionMode = ExecutionMode.InitializeInline;
break;
case "InitializeFromDatabase":
_executionMode = ExecutionMode.InitializeFromDatabase;
break;
case "MergePartitions":
_executionMode = ExecutionMode.MergePartitions;
if (options.MergeTable == null || options.TargetGranularity == null || options.MergePartitionKey == null)
{
throw new ArgumentException($"ExecutionMode MergePartitions additional arguments not provided or not recognized. Requires --MergeTable, --TargetGranularity, --MergePartitionKey.");
}
Console.WriteLine($"Argument MergeTable: {options.MergeTable}");
Console.WriteLine($"Argument TargetGranularity: {options.TargetGranularity}");
Console.WriteLine($"Argument MergePartitionKey: {options.MergePartitionKey}");
mergeTable = options.MergeTable;
mergeTargetGranuarity = options.TargetGranularity == "Yearly" ? Granularity.Yearly : Granularity.Monthly;
mergePartitionKey = options.MergePartitionKey;
break;
case "DefragPartitionedTables":
_executionMode = ExecutionMode.DefragPartitionedTables;
break;
default:
throw new ArgumentException($"Argument --ExecutionMode {options.ExecutionMode} not recognized.");
//break;
}
}
else
{
if (args[0].ToLower() != "--help")
{
throw new ArgumentException($"Arguments provided not recognized.");
}
help = true;
}
}
}
private static ModelConfiguration InitializeInline() private static ModelConfiguration InitializeInline()
{ {
ModelConfiguration partitionedModel = new ModelConfiguration( ModelConfiguration partitionedModel = new ModelConfiguration(
@ -221,7 +274,7 @@ namespace AsPartitionProcessing.SampleClient
try try
{ {
if (!(_executionMode == SampleExecutionMode.InitializeInline)) if (!(_executionMode == ExecutionMode.InitializeInline))
{ {
ConfigDatabaseHelper.LogMessage(message, partitionedModel); ConfigDatabaseHelper.LogMessage(message, partitionedModel);
} }

View File

@ -8,9 +8,9 @@ using System.Runtime.InteropServices;
[assembly: AssemblyTitle("AsPartitionProcessing.SampleClient")] [assembly: AssemblyTitle("AsPartitionProcessing.SampleClient")]
[assembly: AssemblyDescription("")] [assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft Corporation")] [assembly: AssemblyCompany(" ")]
[assembly: AssemblyProduct("AsPartitionProcessing.SampleClient")] [assembly: AssemblyProduct("AsPartitionProcessing.SampleClient")]
[assembly: AssemblyCopyright("Copyright © Microsoft Corporation 2016")] [assembly: AssemblyCopyright(" ")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CommandLineParser" version="1.9.71" targetFramework="net452" />
</packages>