Skip to content

Commit 2580024

Browse files
authored
Merge pull request #467 from moh-hassan/helptext-autobuild
Fix issues for HelpText.AutoBuild configuration (issues #224 , # 259)
2 parents 86d7582 + 4703c74 commit 2580024

File tree

4 files changed

+141
-2
lines changed

4 files changed

+141
-2
lines changed

src/CommandLine/ErrorExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ public static IEnumerable<Error> OnlyMeaningfulOnes(this IEnumerable<Error> erro
2323
.Where(e => !(e.Tag == ErrorType.UnknownOptionError
2424
&& ((UnknownOptionError)e).Token.EqualsOrdinalIgnoreCase("help")));
2525
}
26+
2627
}
2728
}

src/CommandLine/HelpTextExtensions.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2+
using System;
3+
using System.IO;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
namespace CommandLine
8+
{
9+
public static class HelpTextExtensions
10+
{
11+
/// <summary>
12+
/// return true when errors contain HelpXXXError
13+
/// </summary>
14+
public static bool IsHelp(this IEnumerable<Error> errs)
15+
{
16+
if (errs.Any(x => x.Tag == ErrorType.HelpRequestedError ||
17+
x.Tag == ErrorType.HelpVerbRequestedError))
18+
return true;
19+
//when AutoHelp=false in parser, help is disabled and Parser raise UnknownOptionError
20+
return errs.Any(x => (x is UnknownOptionError ee ? ee.Token : "") == "help");
21+
}
22+
23+
/// <summary>
24+
/// return true when errors contain VersionXXXError
25+
/// </summary>
26+
public static bool IsVersion(this IEnumerable<Error> errs)
27+
{
28+
if (errs.Any(x => x.Tag == ErrorType.VersionRequestedError))
29+
return true;
30+
//when AutoVersion=false in parser, Version is disabled and Parser raise UnknownOptionError
31+
return errs.Any(x => (x is UnknownOptionError ee ? ee.Token : "") == "version");
32+
}
33+
/// <summary>
34+
/// redirect errs to Console.Error, and to Console.Out for help/version error
35+
/// </summary>
36+
public static TextWriter Output(this IEnumerable<Error> errs)
37+
{
38+
if (errs.IsHelp() || errs.IsVersion())
39+
return Console.Out;
40+
return Console.Error;
41+
}
42+
}
43+
}
44+
45+

src/CommandLine/Text/HelpText.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ public static HelpText AutoBuild<T>(
269269

270270
var errors = Enumerable.Empty<Error>();
271271

272+
272273
if (onError != null && parserResult.Tag == ParserResultType.NotParsed)
273274
{
274275
errors = ((NotParsed<T>)parserResult).Errors;
275-
276-
if (errors.OnlyMeaningfulOnes().Any())
276+
if (errors.IsHelp() || errors.OnlyMeaningfulOnes().Any())
277277
auto = onError(auto);
278278
}
279279

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Linq;
3+
using CommandLine.Tests.Fakes;
4+
using CommandLine.Text;
5+
using FluentAssertions;
6+
using Xunit;
7+
8+
namespace CommandLine.Tests.Unit.Text
9+
{
10+
public class HelpTextAutoBuildFix
11+
{
12+
13+
[Fact]
14+
public void HelpText_wit_AdditionalNewLineAfterOption_true_should_have_newline()
15+
{
16+
// Fixture setup
17+
// Exercize system
18+
var sut = new HelpText { AdditionalNewLineAfterOption = true }
19+
.AddOptions(new NotParsed<Simple_Options>(TypeInfo.Create(typeof(Simple_Options)),
20+
Enumerable.Empty<Error>()));
21+
22+
// Verify outcome
23+
24+
var lines = sut.ToString().ToLines();
25+
26+
lines[2].Should().BeEquivalentTo(" stringvalue Define a string value here.");
27+
lines[3].Should().BeEquivalentTo(String.Empty);
28+
lines[4].Should().BeEquivalentTo(" s, shortandlong Example with both short and long name.");
29+
lines[5].Should().BeEquivalentTo(String.Empty);
30+
lines[7].Should().BeEquivalentTo(String.Empty);
31+
lines[9].Should().BeEquivalentTo(String.Empty);
32+
lines[11].Should().BeEquivalentTo(String.Empty);
33+
lines[13].Should().BeEquivalentTo(String.Empty);
34+
lines[14].Should().BeEquivalentTo(" value pos. 0 Define a long value here.");
35+
// Teardown
36+
}
37+
38+
[Fact]
39+
public void HelpText_wit_AdditionalNewLineAfterOption_false_should_not_have_newline()
40+
{
41+
// Fixture setup
42+
// Exercize system
43+
var sut = new HelpText { AdditionalNewLineAfterOption = false }
44+
.AddOptions(new NotParsed<Simple_Options>(TypeInfo.Create(typeof(Simple_Options)),
45+
Enumerable.Empty<Error>()));
46+
47+
// Verify outcome
48+
49+
var lines = sut.ToString().ToLines();
50+
51+
lines[2].Should().BeEquivalentTo(" stringvalue Define a string value here.");
52+
53+
lines[3].Should().BeEquivalentTo(" s, shortandlong Example with both short and long name.");
54+
lines[8].Should().BeEquivalentTo(" value pos. 0 Define a long value here.");
55+
// Teardown
56+
}
57+
[Fact]
58+
public void HelpText_wit_by_default_should_include_help_version_option()
59+
{
60+
// Fixture setup
61+
// Exercize system
62+
var sut = new HelpText ()
63+
.AddOptions(new NotParsed<Simple_Options>(TypeInfo.Create(typeof(Simple_Options)),
64+
Enumerable.Empty<Error>()));
65+
66+
// Verify outcome
67+
68+
var lines = sut.ToString().ToNotEmptyLines();
69+
lines.Should().HaveCount(c => c ==7);
70+
lines.Should().Contain(" help Display more information on a specific command.");
71+
lines.Should().Contain(" version Display version information.");
72+
// Teardown
73+
}
74+
75+
[Fact]
76+
public void HelpText_wit_AutoHelp_false_should_hide_help_option()
77+
{
78+
// Fixture setup
79+
// Exercize system
80+
var sut = new HelpText { AutoHelp = false,AutoVersion = false}
81+
.AddOptions(new NotParsed<Simple_Options>(TypeInfo.Create(typeof(Simple_Options)),
82+
Enumerable.Empty<Error>()));
83+
84+
// Verify outcome
85+
86+
var lines = sut.ToString().ToNotEmptyLines();
87+
lines.Should().HaveCount(c => c ==5);
88+
lines.Should().NotContain(" help Display more information on a specific command.");
89+
lines.Should().NotContain(" version Display version information.");
90+
// Teardown
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)