Skip to content

expose HelpOption and HelpAction, move help configuration to the action #2102

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 5 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ System.CommandLine
public CommandLineBuilder UseExceptionHandler(System.Func<System.Exception,System.CommandLine.Invocation.InvocationContext,System.Int32> onException = null, System.Int32 errorExitCode = 1)
public CommandLineBuilder UseHelp(System.Nullable<System.Int32> maxWidth = null)
public CommandLineBuilder UseHelp(System.String name, System.String[] helpAliases)
public CommandLineBuilder UseHelp(System.Action<System.CommandLine.Help.HelpContext> customize, System.Nullable<System.Int32> maxWidth = null)
public CommandLineBuilder UseHelpBuilder(System.Func<System.CommandLine.Invocation.InvocationContext,System.CommandLine.Help.HelpBuilder> getHelpBuilder)
public CommandLineBuilder UseParseDirective(System.Int32 errorExitCode = 1)
public CommandLineBuilder UseParseErrorReporting(System.Int32 errorExitCode = 1)
public CommandLineBuilder UseSuggestDirective()
Expand All @@ -83,7 +81,7 @@ System.CommandLine
public CommandLineBuilder UseVersionOption(System.String name, System.String[] aliases)
public class CommandLineConfiguration
public static CommandLineBuilder CreateBuilder(Command rootCommand)
.ctor(Command command, System.Boolean enablePosixBundling = True, System.Boolean enableTokenReplacement = True, System.Func<System.CommandLine.Invocation.InvocationContext,System.CommandLine.Help.HelpBuilder> helpBuilderFactory = null, System.CommandLine.Parsing.TryReplaceToken tokenReplacer = null)
.ctor(Command command, System.Boolean enablePosixBundling = True, System.Boolean enableTokenReplacement = True, System.CommandLine.Parsing.TryReplaceToken tokenReplacer = null)
public System.Collections.Generic.IReadOnlyList<Directive> Directives { get; }
public System.Boolean EnablePosixBundling { get; }
public System.Boolean EnableTokenReplacement { get; }
Expand All @@ -106,6 +104,7 @@ System.CommandLine
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
public class EnvironmentVariablesDirective : Directive
.ctor()
public CliAction Action { get; set; }
public interface IConsole : System.CommandLine.IO.IStandardError, System.CommandLine.IO.IStandardIn, System.CommandLine.IO.IStandardOut
public abstract class Option : Symbol, System.CommandLine.Binding.IValueDescriptor
public CliAction Action { get; set; }
Expand Down Expand Up @@ -133,6 +132,7 @@ System.CommandLine
public static Option<T> AcceptExistingOnly<T>(this Option<T> option)
public class ParseDirective : Directive
.ctor(System.Int32 errorExitCode = 1)
public CliAction Action { get; set; }
public class ParseResult
public CliAction Action { get; }
public System.CommandLine.Parsing.CommandResult CommandResult { get; }
Expand Down Expand Up @@ -160,6 +160,7 @@ System.CommandLine
.ctor(System.String description = )
public class SuggestDirective : Directive
.ctor()
public CliAction Action { get; set; }
public abstract class Symbol
public System.String Description { get; set; }
public System.Boolean IsHidden { get; set; }
Expand Down Expand Up @@ -196,6 +197,11 @@ System.CommandLine.Completions
public TextCompletionContext AtCursorPosition(System.Int32 position)
public class TokenCompletionContext : CompletionContext
System.CommandLine.Help
public class HelpAction : System.CommandLine.CliAction
.ctor()
public HelpBuilder Builder { get; set; }
public System.Int32 Invoke(System.CommandLine.Invocation.InvocationContext context)
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(System.CommandLine.Invocation.InvocationContext context, System.Threading.CancellationToken cancellationToken = null)
public class HelpBuilder
.ctor(System.Int32 maxWidth = 2147483647)
public System.Int32 MaxWidth { get; }
Expand Down Expand Up @@ -226,6 +232,12 @@ System.CommandLine.Help
public HelpBuilder HelpBuilder { get; }
public System.IO.TextWriter Output { get; }
public System.CommandLine.ParseResult ParseResult { get; }
public class HelpOption : System.CommandLine.Option<System.Boolean>, System.CommandLine.Binding.IValueDescriptor
.ctor(System.String name, System.String[] aliases)
.ctor()
public System.CommandLine.CliAction Action { get; set; }
public System.Boolean Equals(System.Object obj)
public System.Int32 GetHashCode()
public class TwoColumnHelpRow, System.IEquatable<TwoColumnHelpRow>
.ctor(System.String firstColumnText, System.String secondColumnText)
public System.String FirstColumnText { get; }
Expand All @@ -237,7 +249,6 @@ System.CommandLine.Invocation
public class InvocationContext
.ctor(System.CommandLine.ParseResult parseResult, System.CommandLine.IConsole console = null)
public System.CommandLine.IConsole Console { get; set; }
public System.CommandLine.Help.HelpBuilder HelpBuilder { get; }
public System.CommandLine.ParseResult ParseResult { get; set; }
public T GetValue<T>(Option<T> option)
public T GetValue<T>(Argument<T> argument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,26 @@ public async Task Can_generate_handler_with_well_know_parameters_types()
InvocationContext? boundInvocationContext = null;
IConsole? boundConsole = null;
ParseResult? boundParseResult = null;
HelpBuilder? boundHelpBuilder = null;

void Execute(
InvocationContext invocationContext,
IConsole console,
ParseResult parseResult,
HelpBuilder helpBuilder)
ParseResult parseResult)
{
boundInvocationContext = invocationContext;
boundConsole = console;
boundParseResult = parseResult;
boundHelpBuilder = helpBuilder;
}

