Skip to content

Commit 4b60585

Browse files
authored
[1653] remove the legacy double dash behavior and ParseResult.UnparsedTokens (#1806)
* [1653] removed LegacyDoubleDashBehavior; removed ParseResult.UnparsedTokens * [1653] - removed ParseRemainingTokens method
1 parent f1f43df commit 4b60585

File tree

16 files changed

+37
-201
lines changed

16 files changed

+37
-201
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ System.CommandLine
7979
public static CommandLineBuilder AddMiddleware(this CommandLineBuilder builder, System.Action<System.CommandLine.Invocation.InvocationContext> onInvoke, System.CommandLine.Invocation.MiddlewareOrder order = Default)
8080
public static CommandLineBuilder CancelOnProcessTermination(this CommandLineBuilder builder, System.Nullable<System.TimeSpan> timeout = null)
8181
public static CommandLineBuilder EnableDirectives(this CommandLineBuilder builder, System.Boolean value = True)
82-
public static CommandLineBuilder EnableLegacyDoubleDashBehavior(this CommandLineBuilder builder, System.Boolean value = True)
8382
public static CommandLineBuilder EnablePosixBundling(this CommandLineBuilder builder, System.Boolean value = True)
8483
public static CommandLineBuilder RegisterWithDotnetSuggest(this CommandLineBuilder builder)
8584
public static CommandLineBuilder UseDefaults(this CommandLineBuilder builder)
@@ -98,9 +97,8 @@ System.CommandLine
9897
public static CommandLineBuilder UseVersionOption(this CommandLineBuilder builder)
9998
public static CommandLineBuilder UseVersionOption(this CommandLineBuilder builder, System.String[] aliases)
10099
public class CommandLineConfiguration
101-
.ctor(Command command, System.Boolean enablePosixBundling = True, System.Boolean enableDirectives = True, System.Boolean enableLegacyDoubleDashBehavior = False, System.Boolean enableTokenReplacement = True, LocalizationResources resources = null, System.Collections.Generic.IReadOnlyList<System.CommandLine.Invocation.InvocationMiddleware> middlewarePipeline = null, System.Func<System.CommandLine.Binding.BindingContext,System.CommandLine.Help.HelpBuilder> helpBuilderFactory = null, System.CommandLine.Parsing.TryReplaceToken tokenReplacer = null)
100+
.ctor(Command command, System.Boolean enablePosixBundling = True, System.Boolean enableDirectives = True, System.Boolean enableTokenReplacement = True, LocalizationResources resources = null, System.Collections.Generic.IReadOnlyList<System.CommandLine.Invocation.InvocationMiddleware> middlewarePipeline = null, System.Func<System.CommandLine.Binding.BindingContext,System.CommandLine.Help.HelpBuilder> helpBuilderFactory = null, System.CommandLine.Parsing.TryReplaceToken tokenReplacer = null)
102101
public System.Boolean EnableDirectives { get; }
103-
public System.Boolean EnableLegacyDoubleDashBehavior { get; }
104102
public System.Boolean EnablePosixBundling { get; }
105103
public System.Boolean EnableTokenReplacement { get; }
106104
public LocalizationResources LocalizationResources { get; }
@@ -236,7 +234,6 @@ System.CommandLine
236234
public System.CommandLine.Parsing.CommandResult RootCommandResult { get; }
237235
public System.Collections.Generic.IReadOnlyList<System.CommandLine.Parsing.Token> Tokens { get; }
238236
public System.Collections.Generic.IReadOnlyList<System.String> UnmatchedTokens { get; }
239-
public System.Collections.Generic.IReadOnlyList<System.String> UnparsedTokens { get; }
240237
public System.CommandLine.Parsing.ArgumentResult FindResultFor(Argument argument)
241238
public System.CommandLine.Parsing.CommandResult FindResultFor(Command command)
242239
public System.CommandLine.Parsing.OptionResult FindResultFor(Option option)
@@ -488,8 +485,7 @@ System.CommandLine.Parsing
488485
Command=1
489486
Option=2
490487
DoubleDash=3
491-
Unparsed=4
492-
Directive=5
488+
Directive=4
493489
public delegate TryReplaceToken : System.MulticastDelegate, System.ICloneable, System.Runtime.Serialization.ISerializable
494490
.ctor(System.Object object, System.IntPtr method)
495491
public System.IAsyncResult BeginInvoke(System.String tokenToReplace, ref System.Collections.Generic.IReadOnlyList<System.String> replacementTokens, ref System.String& errorMessage, System.AsyncCallback callback, System.Object object)

src/System.CommandLine.Hosting.Tests/HostingTests.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ void Execute(IHost host)
9191
}
9292

9393
[Fact]
94-
public static void UseHost_UnparsedTokens_can_propagate_to_Host_Configuration()
94+
public static void UseHost_UnmatchedTokens_can_propagate_to_Host_Configuration()
9595
{
9696
const string testArgument = "test";
97-
const string testKey = "unparsed-config";
98-
string commandLineArgs = $"-- --{testKey} {testArgument}";
97+
const string testKey = "unmatched-config";
98+
string commandLineArgs = $"--{testKey} {testArgument}";
9999

100100
string testConfigValue = null;
101101

@@ -110,11 +110,10 @@ void Execute(IHost host)
110110
{
111111
Handler = CommandHandler.Create<IHost>(Execute),
112112
})
113-
.EnableLegacyDoubleDashBehavior()
114113
.UseHost(host =>
115114
{
116115
var invocation = (InvocationContext)host.Properties[typeof(InvocationContext)];
117-
var args = invocation.ParseResult.UnparsedTokens.ToArray();
116+
var args = invocation.ParseResult.UnmatchedTokens.ToArray();
118117
host.ConfigureHostConfiguration(config =>
119118
{
120119
config.AddCommandLine(args);
@@ -129,11 +128,11 @@ void Execute(IHost host)
129128
}
130129

131130
[Fact]
132-
public static void UseHost_UnparsedTokens_are_available_in_HostBuilder_factory()
131+
public static void UseHost_UnmatchedTokens_are_available_in_HostBuilder_factory()
133132
{
134133
const string testArgument = "test";
135-
const string testKey = "unparsed-config";
136-
string commandLineArgs = $"-- --{testKey} {testArgument}";
134+
const string testKey = "unmatched-config";
135+
string commandLineArgs = $"--{testKey} {testArgument}";
137136

138137
string testConfigValue = null;
139138

@@ -148,7 +147,6 @@ void Execute(IHost host)
148147
{
149148
Handler = CommandHandler.Create<IHost>(Execute),
150149
})
151-
.EnableLegacyDoubleDashBehavior()
152150
.UseHost(args =>
153151
{
154152
var host = new HostBuilder();

src/System.CommandLine.Hosting/HostingExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static CommandLineBuilder UseHost(this CommandLineBuilder builder,
2020
Action<IHostBuilder> configureHost = null) =>
2121
builder.AddMiddleware(async (invocation, next) =>
2222
{
23-
var argsRemaining = invocation.ParseResult.UnparsedTokens.ToArray();
23+
var argsRemaining = invocation.ParseResult.UnmatchedTokens.ToArray();
2424
var hostBuilder = hostBuilderFactory?.Invoke(argsRemaining)
2525
?? new HostBuilder();
2626
hostBuilder.Properties[typeof(InvocationContext)] = invocation;

src/System.CommandLine.Suggest/SuggestionDispatcher.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug
7474
RegisterCommand,
7575
CompleteScriptCommand
7676
};
77-
77+
root.TreatUnmatchedTokensAsErrors = false;
7878
Parser = new CommandLineBuilder(root)
79-
.EnableLegacyDoubleDashBehavior()
8079
.UseVersionOption()
8180
.UseHelp()
8281
.UseParseDirective()
@@ -207,7 +206,7 @@ public static string FormatSuggestionArguments(
207206
int position,
208207
string targetExeName)
209208
{
210-
var tokens = parseResult.UnparsedTokens;
209+
var tokens = parseResult.UnmatchedTokens;
211210

212211
var commandLine = tokens.FirstOrDefault() ?? "";
213212

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ public void Custom_parser_can_pass_on_remaining_tokens(string commandLine)
559559
}
560560

561561
[Fact]
562-
public void When_tokens_are_passed_on_by_custom_parser_on_last_argument_then_they_become_unparsed_tokens()
562+
public void When_tokens_are_passed_on_by_custom_parser_on_last_argument_then_they_become_unmatched_tokens()
563563
{
564564

565565
var argument1 = new Argument<int[]>(
@@ -583,7 +583,7 @@ public void When_tokens_are_passed_on_by_custom_parser_on_last_argument_then_the
583583

584584
var parseResult = command.Parse("1 2 3 4 5 6 7 8");
585585

586-
parseResult.UnparsedTokens
586+
parseResult.UnmatchedTokens
587587
.Should()
588588
.BeEquivalentTo(new[] { "4", "5", "6", "7", "8" },
589589
options => options.WithStrictOrdering());

src/System.CommandLine.Tests/ParserTests.DoubleDash.cs

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public void Subsequent_tokens_are_parsed_as_arguments_even_if_they_match_option_
2424
};
2525

2626
var result = new CommandLineBuilder(rootCommand)
27-
.EnableLegacyDoubleDashBehavior(false)
2827
.Build()
2928
.Parse("-o \"some stuff\" -- -o --one -x -y -z -o:foo");
3029

@@ -34,11 +33,11 @@ public void Subsequent_tokens_are_parsed_as_arguments_even_if_they_match_option_
3433

3534
result.GetValueForArgument(argument).Should().BeEquivalentSequenceTo("-o", "--one", "-x", "-y", "-z", "-o:foo");
3635

37-
result.UnparsedTokens.Should().BeEmpty();
36+
result.UnmatchedTokens.Should().BeEmpty();
3837
}
3938

4039
[Fact]
41-
public void Unparsed_tokens_is_empty()
40+
public void Unmatched_tokens_is_empty()
4241
{
4342
var option = new Option<string[]>(new[] { "-o", "--one" });
4443
var argument = new Argument<string[]>();
@@ -49,11 +48,10 @@ public void Unparsed_tokens_is_empty()
4948
};
5049

5150
var result = new CommandLineBuilder(rootCommand)
52-
.EnableLegacyDoubleDashBehavior(false)
5351
.Build()
5452
.Parse("-o \"some stuff\" -- --one -x -y -z -o:foo");
5553

56-
result.UnparsedTokens.Should().BeEmpty();
54+
result.UnmatchedTokens.Should().BeEmpty();
5755
}
5856

5957
[Fact] // https://github.com/dotnet/command-line-api/issues/1631
@@ -68,7 +66,6 @@ public void No_errors_are_generated()
6866
};
6967

7068
var result = new CommandLineBuilder(rootCommand)
71-
.EnableLegacyDoubleDashBehavior(false)
7269
.Build()
7370
.Parse("-o \"some stuff\" -- -o --one -x -y -z -o:foo");
7471

@@ -85,7 +82,6 @@ public void A_second_double_dash_is_parsed_as_an_argument()
8582
};
8683

8784
var result = new CommandLineBuilder(rootCommand)
88-
.EnableLegacyDoubleDashBehavior(false)
8985
.Build()
9086
.Parse("a b c -- -- d");
9187

@@ -94,69 +90,5 @@ public void A_second_double_dash_is_parsed_as_an_argument()
9490
strings.Should().BeEquivalentSequenceTo("a", "b", "c", "--", "d");
9591
}
9692
}
97-
98-
public class LegacyDoubleDashBehavior
99-
{
100-
[Fact]
101-
public void The_portion_of_the_command_line_following_a_double_is_treated_as_unparsed_tokens()
102-
{
103-
var result = new CommandLineBuilder(new RootCommand { new Option<string>("-o") })
104-
.EnableLegacyDoubleDashBehavior()
105-
.Build()
106-
.Parse("-o \"some stuff\" -- x y z");
107-
108-
result.UnparsedTokens
109-
.Should()
110-
.BeEquivalentSequenceTo("x", "y", "z");
111-
}
112-
113-
[Fact]
114-
public void Subsequent_tokens_matching_options_will_be_treated_as_unparsed_tokens()
115-
{
116-
var optionO = new Option<string>(new[] { "-o" });
117-
var optionX = new Option<bool>(new[] { "-x" });
118-
var optionY = new Option<bool>(new[] { "-y" });
119-
var optionZ = new Option<bool>(new[] { "-z" });
120-
var rootCommand = new RootCommand
121-
{
122-
optionO,
123-
optionX,
124-
optionY,
125-
optionZ
126-
};
127-
var result = new CommandLineBuilder(rootCommand)
128-
.EnableLegacyDoubleDashBehavior()
129-
.Build()
130-
.Parse("-o \"some stuff\" -- -x -y -z -o:foo");
131-
132-
result.HasOption(optionO).Should().BeTrue();
133-
result.HasOption(optionX).Should().BeFalse();
134-
result.HasOption(optionY).Should().BeFalse();
135-
result.HasOption(optionZ).Should().BeFalse();
136-
137-
result.UnparsedTokens
138-
.Should()
139-
.BeEquivalentSequenceTo("-x",
140-
"-y",
141-
"-z",
142-
"-o:foo");
143-
}
144-
145-
[Fact]
146-
public void Subsequent_tokens_matching_argument_will_be_treated_as_unparsed_tokens()
147-
{
148-
var argument = new Argument<int[]>();
149-
var rootCommand = new RootCommand
150-
{
151-
argument
152-
};
153-
var result = new CommandLineBuilder(rootCommand)
154-
.EnableLegacyDoubleDashBehavior()
155-
.Build()
156-
.Parse("1 2 3 -- 4 5 6 7");
157-
158-
result.GetValueForArgument(argument).Should().BeEquivalentSequenceTo(1, 2, 3);
159-
}
160-
}
16193
}
16294
}

