Skip to content

Commit bf2710b

Browse files
committed
Remove StopParsing options
After further consideration, these were fairly obtuse APIs only really needed internally for the parser
1 parent d1b6b5b commit bf2710b

File tree

9 files changed

+47
-118
lines changed

9 files changed

+47
-118
lines changed

releasenotes.props

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ New API
1414
- Added OptionAttribute, ArgumentAttribute, CommandAttribute, SubcommandAttribute, HelpOptionAttribute, and VersionOptionAttribute.
1515
- CommandLineApplication.Execute<TApp>() - executes an app where TApp uses attributes to define its options
1616
- CommandLineApplication.ExecuteAsync<TApp>() - sample thing, but async.
17-
- CommandLineApplication.StopParsingHelpOption and StopParsingVerboseOption. When the help and verbose options are matched
18-
against a command-line flag, the parsing will stop by default. You can turn this off by setting these options if you
19-
want OnExecute to be invoked no matter what.
2017
- CommandLineApplication.ResponseFileHandling - the parser can treat arguments that begin with '@' as response files.
2118
Response files contain arguments that will be treated as if they were passed on command line.
2219

src/CommandLineUtils/Attributes/CommandAttribute.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,6 @@ public CommandAttribute(string name)
6767
/// </summary>
6868
public ResponseFileHandling ResponseFileHandling { get; set; } = ResponseFileHandling.Disabled;
6969

70-
/// <summary>
71-
/// Stops the parsing argument when <see cref="HelpOptionAttribute"/> is matched. Defaults to <c>true</c>.
72-
/// This will prevent any <c>OnExecute</c> methods from being called.
73-
/// </summary>
74-
public bool StopParsingAfterHelpOption { get; set; } = true;
75-
76-
/// <summary>
77-
/// Stops the parsing argument when <see cref="VersionOptionAttribute"/> is matched. Defaults to <c>true</c>.
78-
/// This will prevent any <c>OnExecute</c> methods from being called.
79-
/// </summary>
80-
public bool StopParsingAfterVersionOption { get; set; } = true;
81-
8270
internal void Configure(CommandLineApplication app)
8371
{
8472
// this might have been set from SubcommandAttribute
@@ -90,8 +78,6 @@ internal void Configure(CommandLineApplication app)
9078
app.FullName = FullName;
9179
app.ResponseFileHandling = ResponseFileHandling;
9280
app.ShowInHelpText = ShowInHelpText;
93-
app.StopParsingAfterHelpOption = StopParsingAfterHelpOption;
94-
app.StopParsingAfterVersionOption = StopParsingAfterVersionOption;
9581
app.ThrowOnUnexpectedArgument = ThrowOnUnexpectedArgument;
9682
}
9783
}

