Skip to content
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

replace IConsole with two TextWriters in Configuration #2106

Merged
merged 10 commits into from
Mar 24, 2023

Conversation

adamsitnik
Copy link
Member

@adamsitnik adamsitnik commented Mar 20, 2023

The whole idea behind this PR is to move IConsole to Rendering and extend the Config with two mutable TextWriter properties (Out and Error). Both properties are lazy initialized (perf), with default values of Console.Out and Console.Error. To test the produced output, they can be preconfigured with StringWriter instances. To disable the ouput, they can be set to TextWriter.Null.

/// <summary>
/// The standard output. Used by Help and other facilities that write non-error information.
/// By default it's set to <see cref="Console.Out"/>.
/// For testing purposes, it can be set to a new instance of <see cref="StringWriter"/>.
/// If you want to disable the output, please set it to <see cref="TextWriter.Null"/>.
/// </summary>
public TextWriter Out
{ 
    get => _output ??= Console.Out;
    set => _output = value ?? throw new ArgumentNullException(nameof(value), "Use TextWriter.Null to disable the output");
}

/// <summary>
/// The standard error. Used for printing error information like parse errors.
/// By default it's set to <see cref="Console.Error"/>.
/// For testing purposes, it can be set to a new instance of <see cref="StringWriter"/>.
/// </summary>
public TextWriter Error
{
    get => _error ??= Console.Error;
    set => _error = value ?? throw new ArgumentNullException(nameof(value), "Use TextWriter.Null to disable the output");
}

contributes to #1905, it's a first step towards making configuration mutable

it allows us for creating the host and everything else after parsing has finished
UseCommandHandler must specify the handler for instance of a Command, not per type as there can be multiple commands of the same type
# Conflicts:
#	src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt
#	src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs
#	src/System.CommandLine/Builder/CommandLineBuilder.cs
#	src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs
#	src/System.CommandLine/CommandLineConfiguration.cs
# Conflicts:
#	src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt
#	src/System.CommandLine.Generator.Tests/GeneratedCommandHandlerTests.cs
#	src/System.CommandLine.Hosting/HostingAction.cs
#	src/System.CommandLine.NamingConventionBinder/ServiceProvider.cs
#	src/System.CommandLine.Tests/CommandExtensionsTests.cs
#	src/System.CommandLine.Tests/Help/HelpBuilderTests.Customization.cs
#	src/System.CommandLine.Tests/Invocation/InvocationPipelineTests.cs
#	src/System.CommandLine.Tests/UseExceptionHandlerTests.cs
#	src/System.CommandLine.Tests/UseHelpTests.cs
#	src/System.CommandLine/Help/HelpOption.cs
#	src/System.CommandLine/Help/HelpOptionAction.cs
#	src/System.CommandLine/Invocation/InvocationContext.cs
@adamsitnik adamsitnik marked this pull request as ready for review March 21, 2023 20:18
@adamsitnik adamsitnik requested review from jonsequitur and Keboo March 21, 2023 20:18
@@ -85,11 +80,13 @@ System.CommandLine
public System.Collections.Generic.IReadOnlyList<Directive> Directives { get; }
public System.Boolean EnablePosixBundling { get; }
public System.Boolean EnableTokenReplacement { get; }
public System.IO.TextWriter Error { get; set; }
public System.IO.TextWriter Out { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

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

The names Error and Out might need a little more context unless we're also renaming CommandLineConfiguration, e.g. ConsoleOut and ConsoleError.

@adamsitnik adamsitnik merged commit b90bff7 into dotnet:main Mar 24, 2023
@adamsitnik adamsitnik deleted the removeIConsole branch March 24, 2023 17:19
@tmds
Copy link
Member

tmds commented Mar 27, 2023

The whole idea behind this PR is to move IConsole to Rendering and extend the Config with two mutable TextWriter properties (Out and Error).

    public interface IConsole :
        IStandardOut,
        IStandardError,
        IStandardIn

@adamsitnik @jonsequitur how does standard input fit into this?

@jonsequitur
Copy link
Contributor

We never arrived at a satisfactory design for these interfaces. We decided it's safest to leave it out of scope for now.

@tmds
Copy link
Member

tmds commented Mar 29, 2023

That makes sense. My main rule of API design: when in doubt, leave it out.

This seems somewhat awkward:

parseResult.Configuration.Output.WriteLine("Hello");

@jonsequitur
Copy link
Contributor

It is a little awkward. We're working on how to better separate parser configuration from invocation-related concerns. Suggestions are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants