Skip to content

Commit

Permalink
refactor: obsolete throwOnUnexpectedArg in favor of UnrecognizedArgum…
Browse files Browse the repository at this point in the history
…entHandling enum
  • Loading branch information
natemcmaster committed Feb 9, 2020
1 parent 11e2079 commit f88fad8
Show file tree
Hide file tree
Showing 25 changed files with 431 additions and 105 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

[Unreleased changes](https://github.com/natemcmaster/CommandLineUtils/compare/v2.5.0...HEAD):
[Unreleased changes](https://github.com/natemcmaster/CommandLineUtils/compare/v2.5.1...HEAD):

* Refactor: obsolete throwOnUnexpectedArg in favor of UnrecognizedArgumentHandling. See https://github.com/natemcmaster/CommandLineUtils/issues/339 for details

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

Expand Down
6 changes: 3 additions & 3 deletions docs/docs/arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ or the Windows command `cmd` take some arguments, and pass the rest on to the co
> In this example, `-l` is an option on `time`. This starts a timer which then invokes `ls` with additional arguments.
> `-l` is also an option on `ls`.
Normally, unrecognized arguments is an error. You must set ThrowOnUnexpectedArgument to `false` to allow the parser
to allow unrecognized arguments and options.
Normally, unrecognized arguments is an error. You must set `UnrecognizedArgumentHandling` to `StopParsingAndCollect` to allow the parser
to collect unrecognized arguments and options.
### The double-dash convention `--`
Expand All @@ -171,6 +171,6 @@ to include all values. See @McMaster.Extensions.CommandLineUtils.Conventions.Rem
### [Using Builder API](#tab/using-builder-api)
When `throwOnUnexpctedArg` is set to false,
When `UnrecognizedArgumentHandling` is set to `Stop`,
[!code-csharp[Program](../samples/passthru-args/builder-api/Program.cs)]
4 changes: 3 additions & 1 deletion docs/samples/passthru-args/attributes/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using System.Linq;
using McMaster.Extensions.CommandLineUtils;

[Command(ThrowOnUnexpectedArgument = false, AllowArgumentSeparator = true)]
[Command(
UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.StopParsingAndCollect,
AllowArgumentSeparator = true)]
public class Program
{
public static int Main(string[] args) => CommandLineApplication.Execute<Program>(args);
Expand Down
5 changes: 3 additions & 2 deletions docs/samples/passthru-args/builder-api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ public class Program
{
public static int Main(string[] args)
{
var app = new CommandLineApplication(throwOnUnexpectedArg: false)
var app = new CommandLineApplication
{
AllowArgumentSeparator = true
AllowArgumentSeparator = true,
UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.StopParsingAndCollect,
};

var showMilliseconds = app.Option<int>("-m", "Show time in milliseconds", CommandOptionType.NoValue);
Expand Down
4 changes: 2 additions & 2 deletions docs/samples/subcommands/nested-types/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void OnExecute(IConsole console)

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


[Command("ls", Description = "List images",
ThrowOnUnexpectedArgument = false)]
UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.StopParsingAndCollect)]
private class List
{
[Option(Description = "Show all containers (default shows just running)")]
Expand Down
25 changes: 23 additions & 2 deletions src/CommandLineUtils/Attributes/CommandAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;

Expand Down Expand Up @@ -90,10 +91,30 @@ public string? Name
public string? ExtendedHelpText { get; set; }

/// <summary>
/// <para>
/// This property is obsolete and will be removed in a future version.
/// The recommended replacement is <seealso cref="UnrecognizedArgumentHandling"/>.
/// </para>
/// <para>
/// Throw when unexpected arguments are encountered.
/// </para>
/// </summary>
/// <seealso cref="CommandLineApplication.ThrowOnUnexpectedArgument"/>
public bool ThrowOnUnexpectedArgument { get; set; } = true;
[Obsolete("This property is obsolete and will be removed in a future version. " +
"The recommended replacement is UnrecognizedArgumentHandling.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public bool ThrowOnUnexpectedArgument
{
get => UnrecognizedArgumentHandling == UnrecognizedArgumentHandling.Throw;
set => UnrecognizedArgumentHandling = value
? UnrecognizedArgumentHandling.Throw
: UnrecognizedArgumentHandling.StopParsingAndCollect;
}

/// <summary>
/// Set the behavior for how to handle unrecognized arguments.
/// </summary>
public UnrecognizedArgumentHandling UnrecognizedArgumentHandling { get; set; } = UnrecognizedArgumentHandling.Throw;

/// <summary>
/// Allow '--' to be used to stop parsing arguments.
Expand Down Expand Up @@ -167,7 +188,7 @@ internal void Configure(CommandLineApplication app)
app.FullName = FullName;
app.ResponseFileHandling = ResponseFileHandling;
app.ShowInHelpText = ShowInHelpText;
app.ThrowOnUnexpectedArgument = ThrowOnUnexpectedArgument;
app.UnrecognizedArgumentHandling = UnrecognizedArgumentHandling;
app.OptionsComparison = OptionsComparison;
app.ValueParsers.ParseCulture = ParseCulture;

Expand Down
Loading

0 comments on commit f88fad8

Please sign in to comment.