var command = new Command("command");

command.SetHandler<Action<InvocationContext, IConsole, ParseResult, HelpBuilder>>(Execute);
command.SetHandler<Action<InvocationContext, IConsole, ParseResult>>(Execute);

await command.InvokeAsync("command", _console);

boundInvocationContext.Should().NotBeNull();
boundConsole.Should().Be(_console);
boundParseResult.Should().NotBeNull();
boundHelpBuilder.Should().NotBeNull();
}

[Fact]
Expand Down

This file was deleted.

6 changes: 0 additions & 6 deletions src/System.CommandLine.Generator/WellKnownTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ internal bool TryGet(ISymbol symbol, out Parameter? parameter)
return true;
}

if (Comparer.Equals(HelpBuilder, symbol))
{
parameter = new HelpBuilderParameter(HelpBuilder);
return true;
}

if (symbol.MetadataName == "System.CommandLine.Binding.BindingContext" && symbol is INamedTypeSymbol bindingContext)
{
parameter = new BindingContextParameter(bindingContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ internal ServiceProvider(BindingContext bindingContext)
{
[typeof(ParseResult)] = _ => bindingContext.ParseResult,
[typeof(IConsole)] = _ => bindingContext.Console,
[typeof(HelpBuilder)] = _ => bindingContext.ParseResult.Configuration.HelpBuilderFactory(bindingContext.InvocationContext),
[typeof(BindingContext)] = _ => bindingContext
};
}
Expand Down
54 changes: 35 additions & 19 deletions src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,21 @@ public void Option_can_customize_first_column_text_based_on_parse_result()
ctx.Command.Equals(commandA)
? optionAFirstColumnText
: optionBFirstColumnText);
var config = new CommandLineBuilder(command)
.UseDefaults()
.UseHelpBuilder(_ => helpBuilder)
.Build();
command.Options.Add(new HelpOption()
{
Action = new HelpAction()
{
Builder = helpBuilder
}
});

var console = new TestConsole();
config.Invoke("root a -h", console);
var config = CommandLineConfiguration.CreateBuilder(command).Build();
command.Parse("root a -h", config).Invoke(console);
console.Out.ToString().Should().Contain(optionAFirstColumnText);

