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
Dylan R. E. Moonfire ad0525be04 feat: initial commit
2021-09-10 12:33:42 -05:00

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;
}
}
}