-
Notifications
You must be signed in to change notification settings - Fork 0
Add IGlobalOptionsAccessor for typed access to global options #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3d5d75a
Add IGlobalOptionsAccessor for typed access to global options
carldebilly 8d9c187
Document IGlobalOptionsAccessor and UseGlobalOptions<T>
carldebilly 0339558
Support custom route constraints in AddGlobalOption type name
carldebilly d6b711b
Fix stale typed global options in interactive mode
carldebilly 5a2e67c
Add session-sticky global options for interactive mode
carldebilly 8a9d6c6
Propagate property initializer defaults in UseGlobalOptions<T>
carldebilly 3b9e0f5
Address review findings on global options accessor
carldebilly 0c11df6
Fix ToKebabCase for consecutive uppercase and doc singleton warning
carldebilly 959cfb9
Fix PopulateInstance skipping baseline values and add email type name
carldebilly 16428bb
Fix session baseline leak across separate Run() cycles
carldebilly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,83 @@ | ||
| namespace Repl; | ||
|
|
||
| internal sealed class GlobalOptionsSnapshot(ParsingOptions parsingOptions) : IGlobalOptionsAccessor | ||
| { | ||
| private volatile IReadOnlyDictionary<string, IReadOnlyList<string>> _sessionBaseline = | ||
| new Dictionary<string, IReadOnlyList<string>>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| private volatile IReadOnlyDictionary<string, IReadOnlyList<string>> _currentValues = | ||
| new Dictionary<string, IReadOnlyList<string>>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| private volatile HashSet<string> _explicitKeys = new(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| internal void SetSessionBaseline() | ||
| { | ||
| // Capture only the explicitly parsed values as the new baseline. | ||
| // This prevents stale baselines from leaking across separate Run() calls. | ||
| var baseline = new Dictionary<string, IReadOnlyList<string>>(StringComparer.OrdinalIgnoreCase); | ||
| foreach (var key in _explicitKeys) | ||
| { | ||
| if (_currentValues.TryGetValue(key, out var values)) | ||
| { | ||
| baseline[key] = values; | ||
| } | ||
| } | ||
|
carldebilly marked this conversation as resolved.
Dismissed
|
||
|
|
||
| _sessionBaseline = baseline; | ||
| _currentValues = baseline; | ||
| } | ||
|
|
||
| internal void Update(IReadOnlyDictionary<string, IReadOnlyList<string>> parsedValues) | ||
| { | ||
| _explicitKeys = new HashSet<string>(parsedValues.Keys, StringComparer.OrdinalIgnoreCase); | ||
| var merged = new Dictionary<string, IReadOnlyList<string>>(_sessionBaseline, StringComparer.OrdinalIgnoreCase); | ||
| foreach (var (key, value) in parsedValues) | ||
| { | ||
| merged[key] = value; | ||
| } | ||
|
|
||
| _currentValues = merged; | ||
| } | ||
|
|
||
| public T? GetValue<T>(string name, T? defaultValue = default) | ||
| { | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(name); | ||
|
|
||
| if (_currentValues.TryGetValue(name, out var values) && values.Count > 0) | ||
| { | ||
| return (T?)ParameterValueConverter.ConvertSingle( | ||
| values[0], | ||
| typeof(T), | ||
| parsingOptions.NumericFormatProvider); | ||
| } | ||
|
|
||
| if (parsingOptions.GlobalOptions.TryGetValue(name, out var definition) | ||
| && definition.DefaultValue is not null) | ||
| { | ||
| return (T?)ParameterValueConverter.ConvertSingle( | ||
| definition.DefaultValue, | ||
| typeof(T), | ||
| parsingOptions.NumericFormatProvider); | ||
| } | ||
|
|
||
| return defaultValue; | ||
| } | ||
|
|
||
| public IReadOnlyList<string> GetRawValues(string name) | ||
| { | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(name); | ||
|
|
||
| return _currentValues.TryGetValue(name, out var values) | ||
| ? values | ||
| : []; | ||
| } | ||
|
|
||
| public bool HasValue(string name) | ||
| { | ||
| ArgumentException.ThrowIfNullOrWhiteSpace(name); | ||
|
|
||
| return _explicitKeys.Contains(name); | ||
| } | ||
|
|
||
| public IEnumerable<string> GetOptionNames() => _currentValues.Keys; | ||
| } | ||
This file contains hidden or 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,36 @@ | ||
| namespace Repl; | ||
|
|
||
| /// <summary> | ||
| /// Provides typed access to parsed global option values. | ||
| /// Values are updated after each global option parsing pass. | ||
| /// </summary> | ||
| public interface IGlobalOptionsAccessor | ||
| { | ||
| /// <summary> | ||
| /// Gets the typed value of a global option by its registered name. | ||
| /// Returns <paramref name="defaultValue"/> if the option was not provided. | ||
| /// </summary> | ||
| /// <typeparam name="T">Target value type.</typeparam> | ||
| /// <param name="name">The registered option name (without <c>--</c> prefix).</param> | ||
| /// <param name="defaultValue">Value to return if the option was not provided.</param> | ||
| T? GetValue<T>(string name, T? defaultValue = default); | ||
|
|
||
| /// <summary> | ||
| /// Gets all raw string values for a global option (supports repeated options). | ||
| /// Returns an empty list if the option was not provided. | ||
| /// </summary> | ||
| /// <param name="name">The registered option name (without <c>--</c> prefix).</param> | ||
| IReadOnlyList<string> GetRawValues(string name); | ||
|
|
||
| /// <summary> | ||
| /// Returns <see langword="true"/> if the named global option was explicitly provided | ||
| /// in the current invocation. | ||
| /// </summary> | ||
| /// <param name="name">The registered option name (without <c>--</c> prefix).</param> | ||
| bool HasValue(string name); | ||
|
|
||
| /// <summary> | ||
| /// Gets all parsed global option names that have values in the current invocation. | ||
| /// </summary> | ||
| IEnumerable<string> GetOptionNames(); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.