Skip to content

Commit ee30c24

Browse files
committed
Fix issue #259 for HelpText.AutoBuild configuration
1 parent e9340b0 commit ee30c24

File tree

3 files changed

+195
-2
lines changed

3 files changed

+195
-2
lines changed

src/CommandLine/ErrorExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
22

3+
using System;
34
using System.Collections.Generic;
45
using System.Linq;
56
using CommandLine.Core;
@@ -23,5 +24,31 @@ public static IEnumerable<Error> OnlyMeaningfulOnes(this IEnumerable<Error> erro
2324
.Where(e => !(e.Tag == ErrorType.UnknownOptionError
2425
&& ((UnknownOptionError)e).Token.EqualsOrdinalIgnoreCase("help")));
2526
}
27+
/// <summary>
28+
/// return true when errors contain HelpXXXError
29+
/// </summary>
30+
public static bool IsHelp (this IEnumerable<Error> errs)
31+
{
32+
if (errs.Any(x=>x.Tag == ErrorType.HelpRequestedError ||
33+
x.Tag == ErrorType.HelpVerbRequestedError))
34+
return true;
35+
//when AutoHelp=false in parser, help is disabled and Parser raise UnknownOptionError
36+
if( errs.Any(x=> (x is UnknownOptionError ee ? ee.Token:"") == "help"))
37+
return true;
38+
return false;
39+
}
40+
41+
/// <summary>
42+
/// return true when errors contain VersionXXXError
43+
/// </summary>
44+
public static bool IsVersion (this IEnumerable<Error> errs)
45+
{
46+
if (errs.Any(x=>x.Tag == ErrorType.VersionRequestedError ))
47+
return true;
48+
//when AutoVersion=false in parser, Version is disabled and Parser raise UnknownOptionError
49+
if( errs.Any(x=> (x is UnknownOptionError ee ? ee.Token:"") == "version"))
50+
return true;
51+
return false;
52+
}
2653
}
2754
}

src/CommandLine/Text/HelpText.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,13 @@ 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())
277+
auto = onError(auto);
278+
else if (errors.OnlyMeaningfulOnes().Any())
277279
auto = onError(auto);
278280
}
279281

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using CommandLine.Tests.Fakes;
2+
using CommandLine.Text;
3+
using FluentAssertions;
4+
using Xunit;
5+
6+
namespace CommandLine.Tests.Unit.Text
7+
{
8+
public class HelpTextTests2
9+
{
10+
[Fact]
11+
public static void error_ishelp()
12+
{
13+
// Fixture setup
14+
// Exercize system
15+
var parser = new Parser(x => x.HelpWriter = null);
16+
var result = parser.ParseArguments<Simple_Options>(new[]{"--help"});
17+
18+
result .WithNotParsed(errs =>
19+
{
20+
errs.IsHelp().Should().BeTrue();
21+
errs.IsVersion().Should().BeFalse();
22+
});
23+
}
24+
[Fact]
25+
public static void error_isVersion()
26+
{
27+
// Fixture setup
28+
// Exercize system
29+
var parser = new Parser(x => x.HelpWriter = null);
30+
var result = parser.ParseArguments<Simple_Options>(new[]{"--version"});
31+
32+
result .WithNotParsed(errs =>
33+
{
34+
errs.IsHelp().Should().BeFalse();
35+
errs.IsVersion().Should().BeTrue();
36+
});
37+
}
38+
39+
[Fact]
40+
public static void custom_helptext_with_AdditionalNewLineAfterOption_false()
41+
{
42+
// Fixture setup
43+
// Exercize system
44+
var parser = new Parser(x => x.HelpWriter = null);
45+
var result = parser.ParseArguments<Simple_Options>(new[]{"--help"});
46+
47+
result .WithNotParsed(errs =>
48+
{
49+
50+
var sut = HelpText.AutoBuild(result,
51+
h =>
52+
{
53+
h.AdditionalNewLineAfterOption = false;
54+
return h;
55+
}
56+
, e => e);
57+
//Assert
58+
var expected = new[]
59+
{
60+
" --help Display this help screen.",
61+
" --version Display version information."
62+
};
63+
var lines = sut.ToString().ToLines();
64+
lines.Should().ContainInOrder(expected);
65+
});
66+
}
67+
68+
[Fact]
69+
public static void custom_helptext_with_AdditionalNewLineAfterOption_true()
70+
{
71+
// Fixture setup
72+
// Exercize system
73+
var parser = new Parser(x => x.HelpWriter = null);
74+
var result = parser.ParseArguments<Simple_Options>(new[]{"--help"});
75+
76+
result .WithNotParsed(errs =>
77+
{
78+
79+
var sut = HelpText.AutoBuild(result,
80+
h =>h //AdditionalNewLineAfterOption =true by default
81+
, e => e);
82+
83+
//Assert
84+
var expected = new[]
85+
{
86+
string.Empty,
87+
" --help Display this help screen.",
88+
string.Empty,
89+
" --version Display version information."
90+
};
91+
var lines = sut.ToString().ToLines();
92+
lines.Should().ContainInOrder(expected);
93+
});
94+
}
95+
96+
97+
[Fact]
98+
public static void custom_helptext_with_parser_autohelp_false_and_AdditionalNewLineAfterOption_false()
99+
{
100+
// Fixture setup
101+
// Exercize system
102+
var parser = new Parser(x =>
103+
{
104+
x.HelpWriter = null;
105+
x.AutoHelp=false;
106+
//x.AutoVersion=false;
107+
});
108+
var result = parser.ParseArguments<Simple_Options>(new[]{"--help"});
109+
//you could generate help even parser.AutoHelp is disabled
110+
result .WithNotParsed(errs =>
111+
{
112+
errs.IsHelp().Should().BeTrue();
113+
var sut = HelpText.AutoBuild(result,
114+
h =>
115+
{
116+
h.AdditionalNewLineAfterOption = false;
117+
return h;
118+
}
119+
, e => e);
120+
121+
//Assert
122+
var expected = new[]
123+
{
124+
" --help Display this help screen.",
125+
" --version Display version information."
126+
};
127+
var lines = sut.ToString().ToLines();
128+
lines.Should().ContainInOrder(expected);
129+
});
130+
}
131+
132+
[Fact]
133+
public static void custom_helptext_with_autohelp_false()
134+
{
135+
// Fixture setup
136+
// Exercize system
137+
var parser = new Parser(x =>
138+
{
139+
x.HelpWriter = null;
140+
x.AutoHelp=false;
141+
//x.AutoVersion=false;
142+
});
143+
var result = parser.ParseArguments<Simple_Options>(new[]{"--help"});
144+
145+
result .WithNotParsed(errs =>
146+
{
147+
errs.IsHelp().Should().BeTrue();
148+
var sut = HelpText.AutoBuild(result,
149+
h =>h,e => e);
150+
151+
//Assert
152+
var expected = new[]
153+
{
154+
string.Empty,
155+
" --help Display this help screen.",
156+
string.Empty,
157+
" --version Display version information."
158+
};
159+
var lines = sut.ToString().ToLines();
160+
lines.Should().ContainInOrder(expected);
161+
});
162+
}
163+
}
164+
}

0 commit comments

Comments
 (0)