|
| 1 | +// Copyright (c) .NET Foundation and contributors. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.CommandLine.Tests.Utility; |
| 5 | +using FluentAssertions; |
| 6 | +using FluentAssertions.Execution; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace System.CommandLine.Tests |
| 10 | +{ |
| 11 | + public partial class ParserTests |
| 12 | + { |
| 13 | + public class CaptureRemainingTokens |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public void Option_like_tokens_after_capturing_argument_are_captured() |
| 17 | + { |
| 18 | + var option = new Option<string>("--source"); |
| 19 | + var toolName = new Argument<string>("toolName"); |
| 20 | + var toolArgs = new Argument<string[]>("toolArgs") { CaptureRemainingTokens = true }; |
| 21 | + var command = new RootCommand |
| 22 | + { |
| 23 | + option, |
| 24 | + toolName, |
| 25 | + toolArgs |
| 26 | + }; |
| 27 | + |
| 28 | + var result = command.Parse("--source https://nuget.org myTool -a 1 --help"); |
| 29 | + |
| 30 | + using var _ = new AssertionScope(); |
| 31 | + result.GetValue(option).Should().Be("https://nuget.org"); |
| 32 | + result.GetValue(toolName).Should().Be("myTool"); |
| 33 | + result.GetValue(toolArgs).Should().BeEquivalentSequenceTo("-a", "1", "--help"); |
| 34 | + result.Errors.Should().BeEmpty(); |
| 35 | + result.UnmatchedTokens.Should().BeEmpty(); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void Known_options_after_capturing_argument_are_captured() |
| 40 | + { |
| 41 | + var verbose = new Option<bool>("--verbose"); |
| 42 | + var toolArgs = new Argument<string[]>("toolArgs") { CaptureRemainingTokens = true }; |
| 43 | + var command = new RootCommand |
| 44 | + { |
| 45 | + verbose, |
| 46 | + toolArgs |
| 47 | + }; |
| 48 | + |
| 49 | + var result = command.Parse("foo --verbose bar"); |
| 50 | + |
| 51 | + using var _ = new AssertionScope(); |
| 52 | + result.GetValue(toolArgs).Should().BeEquivalentSequenceTo("foo", "--verbose", "bar"); |
| 53 | + result.GetValue(verbose).Should().BeFalse(); |
| 54 | + result.Errors.Should().BeEmpty(); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void Tokens_matching_subcommand_names_are_captured() |
| 59 | + { |
| 60 | + var sub = new Command("sub"); |
| 61 | + var toolName = new Argument<string>("toolName"); |
| 62 | + var toolArgs = new Argument<string[]>("toolArgs") { CaptureRemainingTokens = true }; |
| 63 | + var command = new RootCommand |
| 64 | + { |
| 65 | + sub, |
| 66 | + toolName, |
| 67 | + toolArgs |
| 68 | + }; |
| 69 | + |
| 70 | + var result = command.Parse("myTool sub --flag"); |
| 71 | + |
| 72 | + using var _ = new AssertionScope(); |
| 73 | + result.GetValue(toolName).Should().Be("myTool"); |
| 74 | + result.GetValue(toolArgs).Should().BeEquivalentSequenceTo("sub", "--flag"); |
| 75 | + result.UnmatchedTokens.Should().BeEmpty(); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void Options_before_capturing_argument_are_parsed_normally() |
| 80 | + { |
| 81 | + var source = new Option<string>("--source"); |
| 82 | + var toolName = new Argument<string>("toolName"); |
| 83 | + var toolArgs = new Argument<string[]>("toolArgs") { CaptureRemainingTokens = true }; |
| 84 | + var command = new RootCommand |
| 85 | + { |
| 86 | + source, |
| 87 | + toolName, |
| 88 | + toolArgs |
| 89 | + }; |
| 90 | + |
| 91 | + var result = command.Parse("--source https://nuget.org myTool --help"); |
| 92 | + |
| 93 | + using var _ = new AssertionScope(); |
| 94 | + result.GetValue(source).Should().Be("https://nuget.org"); |
| 95 | + result.GetValue(toolName).Should().Be("myTool"); |
| 96 | + result.GetValue(toolArgs).Should().BeEquivalentSequenceTo("--help"); |
| 97 | + result.Errors.Should().BeEmpty(); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public void Double_dash_before_capturing_argument_works() |
| 102 | + { |
| 103 | + var option = new Option<string>("--source"); |
| 104 | + var toolArgs = new Argument<string[]>("toolArgs") { CaptureRemainingTokens = true }; |
| 105 | + var command = new RootCommand |
| 106 | + { |
| 107 | + option, |
| 108 | + toolArgs |
| 109 | + }; |
| 110 | + |
| 111 | + var result = command.Parse("--source foo -- --help --version"); |
| 112 | + |
| 113 | + using var _ = new AssertionScope(); |
| 114 | + result.GetValue(option).Should().Be("foo"); |
| 115 | + result.GetValue(toolArgs).Should().BeEquivalentSequenceTo("--help", "--version"); |
| 116 | + result.Errors.Should().BeEmpty(); |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + public void Double_dash_during_capture_is_captured() |
| 121 | + { |
| 122 | + var toolArgs = new Argument<string[]>("toolArgs") { CaptureRemainingTokens = true }; |
| 123 | + var command = new RootCommand |
| 124 | + { |
| 125 | + toolArgs |
| 126 | + }; |
| 127 | + |
| 128 | + var result = command.Parse("foo -- --help"); |
| 129 | + |
| 130 | + using var _ = new AssertionScope(); |
| 131 | + result.GetValue(toolArgs).Should().BeEquivalentSequenceTo("foo", "--", "--help"); |
| 132 | + result.Errors.Should().BeEmpty(); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public void Non_capturing_arguments_are_unaffected() |
| 137 | + { |
| 138 | + var option = new Option<bool>("--verbose"); |
| 139 | + var arg = new Argument<string[]>("arg"); |
| 140 | + var command = new RootCommand |
| 141 | + { |
| 142 | + option, |
| 143 | + arg |
| 144 | + }; |
| 145 | + |
| 146 | + var result = command.Parse("foo --verbose bar"); |
| 147 | + |
| 148 | + using var _ = new AssertionScope(); |
| 149 | + result.GetValue(option).Should().Be(true); |
| 150 | + result.GetValue(arg).Should().BeEquivalentSequenceTo("foo", "bar"); |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public void Arity_limits_are_still_respected() |
| 155 | + { |
| 156 | + var toolArg = new Argument<string>("toolArg") { CaptureRemainingTokens = true }; |
| 157 | + var command = new RootCommand |
| 158 | + { |
| 159 | + toolArg |
| 160 | + }; |
| 161 | + |
| 162 | + var result = command.Parse("first --extra"); |
| 163 | + |
| 164 | + using var _ = new AssertionScope(); |
| 165 | + result.GetValue(toolArg).Should().Be("first"); |
| 166 | + result.UnmatchedTokens.Should().BeEquivalentTo("--extra"); |
| 167 | + } |
| 168 | + |
| 169 | + [Fact] |
| 170 | + public void Empty_input_with_capturing_argument_produces_no_errors_when_arity_allows() |
| 171 | + { |
| 172 | + var toolArgs = new Argument<string[]>("toolArgs") { CaptureRemainingTokens = true }; |
| 173 | + var command = new RootCommand |
| 174 | + { |
| 175 | + toolArgs |
| 176 | + }; |
| 177 | + |
| 178 | + var result = command.Parse(""); |
| 179 | + |
| 180 | + using var _ = new AssertionScope(); |
| 181 | + result.GetValue(toolArgs).Should().BeEmpty(); |
| 182 | + result.Errors.Should().BeEmpty(); |
| 183 | + } |
| 184 | + |
| 185 | + [Fact] |
| 186 | + public void Option_with_value_syntax_is_captured_as_single_token() |
| 187 | + { |
| 188 | + var toolArgs = new Argument<string[]>("toolArgs") { CaptureRemainingTokens = true }; |
| 189 | + var command = new RootCommand |
| 190 | + { |
| 191 | + toolArgs |
| 192 | + }; |
| 193 | + |
| 194 | + var result = command.Parse("--key=value -x:y"); |
| 195 | + |
| 196 | + using var _ = new AssertionScope(); |
| 197 | + result.GetValue(toolArgs).Should().BeEquivalentSequenceTo("--key=value", "-x:y"); |
| 198 | + result.Errors.Should().BeEmpty(); |
| 199 | + } |
| 200 | + |
| 201 | + [Fact] |
| 202 | + public void Capturing_argument_on_subcommand_works() |
| 203 | + { |
| 204 | + var toolName = new Argument<string>("toolName"); |
| 205 | + var toolArgs = new Argument<string[]>("toolArgs") { CaptureRemainingTokens = true }; |
| 206 | + var exec = new Command("exec") |
| 207 | + { |
| 208 | + toolName, |
| 209 | + toolArgs |
| 210 | + }; |
| 211 | + exec.Options.Add(new Option<bool>("--verbose")); |
| 212 | + var command = new RootCommand |
| 213 | + { |
| 214 | + exec |
| 215 | + }; |
| 216 | + |
| 217 | + var result = command.Parse("exec foo --verbose bar"); |
| 218 | + |
| 219 | + using var _ = new AssertionScope(); |
| 220 | + result.GetValue(toolName).Should().Be("foo"); |
| 221 | + result.GetValue(toolArgs).Should().BeEquivalentSequenceTo("--verbose", "bar"); |
| 222 | + result.Errors.Should().BeEmpty(); |
| 223 | + } |
| 224 | + |
| 225 | + [Fact] |
| 226 | + public void Trailing_double_dash_is_captured() |
| 227 | + { |
| 228 | + var toolArgs = new Argument<string[]>("toolArgs") { CaptureRemainingTokens = true }; |
| 229 | + var command = new RootCommand |
| 230 | + { |
| 231 | + toolArgs |
| 232 | + }; |
| 233 | + |
| 234 | + var result = command.Parse("foo --"); |
| 235 | + |
| 236 | + using var _ = new AssertionScope(); |
| 237 | + result.GetValue(toolArgs).Should().BeEquivalentSequenceTo("foo", "--"); |
| 238 | + result.Errors.Should().BeEmpty(); |
| 239 | + } |
| 240 | + |
| 241 | + [Fact] |
| 242 | + public void Leading_known_option_is_parsed_normally_when_capture_is_first_argument() |
| 243 | + { |
| 244 | + var verbose = new Option<bool>("--verbose"); |
| 245 | + var toolArgs = new Argument<string[]>("toolArgs") { CaptureRemainingTokens = true }; |
| 246 | + var command = new RootCommand |
| 247 | + { |
| 248 | + verbose, |
| 249 | + toolArgs |
| 250 | + }; |
| 251 | + |
| 252 | + var result = command.Parse("--verbose foo --unknown"); |
| 253 | + |
| 254 | + using var _ = new AssertionScope(); |
| 255 | + result.GetValue(verbose).Should().BeTrue(); |
| 256 | + result.GetValue(toolArgs).Should().BeEquivalentSequenceTo("foo", "--unknown"); |
| 257 | + result.Errors.Should().BeEmpty(); |
| 258 | + } |
| 259 | + } |
| 260 | + } |
| 261 | +} |
0 commit comments