src/System.CommandLine.Tests/ParserTests.MultipleArguments.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void Tokens_that_cannot_be_converted_by_multiple_arity_argument_flow_to_n
216216
.Should()
217217
.Be("four");
218218

219-
result.UnparsedTokens
219+
result.UnmatchedTokens
220220
.Should()
221221
.ContainSingle()
222222
.Which

src/System.CommandLine/Builder/CommandLineBuilder.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ public CommandLineBuilder(Command? rootCommand = null)
4545
internal bool EnablePosixBundling { get; set; } = true;
4646

4747
internal bool EnableTokenReplacement { get; set; } = true;
48-
49-
/// <summary>
50-
/// Determines the behavior when parsing a double dash (<c>--</c>) in a command line.
51-
/// </summary>
52-
/// <remarks>When set to <see langword="true"/>, all tokens following <c>--</c> will be placed into the <see cref="ParseResult.UnparsedTokens"/> collection. When set to <see langword="false"/>, all tokens following <c>--</c> will be treated as command arguments, even if they match an existing option.</remarks>
53-
internal bool EnableLegacyDoubleDashBehavior { get; set; }
5448

5549
internal void CustomizeHelpLayout(Action<HelpContext> customize) =>
5650
_customizeHelpBuilder = customize;
@@ -97,7 +91,6 @@ public Parser Build() =>
9791
Command,
9892
enablePosixBundling: EnablePosixBundling,
9993
enableDirectives: EnableDirectives,
100-
enableLegacyDoubleDashBehavior: EnableLegacyDoubleDashBehavior,
10194
enableTokenReplacement: EnableTokenReplacement,
10295
resources: LocalizationResources,
10396
middlewarePipeline: _middlewareList is null

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,6 @@ public static CommandLineBuilder EnableDirectives(
156156
return builder;
157157
}
158158

