This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
mfgames-toolbuilder-cil/src/MfGames.ToolBuilder/GlobalOptionHelper.cs

36 lines
863 B
C#
Raw Normal View History

2021-09-10 17:33:42 +00:00
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;
}
}
}