src/CommandLineUtils/CommandLineApplication.Execute.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static int Execute<TApp>(IConsole console, params string[] args)
4646
where TApp : class, new()
4747
{
4848
var bindResult = Bind<TApp>(console, args);
49-
if (IsShowingInfo(bindResult))
49+
if (bindResult.Command.IsShowingInformation)
5050
{
5151
return 0;
5252
}
@@ -99,7 +99,7 @@ public static async Task<int> ExecuteAsync<TApp>(IConsole console, params string
9999
where TApp : class, new()
100100
{
101101
var bindResult = Bind<TApp>(console, args);
102-
if (IsShowingInfo(bindResult))
102+
if (bindResult.Command.IsShowingInformation)
103103
{
104104
return 0;
105105
}
@@ -155,23 +155,5 @@ private static int HandleValidationError(IConsole console, BindResult bindResult
155155
var applicationBuilder = new ReflectionAppBuilder<TApp>();
156156
return applicationBuilder.Bind(console, args);
157157
}
158-
159-
private static bool IsShowingInfo(BindResult bindResult)
160-
{
161-
if (bindResult.Command.IsShowingInformation)
162-
{
163-
if (bindResult.Command.OptionHelp?.HasValue() == true && bindResult.Command.StopParsingAfterHelpOption)
164-
{
165-
return true;
166-
}
167-
168-
if (bindResult.Command.OptionVersion?.HasValue() == true && bindResult.Command.StopParsingAfterVersionOption)
169-
{
170-
return true;
171-
}
172-
}
173-
174-
return false;
175-
}
176158
}
177159
}

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ private CommandLineApplication(CommandLineApplication parent, string name, bool
8686
{
8787
Name = name;
8888
Parent = parent;
89-
StopParsingAfterHelpOption = parent.StopParsingAfterHelpOption;
90-
StopParsingAfterVersionOption = parent.StopParsingAfterVersionOption;
9189
}
9290

9391
/// <summary>
@@ -166,18 +164,6 @@ public IHelpTextGenerator HelpTextGenerator
166164
/// </summary>
167165
public bool IsShowingInformation { get; protected set; }
168166

169-
/// <summary>
170-
/// Stops the parsing argument when <see cref="OptionHelp"/> is matched. Defaults to <c>true</c>.
171-
/// This will prevent <see cref="Invoke" /> or <see cref="ValidationErrorHandler" /> from being called.
172-
/// </summary>
173-
public bool StopParsingAfterHelpOption { get; set; } = true;
174-
175-
/// <summary>
176-
/// Stops the parsing argument when <see cref="OptionVersion"/> is matched. Defaults to <c>true</c>.
177-
/// This will prevent <see cref="Invoke" /> or <see cref="ValidationErrorHandler" /> from being called.
178-
/// </summary>
179-
public bool StopParsingAfterVersionOption { get; set; } = true;
180-
181167
/// <summary>
182168
/// The action to call when this command is matched and <see cref="IsShowingInformation"/> is <c>false</c>.
183169
/// </summary>

src/CommandLineUtils/Internal/CommandLineProcessor.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,13 @@ private bool ProcessOption()
162162
{
163163
_currentCommand.ShowHelp();
164164
option.TryParse(null);
165-
if (_currentCommand.StopParsingAfterHelpOption)
166-
{
167-
return false;
168-
}
165+
return false;
169166
}
170167
else if (_currentCommand.OptionVersion == option)
171168
{
172169
_currentCommand.ShowVersion();
173170
option.TryParse(null);
174-
if (_currentCommand.StopParsingAfterVersionOption)
175-
{
176-
return false;
177-
}
171+
return false;
178172
}
179173

180174
if (arg.Value != null)

test/CommandLineUtils.Tests/HelpOptionAttributeTests.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -147,26 +147,5 @@ public void OnExecuteIsNotInvokedWhenHelpOptionSpecified(string arg)
147147
{
148148
Assert.Equal(0, CommandLineApplication.Execute<SimpleHelpApp>(new TestConsole(_output), arg));
149149
}
150-
151-
[Command(StopParsingAfterHelpOption = false)]
152-
private class KeepParsingHelpApp
153-
{
154-
[HelpOption]
155-
public bool IsHelp { get; }
156-
157-
private int OnExecute()
158-
{
159-
return 15;
160-
}
161-
}
162-
163-
[Theory]
164-
[InlineData("-h")]
165-
[InlineData("-?")]
166-
[InlineData("--help")]
167-
public void InvokesOnExecuteWhenStopParsingHelpDisabled(string arg)
168-
{
169-
Assert.Equal(15, CommandLineApplication.Execute<KeepParsingHelpApp>(new TestConsole(_output), arg));
170-
}
171150
}
172151
}

test/CommandLineUtils.Tests/SubCommandTests.cs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.IO;
56
using System.Reflection;
7+
using System.Text;
68
using Xunit;
79
using Xunit.Abstractions;
810

@@ -17,14 +19,14 @@ public SubcommandTests(ITestOutputHelper output)
1719
_output = output;
1820
}
1921

20-
[Command(StopParsingAfterHelpOption = false)]
22+
[Command(Name = "master")]
2123
[Subcommand("level1", typeof(Level1Cmd))]
2224
private class MasterApp : CommandBase
2325
{
2426
[Option(Inherited = true)]
2527
public bool Verbose { get; set; }
2628

27-
protected override int OnExecute(CommandLineApplication app) => IsHelp ? 100 : 0;
29+
protected override int OnExecute(CommandLineApplication app) => 100;
2830
}
2931

3032
[Subcommand("level2", typeof(Level2Cmd))]
@@ -33,7 +35,7 @@ private class Level1Cmd : CommandBase
3335
[Option("--mid")]
3436
public bool Mid { get; }
3537

36-
protected override int OnExecute(CommandLineApplication app) => IsHelp ? 101 : 1;
38+
protected override int OnExecute(CommandLineApplication app) => 101;
3739

3840
public MasterApp Parent { get; }
3941
}
@@ -43,10 +45,8 @@ private class Level2Cmd : CommandBase
4345
[Option("--value")]
4446
public int? Value { get; set; }
4547

46-
protected override int OnExecute(CommandLineApplication app) =>
47-
IsHelp
48-
? 102
49-
: Value.HasValue ? Value.Value : 2;
48+
protected override int OnExecute(CommandLineApplication app)
49+
=> Value.HasValue ? Value.Value : 102;
5050

