36 lines
863 B
C#
36 lines
863 B
C#
using System.CommandLine;
|
|
using System.CommandLine.Parsing;
|
|
|
|
namespace MfGames.ToolBuilder
|
|
{
|
|
/// <summary>
|
|
/// Helper methods for pulling out values without performing the full parse.
|
|
/// </summary>
|
|
public static class GlobalOptionHelper
|
|
{
|
|
public static TType GetArgumentValue<TType>(
|
|
Option<TType> option,
|
|
string[] arguments,
|
|
TType defaultValue)
|
|
{
|
|
var rootCommand = new RootCommand
|
|
{
|
|
option,
|
|
};
|
|
|
|
rootCommand.TreatUnmatchedTokensAsErrors = false;
|
|
|
|
ParseResult results = rootCommand.Parse(arguments);
|
|
|
|
if (!results.HasOption(option))
|
|
{
|
|
return defaultValue;
|
|
}
|
|
|
|
TType value = results.ValueForOption(option)!;
|
|
|
|
return value;
|
|
}
|
|
}
|
|
}
|