Skip to content

Commit

Permalink
Updates CLI lib
Browse files Browse the repository at this point in the history
We were using a custom build of the new CLI lib. The lib has been updated with the missing features we need so this commit updates to the official build.

This is a checkpoint commit. We found a bug while making this change and we're waiting for that bug to be merged in.
  • Loading branch information
atruskie committed Mar 29, 2018
1 parent 870c09f commit fb7ed6d
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 38 deletions.
1 change: 0 additions & 1 deletion NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<packageSources>
<add key="All" value="(Aggregate source)" />
<add key="SqLiteFileSystem Packages" value="https://ci.appveyor.com/nuget/sqlitefilesystem-dcfwbf0p9du0" />
<add key="atruskie Packages" value="https://www.myget.org/F/atruskie" />
<add key="natemcmaster Packages" value="https://www.myget.org/F/natemcmaster" />
</packageSources>
</configuration>
2 changes: 1 addition & 1 deletion src/AnalysisPrograms/AnalysisPrograms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<HintPath>..\..\packages\MathNet.Numerics.3.20.2\lib\net40\MathNet.Numerics.dll</HintPath>
</Reference>
<Reference Include="McMaster.Extensions.CommandLineUtils, Version=2.2.0.0, Culture=neutral, PublicKeyToken=6f71cb76b82f055d, processorArchitecture=MSIL">
<HintPath>..\..\packages\McMaster.Extensions.CommandLineUtils.1.0.0-CI00005\lib\net45\McMaster.Extensions.CommandLineUtils.dll</HintPath>
<HintPath>..\..\packages\McMaster.Extensions.CommandLineUtils.2.2.0-rc\lib\net45\McMaster.Extensions.CommandLineUtils.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Data.Sqlite, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
Expand Down
26 changes: 14 additions & 12 deletions src/AnalysisPrograms/MainEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace AnalysisPrograms
using McMaster.Extensions.CommandLineUtils.Abstractions;
using Production;
using Production.Arguments;
using static System.Environment;