5151
public Level1Cmd Parent { get; }
5252
}
@@ -66,41 +66,65 @@ public void ItInvokesExecuteOnSubcommand_Bottom()
6666
{
6767
var rc = CommandLineApplication.Execute<MasterApp>(new TestConsole(_output), "level1", "level2", "--value", "6");
6868
Assert.Equal(6, rc);
69+
70+
rc = CommandLineApplication.Execute<MasterApp>(new TestConsole(_output), "level1", "level2");
71+
Assert.Equal(102, rc);
6972
}
7073

7174
[Fact]
7275
public void ItInvokesExecuteOnSubcommand_Middle()
7376
{
7477
var rc = CommandLineApplication.Execute<MasterApp>(new TestConsole(_output), "level1");
75-
Assert.Equal(1, rc);
78+
Assert.Equal(101, rc);
7679
}
7780

7881
[Fact]
7982
public void ItInvokesExecuteOnSubcommand_Top()
8083
{
8184
var rc = CommandLineApplication.Execute<MasterApp>(new TestConsole(_output));
82-
Assert.Equal(0, rc);
85+
Assert.Equal(100, rc);
8386
}
8487

8588
[Fact]
8689
public void HandlesHelp_Bottom()
8790
{
88-
var rc = CommandLineApplication.Execute<MasterApp>(new TestConsole(_output), "level1", "level2", "--help");
89-
Assert.Equal(102, rc);
91+
var sb = new StringBuilder();
92+
var output = new StringWriter(sb);
93+
var console = new TestConsole(_output)
94+
{
95+
Out = output,
96+
};
97+
var rc = CommandLineApplication.Execute<MasterApp>(console, "level1", "level2", "--help");
98+
Assert.Equal(0, rc);
99+
Assert.Contains("Usage: master level1 level2 [options]", sb.ToString());
90100
}
91101

92102
[Fact]
93103
public void HandlesHelp_Middle()
94104
{
95-
var rc = CommandLineApplication.Execute<MasterApp>(new TestConsole(_output), "level1", "--help");
96-
Assert.Equal(101, rc);
105+
var sb = new StringBuilder();
106+
var output = new StringWriter(sb);
107+
var console = new TestConsole(_output)
108+
{
109+
Out = output,
110+
};
111+
var rc = CommandLineApplication.Execute<MasterApp>(console, "level1", "--help");
112+
Assert.Equal(0, rc);
113+
Assert.Contains("Usage: master level1 [options]", sb.ToString());
97114
}
98115

99116
[Fact]
100117
public void HandlesHelp_Top()
101118
{
102-
var rc = CommandLineApplication.Execute<MasterApp>(new TestConsole(_output), "--help");
103-
Assert.Equal(100, rc);
119+
var sb = new StringBuilder();
120+
var output = new StringWriter(sb);
121+
var console = new TestConsole(_output)
122+
{
123+
Out = output,
124+
};
125+
var rc = CommandLineApplication.Execute<MasterApp>(console, "--help");
126+
Assert.Equal(0, rc);
127+
Assert.Contains("Usage: master [options]", sb.ToString());
104128
}
105129

106130
[Fact]

test/CommandLineUtils.Tests/Utilities/TestConsole.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ namespace McMaster.Extensions.CommandLineUtils.Tests
99
{
1010
public class TestConsole : IConsole
1111
{
12-
private readonly ITestOutputHelper _output;
13-
1412
public TestConsole(ITestOutputHelper output)
1513
{
16-
_output = output;
14+
Out = new XunitTextWriter(output);
15+
Error = new XunitTextWriter(output);
1716
}
1817

19-
public TextWriter Out => new XunitTextWriter(_output);
18+
public TextWriter Out { get; set; }
2019

21-
public TextWriter Error => new XunitTextWriter(_output);
20+
public TextWriter Error { get; set; }
2221

2322
public TextReader In => throw new NotImplementedException();
2423

test/CommandLineUtils.Tests/VersionOptionAttributeTests.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,5 @@ public void OnExecuteIsNotInvokedWhenVersionOptionSpecified(string arg)
164164
{
165165
Assert.Equal(0, CommandLineApplication.Execute<SimpleVersionApp>(new TestConsole(_output), arg));
166166
}
167-
168-
[Command(StopParsingAfterVersionOption = false)]
169-
private class KeepParsingVersionApp
170-
{
171-
[VersionOption("1.0.0")]
172-
public bool IsVersion { get; }
173-
174-
private int OnExecute()
175-
{
176-
return 14;
177-
}
178-
}
179-
180-
[Fact]
181-
public void InvokesOnExecuteWhenStopParsingVersionDisabled()
182-
{
183-
Assert.Equal(14, CommandLineApplication.Execute<KeepParsingVersionApp>(new TestConsole(_output), "--version"));
184-
}
185167
}
186168
}

0 commit comments

Comments
 (0)