console = new TestConsole();
config.Invoke("root b -h", console);
command.Parse("root b -h", config).Invoke(console);
console.Out.ToString().Should().Contain(optionBFirstColumnText);
}

Expand Down Expand Up @@ -130,10 +134,15 @@ public void Option_can_customize_second_column_text_based_on_parse_result()
ctx.Command.Equals(commandA)
? optionADescription
: optionBDescription);
command.Options.Add(new HelpOption()
{
Action = new HelpAction()
{
Builder = helpBuilder
}
});

var config = new CommandLineBuilder(command)
.UseDefaults()
.UseHelpBuilder(_ => helpBuilder)
.Build();

var console = new TestConsole();
Expand Down Expand Up @@ -253,14 +262,16 @@ public void Option_can_fallback_to_default_when_customizing(bool conditionA, boo
firstColumnText: ctx => conditionA ? "custom 1st" : HelpBuilder.Default.GetOptionUsageLabel(option),
secondColumnText: ctx => conditionB ? "custom 2nd" : option.Description ?? string.Empty);


var config = new CommandLineBuilder(command)
.UseDefaults()
.UseHelpBuilder(_ => helpBuilder)
.Build();
command.Options.Add(new HelpOption()
{
Action = new HelpAction()
{
Builder = helpBuilder
}
});

var console = new TestConsole();
config.Invoke("test -h", console);
command.Parse("test -h", new CommandLineBuilder(command).Build()).Invoke(console);
console.Out.ToString().Should().MatchRegex(expected);
}

Expand Down Expand Up @@ -295,13 +306,18 @@ public void Argument_can_fallback_to_default_when_customizing(
defaultValue: ctx => conditionC ? "custom def" : HelpBuilder.Default.GetArgumentDefaultValue(argument));


var config = new CommandLineBuilder(command)
.UseDefaults()
.UseHelpBuilder(_ => helpBuilder)
.Build();
var config = new CommandLineBuilder(command).Build();

command.Options.Add(new HelpOption()
{
Action = new HelpAction()
{
Builder = helpBuilder
}
});

var console = new TestConsole();
config.Invoke("test -h", console);
command.Parse("test -h", config).Invoke(console);
console.Out.ToString().Should().MatchRegex(expected);
}
}
Expand Down
57 changes: 4 additions & 53 deletions src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,61 +126,12 @@ public void When_command_handler_throws_then_Invoke_does_not_handle_the_exceptio
}

[Fact]
public async Task When_no_help_builder_is_specified_it_uses_default_implementation()
public void When_no_help_builder_is_specified_it_uses_default_implementation()
{
bool handlerWasCalled = false;
HelpOption helpOption = new();

var command = new Command("help-command");
command.SetAction((context, cancellationToken) =>
{
handlerWasCalled = true;
context.HelpBuilder.Should().NotBeNull();
return Task.FromResult(0);
});

var config = new CommandLineBuilder(new RootCommand
{
command
})
.Build();

await config.InvokeAsync("help-command", new TestConsole());

handlerWasCalled.Should().BeTrue();
}

[Fact]
public async Task When_help_builder_factory_is_specified_it_is_used_to_create_the_help_builder()
{
bool handlerWasCalled = false;
bool factoryWasCalled = false;

HelpBuilder createdHelpBuilder = null;

var command = new Command("help-command");
command.SetAction((context, cancellationToken) =>
{
handlerWasCalled = true;
context.HelpBuilder.Should().Be(createdHelpBuilder);
createdHelpBuilder.Should().NotBeNull();
return Task.FromResult(0);
});

var config = new CommandLineBuilder(new RootCommand
{
command
})
.UseHelpBuilder(context =>
{
factoryWasCalled = true;
return createdHelpBuilder = new HelpBuilder();
})
.Build();

await config.InvokeAsync("help-command", new TestConsole());

handlerWasCalled.Should().BeTrue();
factoryWasCalled.Should().BeTrue();
helpOption.Action.Should().NotBeNull();
(helpOption.Action as HelpAction).Builder.Should().NotBeNull();
}
}
}
Loading