-
Notifications
You must be signed in to change notification settings - Fork 409
Closed
Description
From #2046:
Currently the only way for the users to implement custom directives is enabling directives parsing in the config, using this property to get parsed output:
public IReadOnlyDictionary<string, IReadOnlyList<string>> Directives => _directives ??= new (); |
and provide a custom, expensive Middleware.
We need to introduce a dedicated Directive
type that derives from Symbol
.
Initial requirements:
- introducing new
Directive
symbol type, with a possibility to define anAction<ParseResult, string>
which should be executed when directive is parsed (the list of arguments for the action is an open question, when implementing the feature it should became more obvious) - introducing
Command.Directives
property to allow users for defining directives - turning existing code into new directives
command-line-api/src/System.CommandLine/Parsing/ParseOperation.cs
Lines 350 to 378 in 76437b0
private void OnDirectiveParsed(string directiveKey, string? parsedValues) { if (_configuration.EnableEnvironmentVariableDirective && directiveKey == "env") { if (parsedValues is not null) { var components = parsedValues.Split(new[] { '=' }, count: 2); var variable = components.Length > 0 ? components[0].Trim() : string.Empty; if (string.IsNullOrEmpty(variable) || components.Length < 2) { return; } var value = components[1].Trim(); Environment.SetEnvironmentVariable(variable, value); } } else if (_configuration.ParseDirectiveExitCode.HasValue && directiveKey == "parse") { _isParseRequested = true; _handler = new AnonymousCommandHandler(ParseDirectiveResult.Apply); } else if (_configuration.EnableSuggestDirective && directiveKey == "suggest") { int position = parsedValues is not null ? int.Parse(parsedValues) : _rawInput?.Length ?? 0; _handler = new AnonymousCommandHandler(ctx => SuggestDirectiveResult.Apply(ctx, position)); } } - extending tokenization and parsing with the concept of Directive
Metadata
Metadata
Assignees
Labels
No labels