159-
/// <summary>
160-
/// Determines the behavior when parsing a double dash (<c>--</c>) in a command line.
161-
/// </summary>
162-
/// <param name="builder">A command line builder.</param>
163-
/// <param name="value"><see langword="true" /> to place all tokens following <c>--</c> into the <see cref="ParseResult.UnparsedTokens"/> collection. <see langword="false" /> to treat all tokens following <c>--</c> as command arguments, even if they match an existing option.</param>
164-
public static CommandLineBuilder EnableLegacyDoubleDashBehavior(
165-
this CommandLineBuilder builder,
166-
bool value = true)
167-
{
168-
builder.EnableLegacyDoubleDashBehavior = value;
169-
return builder;
170-
}
171-
172159
/// <summary>
173160
/// Enables the parser to recognize and expand POSIX-style bundled options.
174161
/// </summary>

src/System.CommandLine/CommandLineConfiguration.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public class CommandLineConfiguration
2525
/// <param name="command">The root command for the parser.</param>
2626
/// <param name="enablePosixBundling"><see langword="true"/> to enable POSIX bundling; otherwise, <see langword="false"/>.</param>
2727
/// <param name="enableDirectives"><see langword="true"/> to enable directive parsing; otherwise, <see langword="false"/>.</param>
28-
/// <param name="enableLegacyDoubleDashBehavior">Enables the legacy behavior of the <c>--</c> token, which is to ignore parsing of subsequent tokens and place them in the <see cref="ParseResult.UnparsedTokens"/> list.</param>
2928
/// <param name="enableTokenReplacement"><see langword="true"/> to enable token replacement; otherwise, <see langword="false"/>.</param>
3029
/// <param name="resources">Provide custom validation messages.</param>
3130
/// <param name="middlewarePipeline">Provide a custom middleware pipeline.</param>
@@ -35,16 +34,13 @@ public CommandLineConfiguration(
3534
Command command,
3635
bool enablePosixBundling = true,
3736
bool enableDirectives = true,
38-
bool enableLegacyDoubleDashBehavior = false,
3937
bool enableTokenReplacement = true,
4038
LocalizationResources? resources = null,
4139
IReadOnlyList<InvocationMiddleware>? middlewarePipeline = null,
4240
Func<BindingContext, HelpBuilder>? helpBuilderFactory = null,
4341
TryReplaceToken? tokenReplacer = null)
4442
{
4543
RootCommand = command ?? throw new ArgumentNullException(nameof(command));
46-
47-
EnableLegacyDoubleDashBehavior = enableLegacyDoubleDashBehavior;
4844
EnableTokenReplacement = enableTokenReplacement;
4945
EnablePosixBundling = enablePosixBundling;
5046
EnableDirectives = enableDirectives;
@@ -72,11 +68,6 @@ internal static HelpBuilder DefaultHelpBuilderFactory(BindingContext context, in
7268
/// </summary>
7369
public bool EnableDirectives { get; }
7470

75-
/// <summary>
76-
/// Enables the legacy behavior of the <c>--</c> token, which is to ignore parsing of subsequent tokens and place them in the <see cref="ParseResult.UnparsedTokens"/> list.
77-
/// </summary>
78-
public bool EnableLegacyDoubleDashBehavior { get; }
79-
8071
/// <summary>
8172
/// Gets a value indicating whether POSIX bundling is enabled.
8273
/// </summary>

0 commit comments

Comments
 (0)