Skip to content

Introduce Directive symbol type #2056

Closed
@adamsitnik

Description

@adamsitnik

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 an Action<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
    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
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions