-
Notifications
You must be signed in to change notification settings - Fork 402
make HelpBuilder and related APIs internal #2563
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
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e7038d7
make HelpBuilder and related APIs internal
adamsitnik e99a785
don't use InternalsVisibleTo, link the code and stop using internal A…
adamsitnik 6a7281d
fix the build?
adamsitnik 2fb6d6b
add a tests based on CLR and diagnostics tools use cases
adamsitnik 45281b1
add method missing for SDK scenario
adamsitnik f788bed
add test for the bug I've hit in SDK: the same options being reused f…
adamsitnik e978cf8
The same symbol can be added to multiple commands and have multiple p…
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.CommandLine.Invocation; | ||
|
||
namespace System.CommandLine.Help | ||
{ | ||
/// <summary> | ||
/// Provides command line help. | ||
/// </summary> | ||
public sealed class CustomHelpAction : SynchronousCommandLineAction | ||
{ | ||
private HelpBuilder? _builder; | ||
|
||
/// <summary> | ||
/// Specifies an <see cref="Builder"/> to be used to format help output when help is requested. | ||
/// </summary> | ||
internal HelpBuilder Builder | ||
{ | ||
get => _builder ??= new HelpBuilder(Console.IsOutputRedirected ? int.MaxValue : Console.WindowWidth); | ||
set => _builder = value ?? throw new ArgumentNullException(nameof(value)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int Invoke(ParseResult parseResult) | ||
{ | ||
var output = parseResult.Configuration.Output; | ||
|
||
var helpContext = new HelpContext(Builder, | ||
parseResult.CommandResult.Command, | ||
output); | ||
|
||
Builder.Write(helpContext); | ||
|
||
return 0; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,7 @@ public void Option_can_customize_first_column_text_based_on_parse_result() | |
: optionBFirstColumnText); | ||
command.Options.Add(new HelpOption() | ||
{ | ||
Action = new HelpAction() | ||
Action = new CustomHelpAction() | ||
{ | ||
Builder = helpBuilder | ||
} | ||
|
@@ -140,7 +140,7 @@ public void Option_can_customize_second_column_text_based_on_parse_result() | |
: optionBDescription); | ||
command.Options.Add(new HelpOption | ||
{ | ||
Action = new HelpAction | ||
Action = new CustomHelpAction | ||
{ | ||
Builder = helpBuilder | ||
} | ||
|
@@ -269,7 +269,7 @@ public void Option_can_fallback_to_default_when_customizing(bool conditionA, boo | |
|
||
command.Options.Add(new HelpOption | ||
{ | ||
Action = new HelpAction | ||
Action = new CustomHelpAction | ||
{ | ||
Builder = helpBuilder | ||
} | ||
|
@@ -317,7 +317,7 @@ public void Argument_can_fallback_to_default_when_customizing( | |
|
||
command.Options.Add(new HelpOption | ||
{ | ||
Action = new HelpAction | ||
Action = new CustomHelpAction | ||
{ | ||
Builder = helpBuilder | ||
} | ||
A935
|
@@ -336,11 +336,20 @@ public void Individual_symbols_can_be_customized() | |
var option = new Option<int>("-x") { Description = "The default option description" }; | ||
var argument = new Argument<int>("int-value") { Description = "The default argument description" }; | ||
|
||
var rootCommand = new RootCommand | ||
CustomHelpAction helpAction = new(); | ||
helpAction.Builder.CustomizeSymbol(subcommand, secondColumnText: "The custom command description"); | ||
helpAction.Builder.CustomizeSymbol(option, secondColumnText: "The custom option description"); | ||
helpAction.Builder.CustomizeSymbol(argument, secondColumnText: "The custom argument description"); | ||
|
||
var rootCommand = new Command("name") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am using |
||
{ | ||
subcommand, | ||
option, | ||
argument, | ||
new HelpOption() | ||
{ | ||
Action = helpAction | ||
} | ||
}; | ||
|
||
CommandLineConfiguration config = new(rootCommand) | ||
|
@@ -350,13 +359,6 @@ public void Individual_symbols_can_be_customized() | |
|
||
ParseResult parseResult = rootCommand.Parse("-h", config); | ||
|
||
if (parseResult.Action is HelpAction helpAction) | ||
{ | ||
helpAction.Builder.CustomizeSymbol(subcommand, secondColumnText: "The custom command description"); | ||
helpAction.Builder.CustomizeSymbol(option, secondColumnText: "The custom option description"); | ||
helpAction.Builder.CustomizeSymbol(argument, secondColumnText: "The custom argument description"); | ||
} | ||
|
||
parseResult.Invoke(); | ||
|
||
config.Output | ||
|
@@ -370,18 +372,16 @@ public void Individual_symbols_can_be_customized() | |
[Fact] | ||
public void Help_sections_can_be_replaced() | ||
{ | ||
CommandLineConfiguration config = new(new RootCommand()) | ||
CustomHelpAction helpAction = new(); | ||
helpAction.Builder.CustomizeLayout(CustomLayout); | ||
|
||
CommandLineConfiguration config = new(new Command("name") { new HelpOption() { Action = helpAction} }) | ||
{ | ||
Output = new StringWriter() | ||
}; | ||
|
||
ParseResult parseResult = config.Parse("-h"); | ||
|
||
if (parseResult.Action is HelpAction helpAction) | ||
{ | ||
helpAction.Builder.CustomizeLayout(CustomLayout); | ||
} | ||
|
||
parseResult.Invoke(); | ||
|
||
config.Output.ToString().Should().Be($"one{NewLine}{NewLine}two{NewLine}{NewLine}three{NewLine}{NewLine}"); | ||
|
@@ -397,20 +397,18 @@ IEnumerable<Func<HelpContext, bool>> CustomLayout(HelpContext _) | |
[Fact] | ||
public void Help_sections_can_be_supplemented() | ||
{ | ||
CommandLineConfiguration config = new(new RootCommand("hello")) | ||
CustomHelpAction helpAction = new(); | ||
helpAction.Builder.CustomizeLayout(CustomLayout); | ||
|
||
CommandLineConfiguration config = new(new Command("hello") { new HelpOption() { Action = helpAction } }) | ||
{ | ||
Output = new StringWriter(), | ||
}; | ||
|
||
var defaultHelp = GetDefaultHelp(config.RootCommand); | ||
var defaultHelp = GetDefaultHelp(new Command("hello")); | ||
|
||
ParseResult parseResult = config.Parse("-h"); | ||
|
||
if (parseResult.Action is HelpAction helpAction) | ||
{ | ||
helpAction.Builder.CustomizeLayout(CustomLayout); | ||
} | ||
|
||
parseResult.Invoke(); | ||
|
||
var output = config.Output.ToString(); | ||
|
@@ -444,7 +442,7 @@ public void Layout_can_be_composed_dynamically_based_on_context() | |
commandWithCustomHelp | ||
}; | ||
|
||
command.Options.OfType<HelpOption>().Single().Action = new HelpAction | ||
command.Options.OfType<HelpOption>().Single().Action = new CustomHelpAction | ||
{ | ||
Builder = helpBuilder | ||
}; | ||
|
@@ -480,7 +478,7 @@ public void Help_default_sections_can_be_wrapped() | |
}, | ||
new HelpOption | ||
{ | ||
Action = new HelpAction | ||
Action = new CustomHelpAction | ||
{ | ||
Builder = new HelpBuilder(30) | ||
} | ||
|
@@ -509,19 +507,17 @@ public void Help_default_sections_can_be_wrapped() | |
[Fact] | ||
public void Help_customized_sections_can_be_wrapped() | ||
{ | ||
CommandLineConfiguration config = new(new RootCommand()) | ||
CustomHelpAction helpAction = new(); | ||
helpAction.Builder = new HelpBuilder(10); | ||
helpAction.Builder.CustomizeLayout(CustomLayout); | ||
|
||
CommandLineConfiguration config = new(new Command("name") { new HelpOption() { Action = helpAction } }) | ||
{ | ||
Output = new StringWriter() | ||
}; | ||
|
||
ParseResult parseResult = config.Parse("-h"); | ||
|
||
if (parseResult.Action is HelpAction helpAction) | ||
{ | ||
helpAction.Builder = new HelpBuilder(10); | ||
helpAction.Builder.CustomizeLayout(CustomLayout); | ||
} | ||
|
||
parseResult.Invoke(); | ||
|
||
string result = config.Output.ToString(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
2BAA
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed this to be able to avoid using
Option.Argument
(internal getter), it contributes to #2526