/// <summary>
/// Main Entry for Analysis Programs.
Expand All @@ -30,29 +31,30 @@ public static partial class MainEntry
public static async Task<int> Main(string[] args)
{
// HACK: Disable the following two line when argument refactoring is done
//var options = DebugOptions.Yes;
//AttachDebugger(ref options);
var options = DebugOptions.Yes;
AttachDebugger(ref options);

ParseEnvirionemnt();

Copyright();

AttachExceptionHandler();

NoConsole.Log.Info("Executable called with these arguments: {1}{0}{1}".Format2(Environment.CommandLine, Environment.NewLine));
NoConsole.Log.Info($"Executable called with these arguments: {NewLine}{CommandLine}{NewLine}");

// Note: See MainEntry.BeforeExecute for commands run before invocation.
// note: Exception handling can be found in CurrentDomainOnUnhandledException
var console = PhysicalConsoleLogger.Default;
var helpText = CustomHelpTextGenerator.Singleton;
helpText.EnvironmentOptions = EnvironmentOptions;
var app = new CommandLineApplication<MainArgs>(console);

ValueParserProvider.Default.AddParser(typeof(DateTimeOffset), new DateTimeOffsetParser());
ValueParserProvider.Default.AddParser(typeof(TimeSpan), new TimeSpanParser());
ValueParserProvider.Default.AddParser(typeof(DirectoryInfo), new DirectoryInfoParser());
ValueParserProvider.Default.AddParser(typeof(FileInfo), new FileInfoParser());
app.HelpTextGenerator = new CustomHelpTextGenerator { EnvironmentOptions = EnvironmentOptions };
app.ValueParsers.Add(new DateTimeOffsetParser());
app.ValueParsers.Add(new TimeSpanParser());
app.ValueParsers.Add(new FileInfoParser());
app.ValueParsers.Add(new DirectoryInfoParser());
app.Conventions.UseAttributes();

// Note: See MainEntry.BeforeExecute for commands run before invocation.
// note: Exception handling can be found in CurrentDomainOnUnhandledException
var result = await CommandLineApplication.ExecuteAsync<MainArgs>(console, args);
var result = await Task.FromResult(app.Execute(args));

LogProgramStats();

Expand Down
7 changes: 1 addition & 6 deletions src/AnalysisPrograms/Production/CustomHelpTextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ namespace AnalysisPrograms.Production
/// </summary>
public class CustomHelpTextGenerator : IHelpTextGenerator
{
/// <summary>
/// Gets a singleton instance of <see cref="CustomHelpTextGenerator" />.
/// </summary>
public static CustomHelpTextGenerator Singleton { get; } = new CustomHelpTextGenerator();

private CustomHelpTextGenerator()
public CustomHelpTextGenerator()
{
}

Expand Down
7 changes: 2 additions & 5 deletions src/AnalysisPrograms/Production/MainEntryUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,7 @@ internal enum Usages

internal static void PrintUsage(string message, Usages usageStyle, string commandName = null)
{
//Contract.Requires(usageStyle != Usages.Single || commandName != null);q
// HACK: override help text generator here because we can't change it in MainEntry -- yet
// https://github.com/natemcmaster/CommandLineUtils/issues/52
CommandLineApplication.HelpTextGenerator = CustomHelpTextGenerator.Singleton;
//Contract.Requires(usageStyle != Usages.Single || commandName != null);

var root = CommandLineApplication.Root();

Expand Down Expand Up @@ -217,7 +214,7 @@ internal static void PrintUsage(string message, Usages usageStyle, string comman

using (var sb = new StringWriter())
{
CustomHelpTextGenerator.Singleton.FormatCommands(sb, commands);
((CustomHelpTextGenerator)CommandLineApplication.HelpTextGenerator).FormatCommands(sb, commands);

LoggedConsole.WriteLine(sb.ToString());
}
Expand Down
15 changes: 12 additions & 3 deletions src/AnalysisPrograms/Production/Parsers/DateTimeOffsetParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ namespace AnalysisPrograms.Production.Parsers
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using McMaster.Extensions.CommandLineUtils;
using McMaster.Extensions.CommandLineUtils.Abstractions;

public class DateTimeOffsetParser : IValueParser
public class DateTimeOffsetParser : IValueParser<DateTimeOffset>
{
public object Parse(string argName, string value)
public Type TargetType => typeof(DateTimeOffset);

public DateTimeOffset Parse(string argName, string value, CultureInfo culture)
{
if (!DateTimeOffset.TryParse(value, out var result))
{
throw new CommandParsingException(null, $"Invalid value specified for {argName}. '{value} is not a valid date time (with offset)");
throw new FormatException($"Invalid value specified for {argName}. '{value} is not a valid date time (with offset)");
}

return result;
}

object IValueParser.Parse(string argName, string value, CultureInfo culture)
{
return this.Parse(argName, value, culture);
}
}
}
15 changes: 12 additions & 3 deletions src/AnalysisPrograms/Production/Parsers/DirectoryInfoParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@ namespace AnalysisPrograms.Production.Parsers
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using McMaster.Extensions.CommandLineUtils;
using McMaster.Extensions.CommandLineUtils.Abstractions;

public class DirectoryInfoParser : IValueParser
public class DirectoryInfoParser : IValueParser<DirectoryInfo>
{
public object Parse(string argName, string value)
public Type TargetType { get; } = typeof(DirectoryInfo);

object IValueParser.Parse(string argName, string value, CultureInfo culture)
{
return this.Parse(argName, value, culture);
}

public DirectoryInfo Parse(string argName, string value, CultureInfo culture)
{
try
{
Expand All @@ -24,7 +33,7 @@ public object Parse(string argName, string value)
catch (ArgumentException aex)
{
var message = $"Argument `{argName}` with value `{value}` could not be converted to a directory. Reason: {aex.Message}";
throw new ArgumentException(message, aex);
throw new FormatException(message, aex);
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/AnalysisPrograms/Production/Parsers/FileInfoParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ namespace AnalysisPrograms.Production.Parsers
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using McMaster.Extensions.CommandLineUtils;
using McMaster.Extensions.CommandLineUtils.Abstractions;

public class FileInfoParser : IValueParser
public class FileInfoParser : IValueParser<FileInfo>
{
public object Parse(string argName, string value)
public Type TargetType { get; } = typeof(FileInfo);

public FileInfo Parse(string argName, string value, CultureInfo culture)
{
return value.IsNotWhitespace() ? new FileInfo(value) : null;
}

object IValueParser.Parse(string argName, string value, CultureInfo culture)
{
return value.IsNotWhitespace() ? new FileInfo(value) : null;
return this.Parse(argName, value, culture);
}
}
}
15 changes: 12 additions & 3 deletions src/AnalysisPrograms/Production/Parsers/TimeSpanParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ namespace AnalysisPrograms.Production.Parsers
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using McMaster.Extensions.CommandLineUtils;
using McMaster.Extensions.CommandLineUtils.Abstractions;

public class TimeSpanParser : IValueParser
public class TimeSpanParser : IValueParser<TimeSpan>
{
public object Parse(string argName, string value)
public Type TargetType { get; } = typeof(TimeSpan);

public TimeSpan Parse(string argName, string value, CultureInfo culture)
{
if (!TimeSpan.TryParse(value, out var result))
{
throw new CommandParsingException(null, $"Invalid value specified for {argName}. '{value} is not a valid date time (with offset)");
throw new FormatException($"Invalid value specified for {argName}. '{value} is not a valid date time (with offset)");
}

return result;
}

object IValueParser.Parse(string argName, string value, CultureInfo culture)
{
return this.Parse(argName, value, culture);
}
}
}
2 changes: 1 addition & 1 deletion src/AnalysisPrograms/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<package id="FSPowerPack.Parallel.Seq.Community" version="3.0.0.0" targetFramework="net462" />
<package id="log4net" version="2.0.8" targetFramework="net462" />
<package id="MathNet.Numerics" version="3.20.2" targetFramework="net462" />
<package id="McMaster.Extensions.CommandLineUtils" version="1.0.0-CI00005" targetFramework="net462" />
<package id="McMaster.Extensions.CommandLineUtils" version="2.2.0-rc" targetFramework="net462" />
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net462" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net462" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net462" />
Expand Down

0 comments on commit fb7ed6d

Please sign in to comment.