Skip to content

Commit 4f9d2f1

Browse files
authored
Enable help as a command/argument (#56)
1 parent 9a041a7 commit 4f9d2f1

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

src/Vertical.CommandLine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<PublicSign Condition="'$(OS)' != 'Windows_NT'">true</PublicSign>
1010
<Description>Simple command line argument mapping</Description>
1111
<Authors>Vertical Software contributors</Authors>
12-
<VersionPrefix>2.1.1</VersionPrefix>
12+
<VersionPrefix>2.2.0</VersionPrefix>
1313
<PackageId>vertical-commandline</PackageId>
1414
<PackageTags>commandline;utilities</PackageTags>
1515
<PackageProjectUrl>https://github.com/verticalsoftware/vertical-commandline</PackageProjectUrl>

src/Vertical/CommandLine/Configuration/ApplicationConfiguration.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ public ApplicationConfiguration<TOptions> Command<TCommandOptions>(string templa
8080
/// <returns>Configuration.</returns>
8181
public ApplicationConfiguration<TOptions> HelpOption(string template, IHelpWriter? helpWriter = null)
8282
{
83-
HelpTemplate = Template.ForOptionOrSwitch(template);
84-
83+
HelpTemplate = Template.Parse(template);
8584
_helpWriter = helpWriter;
8685
ParserConfig.AddTemplate(HelpTemplate);
8786
ParserConfig.AddParser(new HelpOptionParser<TOptions>(HelpTemplate));

src/Vertical/CommandLine/Parsing/Template.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ internal static Template ForCommand(string template)
5555
() => ConfigurationExceptions.InvalidCommandTemplate(template)));
5656
}
5757

58+
/// <summary>
59+
/// Creates either an option, switch, or command template.
60+
/// </summary>
61+
/// <param name="template">String to convert to template.</param>
62+
/// <returns><see cref="Template"/></returns>
63+
internal static Template Parse(string template)
64+
{
65+
return template.StartsWith("-")
66+
? ForOptionOrSwitch(template)
67+
: ForCommand(template);
68+
}
69+
5870
/// <inheritdoc />
5971
public override string ToString() => string.Join(TokenSeparator.ToString(),
6072
Tokens.Select(token => token.DistinguishedForm));

test/Parsing/HelpOptionParserTests.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,36 @@ public class HelpOptionParserTests
1818
private readonly IArgumentParser<object> _instanceUnderTest = new HelpOptionParser<object>(Template);
1919

2020
[Theory, MemberData(nameof(Theories))]
21-
public void ProcessContextReturnsExpected(string arg, ContextResult expected)
21+
public void ProcessContextReturnsExpected(string templateString, string arg, ContextResult expected)
2222
{
23+
var instanceUnderTest = new HelpOptionParser<object>(Template.Parse(templateString));
2324
var context = new ParseContext(new[]{arg});
24-
var result = _instanceUnderTest.ProcessContext(null, context);
25+
var result = instanceUnderTest.ProcessContext(new object(), context);
2526

2627
result.ShouldBe(expected);
2728
}
2829

2930
// ReSharper disable once MemberCanBePrivate.Global
3031
public static IEnumerable<object[]> Theories => Scenarios
3132
(
32-
Scenario("help", ContextResult.NoMatch),
33-
Scenario("file.txt", ContextResult.NoMatch),
34-
Scenario("-h", ContextResult.Help),
35-
Scenario( "--help", ContextResult.Help)
33+
Scenario("-h|--help", "help", ContextResult.NoMatch),
34+
Scenario("-h|--help", "file.txt", ContextResult.NoMatch),
35+
Scenario("-h|--help", "-h", ContextResult.Help),
36+
Scenario("-h|--help", "--help", ContextResult.Help),
37+
Scenario("help", "file.txt", ContextResult.NoMatch),
38+
Scenario("help", "help", ContextResult.Help)
3639
);
3740

3841
[Fact]
39-
public void ParserTypeReturnsHelp() => _instanceUnderTest.ParserType.ShouldBe(ParserType.Help);
42+
public void OptionParserTypeReturnsHelp() => new HelpOptionParser<object>(Template.ForOptionOrSwitch("-h|--help"))
43+
.ParserType
44+
.ShouldBe(ParserType.Help);
45+
46+
47+
[Fact]
48+
public void ArgumentParserTypeReturnsHelp() => new HelpOptionParser<object>(Template.ForCommand("help"))
49+
.ParserType
50+
.ShouldBe(ParserType.Help);
4051

4152
[Fact]
4253
public void ParserMultiValuedReturnsFalse() => _instanceUnderTest.MultiValued.ShouldBe(false);

0 commit comments

Comments
 (0)