Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove OnParsed: call the parsed command handler from directive handl…
…er (a dirty solution)
  • Loading branch information
adamsitnik committed Feb 22, 2023
commit 7127bc2deded68611a734834c9bdd169543c2b79
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,8 @@ System.CommandLine
public class Directive : Symbol
.ctor(System.String name, System.String description = null, System.Action<System.CommandLine.Invocation.InvocationContext> syncHandler = null, System.Func<System.CommandLine.Invocation.InvocationContext,System.Threading.CancellationToken,System.Threading.Tasks.Task> asyncHandler = null)
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
public System.Void OnParsed(System.CommandLine.Parsing.DirectiveResult directiveResult)
public class EnvironmentVariablesDirective : Directive
.ctor()
public System.Void OnParsed(System.CommandLine.Parsing.DirectiveResult directiveResult)
public static class Handler
public static System.Void SetHandler(this Command command, System.Action<System.CommandLine.Invocation.InvocationContext> handle)
public static System.Void SetHandler(this Command command, System.Action handle)
Expand Down
9 changes: 0 additions & 9 deletions src/System.CommandLine/Directive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,6 @@ public Directive(string name,

internal ICommandHandler? Handler { get; }

/// <summary>
/// Method executed when given Directive is being parsed.
/// Useful for Directives that want to perform an action without setting the Handler for ParseResult.
/// </summary>
/// <param name="directiveResult">Parsed directive result.</param>
public virtual void OnParsed(DirectiveResult directiveResult)
{
}

private protected override string DefaultName => throw new NotImplementedException();

public override IEnumerable<CompletionItem> GetCompletions(CompletionContext context)
Expand Down
13 changes: 10 additions & 3 deletions src/System.CommandLine/EnvironmentVariablesDirective.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.CommandLine.Parsing;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;

namespace System.CommandLine
{
Expand All @@ -7,12 +8,15 @@ namespace System.CommandLine
/// </summary>
public sealed class EnvironmentVariablesDirective : Directive
{
public EnvironmentVariablesDirective() : base("env")
public EnvironmentVariablesDirective() : base("env", syncHandler: SyncHandler)
{
}

public override void OnParsed(DirectiveResult directiveResult)
private static void SyncHandler(InvocationContext context)
{
EnvironmentVariablesDirective symbol = (EnvironmentVariablesDirective)context.ParseResult.Symbol;
DirectiveResult directiveResult = context.ParseResult.FindResultFor(symbol)!;

for (int i = 0; i < directiveResult.Values.Count; i++)
{
string parsedValue = directiveResult.Values[i];
Expand All @@ -31,6 +35,9 @@ public override void OnParsed(DirectiveResult directiveResult)
}
}
}

// we need a cleaner, more flexible and intuitive way of continuing the execution
context.ParseResult.CommandResult.Command.Handler?.Invoke(context);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonsequitur I've reverted the ability to handle continuations and hardcoded it for now as we have agreed

fc987d4

}
}
}
1 change: 0 additions & 1 deletion src/System.CommandLine/Parsing/ParseOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ void ParseDirective()
result.AddValue(withoutBrackets.Slice(indexOfColon + 1).ToString());
}

directive.OnParsed(result);
_handler = directive.Handler;
_symbol = directive;

Expand Down