wip
This commit is contained in:
commit
03ebf47b9f
33 changed files with 1657 additions and 0 deletions
101
src/core/Parsers.cs
Normal file
101
src/core/Parsers.cs
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
using System.CommandLine.Parsing;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.Extensions.FileSystemGlobbing;
|
||||
|
||||
namespace Geekeey.Core;
|
||||
|
||||
internal static class Parsers
|
||||
{
|
||||
public static Uri? Uri(ArgumentResult result)
|
||||
{
|
||||
if (!result.Tokens.Any())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!System.Uri.TryCreate(result.Tokens.Single().Value, UriKind.RelativeOrAbsolute, out var uri))
|
||||
{
|
||||
result.AddError("Not a valid URI.");
|
||||
return null;
|
||||
}
|
||||
|
||||
return uri;
|
||||
}
|
||||
|
||||
public static Pattern? Pattern(ArgumentResult result)
|
||||
{
|
||||
if (!result.Tokens.Any())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (bool.TryParse(result.Tokens.Single().Value, out var value))
|
||||
{
|
||||
return new Pattern(value);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var regex = new Regex(result.Tokens.Single().Value, RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
return new Pattern(regex);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
result.AddError("Not a valid boolean or regex pattern.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Matcher? Matcher(ArgumentResult result)
|
||||
{
|
||||
if (!result.Tokens.Any())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var matcher = new Matcher();
|
||||
|
||||
foreach (var token in result.Tokens)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (token.Value.StartsWith('!'))
|
||||
{
|
||||
matcher.AddExclude(token.Value[1..]);
|
||||
}
|
||||
else
|
||||
{
|
||||
matcher.AddInclude(token.Value);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
result.AddError($"Not a valid glob pattern: {token.Value}. {exception.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return matcher;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class Pattern
|
||||
{
|
||||
private readonly bool _value;
|
||||
private readonly Regex? _pattern;
|
||||
|
||||
public Pattern(bool value)
|
||||
{
|
||||
_value = value;
|
||||
}
|
||||
|
||||
public Pattern(Regex pattern)
|
||||
{
|
||||
_pattern = pattern;
|
||||
}
|
||||
|
||||
public bool Match(string version)
|
||||
{
|
||||
return _pattern?.IsMatch(version) ?? _value;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue