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

Added the ability to use \n and/or \r in HelpText #169

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ build/*
packages
*.suo
*.userprefs
*.DotSettings.user
*~
\#*\#
*.pidb
Expand Down
6 changes: 0 additions & 6 deletions CommandLine4.sln.DotSettings.user

This file was deleted.

2 changes: 2 additions & 0 deletions src/CommandLine.Tests/CommandLine.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\packages\xunit.runner.visualstudio.2.0.0\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\..\packages\xunit.runner.visualstudio.2.0.0\build\net20\xunit.runner.visualstudio.props')" />
<Import Project="..\..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props" Condition="Exists('..\..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -132,6 +133,7 @@
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props'))" />
<Error Condition="!Exists('..\..\packages\xunit.runner.visualstudio.2.0.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.runner.visualstudio.2.0.0\build\net20\xunit.runner.visualstudio.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
21 changes: 21 additions & 0 deletions src/CommandLine.Tests/Fakes/HelpFakes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ class FakeOptionsWithLongDescription
public string FileName { get; set; }
}

class FakeOptionsWithLongDescriptionAndNewLines
{
[Option('v', "verbose", HelpText = "This is the description of the verbosity to test out the wrapping capabilities of the Help Text.\n\nIn Addition to testing the insertion of line feeds.")]
public bool Verbose { get; set; }

[Option("input-file", HelpText = "This is a very long description of the Input File argument that gets passed in. It should be passed in as a string.\nThis tests a single line feed insertion.")]
public string FileName { get; set; }
}

class FakeOptionsWithLongDescriptionAndMixedNewLines
{
/// <summary>
/// HelpText string with a line feed character followed by a carriage return + line feed, should behave the same as 2 line feeds
/// </summary>
[Option('v', "verbose", HelpText = "This is the description of the verbosity to test out the wrapping capabilities of the Help Text.\n\r\nIn Addition to testing the insertion of line feeds.")]
public bool Verbose { get; set; }

[Option("input-file", HelpText = "This is a very long description of the Input File argument that gets passed in. It should be passed in as a string.\r\nThis tests a single line feed insertion.")]
public string FileName { get; set; }
}

class FakeOptionsWithLongDescriptionAndNoSpaces
{
[Option('v', "verbose", HelpText = "Before 012345678901234567890123 After")]
Expand Down
70 changes: 70 additions & 0 deletions src/CommandLine.Tests/Unit/Text/HelpTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,76 @@ public void When_help_text_is_longer_than_width_it_will_wrap_around_as_if_in_a_c
// Teardown
}

[Fact]
public void When_help_text_is_longer_than_width_it_will_wrap_around_as_if_in_a_column_with_new_lines()
{
// Fixture setup
// Exercize system
var sut = new HelpText(new HeadingInfo("CommandLine.Tests.dll", "1.9.4.131"));
sut.MaximumDisplayWidth = 40;
sut.AddOptions(new FakeOptionsWithLongDescriptionAndNewLines());

// Verify outcome
var text = sut.ToString();
var lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
lines[2].Should().Be(" v, verbose This is the description"); //"The first line should have the arguments and the start of the Help Text.");
lines[3].Should().Be(" of the verbosity to ");
lines[4].Should().Be(" test out the wrapping ");
lines[5].Should().Be(" capabilities of the ");
// The following line ended as a result of the first line feed
lines[6].Should().Be(" Help Text.");
// The following empty line is the result of the second line feed
lines[7].Should().Be(" ");
lines[8].Should().Be(" In Addition to testing ");
lines[9].Should().Be(" the insertion of line ");
lines[10].Should().Be(" feeds.");
lines[11].Should().Be(" input-file This is a very long ");
lines[12].Should().Be(" description of the ");
lines[13].Should().Be(" Input File argument ");
lines[14].Should().Be(" that gets passed in. ");
lines[15].Should().Be(" It should be passed in");
// The following line ended as a result of the lone line feed
lines[16].Should().Be(" as a string.");
lines[17].Should().Be(" This tests a single ");
lines[18].Should().Be(" line feed insertion.");
// Teardown
}

[Fact]
public void When_help_text_is_longer_than_width_it_will_wrap_around_as_if_in_a_column_with_mixed_new_line_styles()
{
// Fixture setup
// Exercize system
var sut = new HelpText(new HeadingInfo("CommandLine.Tests.dll", "1.9.4.131"));
sut.MaximumDisplayWidth = 40;
sut.AddOptions(new FakeOptionsWithLongDescriptionAndMixedNewLines());

// Verify outcome
var text = sut.ToString();
var lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
lines[2].Should().Be(" v, verbose This is the description"); //"The first line should have the arguments and the start of the Help Text.");
lines[3].Should().Be(" of the verbosity to ");
lines[4].Should().Be(" test out the wrapping ");
lines[5].Should().Be(" capabilities of the ");
// The following line ended as a result of the line feed without a carriage return
lines[6].Should().Be(" Help Text.");
// The following empty line is the result of the carriage return + line feed
lines[7].Should().Be(" ");
lines[8].Should().Be(" In Addition to testing ");
lines[9].Should().Be(" the insertion of line ");
lines[10].Should().Be(" feeds.");
lines[11].Should().Be(" input-file This is a very long ");
lines[12].Should().Be(" description of the ");
lines[13].Should().Be(" Input File argument ");
lines[14].Should().Be(" that gets passed in. ");
lines[15].Should().Be(" It should be passed in");
// The following line ended as a result of the lone carriage return + line feed
lines[16].Should().Be(" as a string.");
lines[17].Should().Be(" This tests a single ");
lines[18].Should().Be(" line feed insertion.");
// Teardown
}

[Fact]
public void Long_help_text_without_spaces()
{
Expand Down
1 change: 1 addition & 0 deletions src/CommandLine.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<package id="xunit.core" version="2.0.0" targetFramework="net45" />
<package id="xunit.extensibility.core" version="2.0.0" targetFramework="net45" />
<package id="xunit.extensions" version="2.0.0" targetFramework="net45" />
<package id="xunit.runner.visualstudio" version="2.0.0" targetFramework="net45" />
Copy link
Collaborator

Choose a reason for hiding this comment

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

I ran across this problem as well needing to add the xunit.runner dependency. Can we get this as a separate pull request (I can do it)? I would like this merged right away, separate from this discussion.

Copy link
Author

Choose a reason for hiding this comment

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

Ya, that was part of my comments above. I thought you might want to do that.

@mizipzor Should I negate that from my branch to remove it from this pull request when my tests are complete?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I just pushed this in 4a155be, so just rebase this branch on top of master and that line should go away (since it is already added). I think we prefer rebases over merges to keep a linear history but I'm not sure so we might want a comment from @gsscoder here.

Copy link
Author

Choose a reason for hiding this comment

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

That's works for me. That just means I don't need to do anything :o)

</packages>
69 changes: 44 additions & 25 deletions src/CommandLine/Text/HelpText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,46 +640,65 @@ private HelpText AddOption(string requiredWord, int maxLength, OptionSpecificati

if (!string.IsNullOrEmpty(optionHelpText))
{
do
// This uses explicit \r\n and \n values instead of Environment.NewLine (that is either one or the other) to make HelpText creation easier for the developer
var paragraphs = optionHelpText.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
for (var p = 0; p < paragraphs.Length; p++)
{
var wordBuffer = 0;
var words = optionHelpText.Split(new[] { ' ' });
for (var i = 0; i < words.Length; i++)
var paragraph = paragraphs[p].Trim();

if (paragraph.Length > 0)
{
if (words[i].Length < (widthOfHelpText - wordBuffer))
do
{
this.optionsHelp.Append(words[i]);
wordBuffer += words[i].Length;
if ((widthOfHelpText - wordBuffer) > 1 && i != words.Length - 1)
var wordBuffer = 0;
var words = paragraph.Split(new[] { ' ' });
for (var i = 0; i < words.Length; i++)
{
this.optionsHelp.Append(" ");
wordBuffer++;
if (words[i].Length < (widthOfHelpText - wordBuffer))
{
this.optionsHelp.Append(words[i]);
wordBuffer += words[i].Length;
if ((widthOfHelpText - wordBuffer) > 1 && i != words.Length - 1)
{
this.optionsHelp.Append(" ");
wordBuffer++;
}
}
else if (words[i].Length >= widthOfHelpText && wordBuffer == 0)
{
this.optionsHelp.Append(words[i].Substring(0, widthOfHelpText));
wordBuffer = widthOfHelpText;
break;
}
else
{
break;
}
}

paragraph = paragraph.Substring(
Math.Min(wordBuffer, paragraph.Length)).Trim();
if (paragraph.Length > 0)
{
this.optionsHelp.Append(Environment.NewLine);
this.optionsHelp.Append(new string(' ', maxLength + 6));
}

}
else if (words[i].Length >= widthOfHelpText && wordBuffer == 0)
{
this.optionsHelp.Append(words[i].Substring(0, widthOfHelpText));
wordBuffer = widthOfHelpText;
break;
}
else
{
break;
}
while (paragraph.Length > widthOfHelpText);

this.optionsHelp.Append(paragraph);

}

optionHelpText = optionHelpText.Substring(
Math.Min(wordBuffer, optionHelpText.Length)).Trim();
if (optionHelpText.Length > 0)
if (p < paragraphs.Length - 1)
{
this.optionsHelp.Append(Environment.NewLine);
this.optionsHelp.Append(new string(' ', maxLength + 6));
}
}
while (optionHelpText.Length > widthOfHelpText);
}

this.optionsHelp.Append(optionHelpText);
this.optionsHelp.Append(Environment.NewLine);
if (this.additionalNewLineAfterOption)
{
Expand Down