-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from GeminiLab/f/hook
add parsing hook (close #2)
- Loading branch information
Showing
6 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System.Collections.Generic; | ||
using GeminiLab.Core2.CommandLineParser; | ||
using GeminiLab.Core2.CommandLineParser.Default; | ||
using Xunit; | ||
|
||
namespace XUnitTester { | ||
public static class LifecycleHook { | ||
class HookedOption { | ||
public List<string> Queue { get; set; } = new List<string>(); | ||
|
||
[PreParsing] | ||
public void PreParsing() { | ||
Queue.Add("pre"); | ||
} | ||
|
||
[PostParsing] | ||
public void PostParsing() { | ||
Queue.Add("post"); | ||
} | ||
|
||
[ShortOption('z', OptionParameter.Required)] | ||
public void Zulu(string z) { | ||
Queue.Add($"z:{z}"); | ||
} | ||
} | ||
|
||
[Fact] | ||
public static void LifecycleHookTest() { | ||
var options = CommandLineParser<HookedOption>.DoParse("-zzulu"); | ||
|
||
Assert.Equal(new string[] { "pre", "z:zulu", "post" }, options.Queue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/GeminiLab.Core2.CommandLineParser/Custom/IParsingHook.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace GeminiLab.Core2.CommandLineParser.Custom { | ||
public enum ParsingEvent { | ||
PreParsing, | ||
PostParsing, | ||
} | ||
|
||
public interface IParsingHook { | ||
void OnParsingEvent(ParsingEvent parsingEvent, object target); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/GeminiLab.Core2.CommandLineParser/Default/LifecycleHookAttributes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using GeminiLab.Core2.CommandLineParser.Custom; | ||
|
||
namespace GeminiLab.Core2.CommandLineParser.Default { | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
public class PreParsingAttribute : ParsingAttribute { } | ||
|
||
|
||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
public class PostParsingAttribute : ParsingAttribute { } | ||
} |
27 changes: 27 additions & 0 deletions
27
src/GeminiLab.Core2.CommandLineParser/Default/LifecycleHookComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using GeminiLab.Core2.CommandLineParser.Custom; | ||
|
||
namespace GeminiLab.Core2.CommandLineParser.Default { | ||
public class LifecycleHookComponent : IParsingHook, IAttributeCategory<PreParsingAttribute>, IAttributeCategory<PostParsingAttribute> { | ||
private MethodInfo? _pre, _post; | ||
|
||
public void OnParsingEvent(ParsingEvent parsingEvent, object target) { | ||
(parsingEvent switch { | ||
ParsingEvent.PreParsing => _pre, | ||
ParsingEvent.PostParsing => _post, | ||
_ => null, | ||
})?.Invoke(target, Array.Empty<object>()); | ||
} | ||
|
||
IEnumerable<IAttributeCategory<PreParsingAttribute>.MemberWithAttribute> IAttributeCategory<PreParsingAttribute>.Options { | ||
set { _pre = value.Select(mwa => mwa.Target).OfType<MethodInfo>().FirstOrDefault(); } | ||
} | ||
|
||
IEnumerable<IAttributeCategory<PostParsingAttribute>.MemberWithAttribute> IAttributeCategory<PostParsingAttribute>.Options { | ||
set { _post = value.Select(mwa => mwa.Target).OfType<MethodInfo>().FirstOrDefault(); } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters