Skip to content

Commit 42b8994

Browse files
committed
refactor: obsolete throwOnUnexpectedArg in favor of UnrecognizedArgumentHandling enum
1 parent 11e2079 commit 42b8994

25 files changed

+433
-105
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22

3-
[Unreleased changes](https://github.com/natemcmaster/CommandLineUtils/compare/v2.5.0...HEAD):
3+
[Unreleased changes](https://github.com/natemcmaster/CommandLineUtils/compare/v2.5.1...HEAD):
4+
5+
## [v2.6.0](https://github.com/natemcmaster/CommandLineUtils/compare/v2.5.1...v2.6.0)
6+
7+
* Refactor: obsolete throwOnUnexpectedArg in favor of UnrecognizedArgumentHandling. See https://github.com/natemcmaster/CommandLineUtils/issues/339 for details
48

59
## [v2.5.1](https://github.com/natemcmaster/CommandLineUtils/compare/v2.5.0...v2.5.1)
610

docs/docs/arguments.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ or the Windows command `cmd` take some arguments, and pass the rest on to the co
145145
> In this example, `-l` is an option on `time`. This starts a timer which then invokes `ls` with additional arguments.
146146
> `-l` is also an option on `ls`.
147147
148-
Normally, unrecognized arguments is an error. You must set ThrowOnUnexpectedArgument to `false` to allow the parser
149-
to allow unrecognized arguments and options.
148+
Normally, unrecognized arguments is an error. You must set `UnrecognizedArgumentHandling` to `StopParsingAndCollect` to allow the parser
149+
to collect unrecognized arguments and options.
150150
151151
### The double-dash convention `--`
152152
@@ -171,6 +171,6 @@ to include all values. See @McMaster.Extensions.CommandLineUtils.Conventions.Rem
171171
172172
### [Using Builder API](#tab/using-builder-api)
173173
174-
When `throwOnUnexpctedArg` is set to false,
174+
When `UnrecognizedArgumentHandling` is set to `Stop`,
175175
176176
[!code-csharp[Program](../samples/passthru-args/builder-api/Program.cs)]

docs/samples/passthru-args/attributes/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.Linq;
44
using McMaster.Extensions.CommandLineUtils;
55

6-
[Command(ThrowOnUnexpectedArgument = false, AllowArgumentSeparator = true)]
6+
[Command(
7+
UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.StopParsingAndCollect,
8+
AllowArgumentSeparator = true)]
79
public class Program
810
{
911
public static int Main(string[] args) => CommandLineApplication.Execute<Program>(args);

docs/samples/passthru-args/builder-api/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ public class Program
77
{
88
public static int Main(string[] args)
99
{
10-
var app = new CommandLineApplication(throwOnUnexpectedArg: false)
10+
var app = new CommandLineApplication
1111
{
12-
AllowArgumentSeparator = true
12+
AllowArgumentSeparator = true,
13+
UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.StopParsingAndCollect,
1314
};
1415

1516
var showMilliseconds = app.Option<int>("-m", "Show time in milliseconds", CommandOptionType.NoValue);

docs/samples/subcommands/nested-types/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private void OnExecute(IConsole console)
5757

5858
[Command("run", Description = "Run a command in a new container",
5959
AllowArgumentSeparator = true,
60-
ThrowOnUnexpectedArgument = false)]
60+
UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.StopParsingAndCollect)]
6161
private class Run
6262
{
6363
[Required(ErrorMessage = "You must specify the image name")]
@@ -94,7 +94,7 @@ private int OnExecute(IConsole console)
9494

9595

9696
[Command("ls", Description = "List images",
97-
ThrowOnUnexpectedArgument = false)]
97+
UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.StopParsingAndCollect)]
9898
private class List
9999
{
100100
[Option(Description = "Show all containers (default shows just running)")]

src/CommandLineUtils/Attributes/CommandAttribute.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.ComponentModel;
67
using System.Globalization;
78
using System.Linq;
89

@@ -90,10 +91,30 @@ public string? Name
9091
public string? ExtendedHelpText { get; set; }
9192

9293
/// <summary>
94+
/// <para>
95+
/// This property is obsolete and will be removed in a future version.
96+
/// The recommended replacement is <seealso cref="UnrecognizedArgumentHandling"/>.
97+
/// </para>
98+
/// <para>
9399
/// Throw when unexpected arguments are encountered.
100+
/// </para>
94101
/// </summary>
95102
/// <seealso cref="CommandLineApplication.ThrowOnUnexpectedArgument"/>
96-
public bool ThrowOnUnexpectedArgument { get; set; } = true;
103+
[Obsolete("This property is obsolete and will be removed in a future version. " +
104+
"The recommended replacement is UnrecognizedArgumentHandling.")]
105+
[EditorBrowsable(EditorBrowsableState.Never)]
106+
public bool ThrowOnUnexpectedArgument
107+
{
108+
get => UnrecognizedArgumentHandling == UnrecognizedArgumentHandling.Throw;
109+
set => UnrecognizedArgumentHandling = value
110+
? UnrecognizedArgumentHandling.Throw
111+
: UnrecognizedArgumentHandling.StopParsingAndCollect;
112+
}
113+
114+
/// <summary>
115+
/// Set the behavior for how to handle unrecognized arguments.
116+
/// </summary>
117+
public UnrecognizedArgumentHandling UnrecognizedArgumentHandling { get; set; } = UnrecognizedArgumentHandling.Throw;
97118

98119
/// <summary>
99120
/// Allow '--' to be used to stop parsing arguments.
@@ -167,7 +188,7 @@ internal void Configure(CommandLineApplication app)
167188
app.FullName = FullName;
168189
app.ResponseFileHandling = ResponseFileHandling;
169190
app.ShowInHelpText = ShowInHelpText;
170-
app.ThrowOnUnexpectedArgument = ThrowOnUnexpectedArgument;
191+
app.UnrecognizedArgumentHandling = UnrecognizedArgumentHandling;
171192
app.OptionsComparison = OptionsComparison;
172193
app.ValueParsers.ParseCulture = ParseCulture;
173194

0 commit comments

Comments
 (0)