Skip to content

Commit 1de518f

Browse files
committed
move IsHelp/Isversion to separate file with scope public
1 parent ee30c24 commit 1de518f

File tree

3 files changed

+97
-159
lines changed

3 files changed

+97
-159
lines changed

src/CommandLine/ErrorExtensions.cs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
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;
43
using System.Collections.Generic;
54
using System.Linq;
65
using CommandLine.Core;
@@ -24,31 +23,6 @@ public static IEnumerable<Error> OnlyMeaningfulOnes(this IEnumerable<Error> erro
2423
.Where(e => !(e.Tag == ErrorType.UnknownOptionError
2524
&& ((UnknownOptionError)e).Token.EqualsOrdinalIgnoreCase("help")));
2625
}
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-
}
26+
5327
}
5428
}

src/CommandLine/HelpTextExtensions.cs

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

68
namespace CommandLine.Tests.Unit.Text
79
{
8-
public class HelpTextTests2
10+
public class HelpTextAutoBuildFix
911
{
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"});
1712

18-
result .WithNotParsed(errs =>
19-
{
20-
errs.IsHelp().Should().BeTrue();
21-
errs.IsVersion().Should().BeFalse();
22-
});
23-
}
2413
[Fact]
25-
public static void error_isVersion()
14+
public void HelpText_wit_AdditionalNewLineAfterOption_true_should_have_newline()
2615
{
2716
// Fixture setup
28-
// Exercize system
29-
var parser = new Parser(x => x.HelpWriter = null);
30-
var result = parser.ParseArguments<Simple_Options>(new[]{"--version"});
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>()));
3121

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"});
22+
// Verify outcome
4623

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-
});
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
6636
}
6737

6838
[Fact]
69-
public static void custom_helptext_with_AdditionalNewLineAfterOption_true()
39+
public void HelpText_wit_AdditionalNewLineAfterOption_false_should_not_have_newline()
7040
{
7141
// Fixture setup
72-
// Exercize system
73-
var parser = new Parser(x => x.HelpWriter = null);
74-
var result = parser.ParseArguments<Simple_Options>(new[]{"--help"});
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>()));
7546

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-
}
47+
// Verify outcome
48+
49+
var lines = sut.ToString().ToLines();
50+
51+
lines[2].Should().BeEquivalentTo(" stringvalue Define a string value here.");
9552

96-
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+
}
9757
[Fact]
98-
public static void custom_helptext_with_parser_autohelp_false_and_AdditionalNewLineAfterOption_false()
58+
public void HelpText_wit_by_default_should_include_help_version_option()
9959
{
10060
// 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-
});
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
13073
}
13174

13275
[Fact]
133-
public static void custom_helptext_with_autohelp_false()
76+
public void HelpText_wit_AutoHelp_false_should_hide_help_option()
13477
{
13578
// 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"});
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
14485

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-
});
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
16291
}
16392
}
16493
}

0 commit comments

Comments
 (0)