Skip to content

Commit 6da8117

Browse files
committed
re #1127 remove from ParseResult: ValueForOption(string), ValueForArgument(string), string indexer
1 parent 13d94d4 commit 6da8117

File tree

11 files changed

+179
-186
lines changed

11 files changed

+179
-186
lines changed

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -451,25 +451,26 @@ public void When_custom_conversion_fails_then_an_option_does_not_accept_further_
451451
[Fact]
452452
public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_value_throws()
453453
{
454-
var command = new Command("the-command")
454+
var option = new Option<int>(new[] { "-o", "--one" }, argumentResult =>
455455
{
456-
new Option<int>(new[] { "-o", "--one" }, argumentResult =>
457-
{
458-
if (int.TryParse(argumentResult.Tokens.Select(t => t.Value).Single(), out var value))
459-
{
460-
return value;
461-
}
456+
if (int.TryParse(argumentResult.Tokens.Select(t => t.Value).Single(), out var value))
457+
{
458+
return value;
459+
}
462460

463-
argumentResult.ErrorMessage = $"'{argumentResult.Tokens.Single().Value}' is not an integer";
461+
argumentResult.ErrorMessage = $"'{argumentResult.Tokens.Single().Value}' is not an integer";
464462

465-
return default;
466-
})
463+
return default;
464+
});
465+
var command = new Command("the-command")
466+
{
467+
option
467468
};
468469

469470
var result = command.Parse("the-command -o not-an-int");
470471

471472
Action getValue = () =>
472-
result.ValueForOption("-o");
473+
result.ValueForOption(option);
473474

474475
getValue.Should()
475476
.Throw<InvalidOperationException>()

src/System.CommandLine.Tests/Binding/TypeConversionTests.cs

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -183,44 +183,47 @@ public void Bool_does_not_parse_as_the_default_value_when_the_option_has_been_ap
183183
[Fact]
184184
public void By_default_an_option_with_zero_or_one_argument_parses_as_the_argument_string_value_by_default()
185185
{
186+
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
186187
var command = new Command("the-command")
187188
{
188-
new Option("-x", arity: ArgumentArity.ZeroOrOne)
189+
option
189190
};
190191

191192
var result = command.Parse("the-command -x the-argument");
192193

193-
result.ValueForOption("-x")
194+
result.ValueForOption(option)
194195
.Should()
195196
.Be("the-argument");
196197
}
197198

198199
[Fact]
199200
public void By_default_an_option_with_exactly_one_argument_parses_as_the_argument_string_value_by_default()
200201
{
202+
var option = new Option("-x", arity: ArgumentArity.ExactlyOne);
201203
var command = new Command("the-command")
202204
{
203-
new Option("-x", arity: ArgumentArity.ExactlyOne)
205+
option
204206
};
205207

206208
var result = command.Parse("the-command -x the-argument");
207209

208-
result.ValueForOption("-x")
210+
result.ValueForOption(option)
209211
.Should()
210212
.Be("the-argument");
211213
}
212214

213215
[Fact]
214216
public void When_exactly_one_argument_is_expected_and_none_are_provided_then_getting_value_throws()
215217
{
218+
var option = new Option("-x", arity: ArgumentArity.ExactlyOne);
216219
var command = new Command("the-command")
217220
{
218-
new Option("-x", arity: ArgumentArity.ExactlyOne)
221+
option
219222
};
220223

221224
var result = command.Parse("the-command -x");
222225

223-
Action getValue = () => result.ValueForOption("-x");
226+
Action getValue = () => result.ValueForOption(option);
224227

225228
getValue.Should()
226229
.Throw<InvalidOperationException>()
@@ -233,14 +236,15 @@ public void When_exactly_one_argument_is_expected_and_none_are_provided_then_get
233236
[Fact]
234237
public void When_zero_or_more_arguments_of_unspecified_type_are_expected_and_none_are_provided_then_getting_value_returns_an_empty_sequence_of_strings()
235238
{
239+
var option = new Option("-x", arity: ArgumentArity.ZeroOrMore);
236240
var command = new Command("the-command")
237241
{
238-
new Option("-x", arity: ArgumentArity.ZeroOrMore)
242+
option
239243
};
240244

241245
var result = command.Parse("the-command -x");
242246

243-
result.ValueForOption("-x")
247+
result.ValueForOption(option)
244248
.Should()
245249
.BeAssignableTo<IReadOnlyCollection<string>>()
246250
.Which
@@ -253,15 +257,14 @@ public void
253257
When_zero_or_more_arguments_of_unspecified_type_are_expected_and_none_are_provided_and_there_is_a_default_then_getting_value_returns_default_in_an_empty_sequence_of_strings()
254258
{
255259
var option = new Option("-x", getDefaultValue: () => "the-default", arity: ArgumentArity.ZeroOrMore);
256-
257260
var command = new Command("the-command")
258261
{
259262
option
260263
};
261264

262265
var result = command.Parse("the-command");
263266

264-
result.ValueForOption("-x")
267+
result.ValueForOption(option)
265268
.Should()
266269
.BeAssignableTo<IReadOnlyCollection<string>>()
267270
.Which
@@ -272,14 +275,15 @@ public void
272275
[Fact]
273276
public void When_one_or_more_arguments_of_unspecified_type_are_expected_and_none_are_provided_then_getting_value_throws()
274277
{
278+
var option = new Option("-x", arity: ArgumentArity.OneOrMore);
275279
var command = new Command("the-command")
276280
{
277-
new Option("-x", arity: ArgumentArity.OneOrMore)
281+
option
278282
};
279283

280284
var result = command.Parse("the-command -x");
281285

282-
Action getValue = () => result.ValueForOption("-x");
286+
Action getValue = () => result.ValueForOption(option);
283287

284288
getValue.Should()
285289
.Throw<InvalidOperationException>()
@@ -292,27 +296,29 @@ public void When_one_or_more_arguments_of_unspecified_type_are_expected_and_none
292296
[Fact]
293297
public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_multiple_arguments_parses_as_a_sequence_of_strings()
294298
{
299+
var option = new Option("-x", arity: ArgumentArity.ZeroOrMore);
295300
var command = new Command("the-command")
296301
{
297-
new Option("-x", arity: ArgumentArity.ZeroOrMore)
302+
option
298303
};
299304

300305
command.Parse("the-command -x arg1 -x arg2")
301-
.ValueForOption("-x")
306+
.ValueForOption(option)
302307
.Should()
303308
.BeEquivalentTo(new[] { "arg1", "arg2" });
304309
}
305310

306311
[Fact]
307312
public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_one_argument_parses_as_a_sequence_of_strings()
308313
{
314+
var option = new Option("-x", arity: ArgumentArity.ZeroOrMore);
309315
var command = new Command("the-command")
310316
{
311-
new Option("-x", arity: ArgumentArity.ZeroOrMore)
317+
option
312318
};
313319

314320
command.Parse("the-command -x arg1")
315-
.ValueForOption("-x")
321+
.ValueForOption(option)
316322
.Should()
317323
.BeEquivalentTo(new[] { "arg1" });
318324
}
@@ -323,18 +329,19 @@ public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_on
323329
[InlineData("c c c")]
324330
public void When_command_argument_has_arity_greater_than_one_it_captures_arguments_before_and_after_option(string commandLine)
325331
{
332+
var argument = new Argument<string[]>("the-arg")
333+
{
334+
Arity = ArgumentArity.ZeroOrMore
335+
};
326336
var command = new Command("the-command")
327337
{
328338
new Option<string>("-a"),
329-
new Argument<string>("the-arg")
330-
{
331-
Arity = ArgumentArity.ZeroOrMore
332-
}
339+
argument
333340
};
334341

335342
var result = command.Parse(commandLine);
336343

337-
result.ValueForArgument("the-arg")
344+
result.ValueForArgument(argument)
338345
.Should()
339346
.BeEquivalentTo(new[] { "c", "c", "c" });
340347
}
@@ -361,14 +368,15 @@ public void The_default_value_of_an_option_with_no_arguments_is_null()
361368
[Fact]
362369
public void By_default_an_option_without_arguments_parses_as_false_when_it_is_not_applied()
363370
{
371+
var option = new Option("-x");
364372
var command = new Command("something")
365373
{
366-
new Option("-x")
374+
option
367375
};
368376

369377
var result = command.Parse("something");
370378

371-
result.ValueForOption<bool>("-x")
379+
result.ValueForOption<bool>(option)
372380
.Should()
373381
.BeFalse();
374382
}
@@ -439,7 +447,7 @@ public void A_default_value_with_a_custom_constructor_can_be_specified_for_a_com
439447

440448
result.Errors.Should().BeEmpty();
441449

442-
var value = result.ValueForArgument("the-arg");
450+
var value = result.ValueForArgument(argument);
443451

444452
value.Should().Be(directoryInfo);
445453
}
@@ -464,14 +472,15 @@ public void An_option_argument_with_a_default_argument_can_be_converted_to_the_r
464472
[Fact]
465473
public void Specifying_an_option_argument_overrides_the_default_value()
466474
{
475+
var option = new Option<int>("-x", () => 123);
467476
var command = new Command("something")
468477
{
469-
new Option<int>("-x", () => 123)
478+
option
470479
};
471480

472481
var result = command.Parse("something -x 456");
473482

474-
var value = result.ValueForOption<int>("-x");
483+
var value = result.ValueForOption(option);
475484

476485
value.Should().Be(456);
477486
}
@@ -491,7 +500,7 @@ public void Values_can_be_correctly_converted_to_nullable_int_with_no_value_with
491500
{
492501
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
493502

494-
var value = option.Parse("").ValueForOption<int?>("-x");
503+
var value = option.Parse("").ValueForOption<int?>(option);
495504

496505
value.Should().BeNull();
497506
}
@@ -521,7 +530,7 @@ public void Values_can_be_correctly_converted_to_double_without_the_parser_speci
521530
{
522531
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
523532

524-
var value = option.Parse("-x 123.456").ValueForOption<double>("-x");
533+
var value = option.Parse("-x 123.456").ValueForOption<double>(option);
525534

526535
value.Should().Be(123.456d);
527536
}
@@ -531,7 +540,7 @@ public void Values_can_be_correctly_converted_to_float_without_the_parser_specif
531540
{
532541
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
533542

534-
var value = option.Parse("-x 123.456").ValueForOption<float>("-x");
543+
var value = option.Parse("-x 123.456").ValueForOption<float>(option);
535544

536545
value.Should().Be(123.456f);
537546
}
@@ -541,24 +550,24 @@ public void Options_with_no_arguments_specified_can_be_correctly_converted_to_bo
541550
{
542551
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
543552

544-
option.Parse("-x").ValueForOption<bool>("-x").Should().BeTrue();
553+
option.Parse("-x").ValueForOption<bool>(option).Should().BeTrue();
545554
}
546555

547556
[Fact]
548557
public void Options_with_arguments_specified_can_be_correctly_converted_to_bool_without_the_parser_specifying_a_custom_converter()
549558
{
550559
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
551560

552-
option.Parse("-x false").ValueForOption<bool>("-x").Should().BeFalse();
553-
option.Parse("-x true").ValueForOption<bool>("-x").Should().BeTrue();
561+
option.Parse("-x false").ValueForOption<bool>(option).Should().BeFalse();
562+
option.Parse("-x true").ValueForOption<bool>(option).Should().BeTrue();
554563
}
555564

556565
[Fact]
557566
public void Values_can_be_correctly_converted_to_array_of_int_without_the_parser_specifying_a_custom_converter()
558567
{
559568
var option = new Option("-x", arity: ArgumentArity.ZeroOrMore);
560569

561-
var value = option.Parse("-x 1 -x 2 -x 3").ValueForOption<int[]>("-x");
570+
var value = option.Parse("-x 1 -x 2 -x 3").ValueForOption<int[]>(option);
562571

563572
value.Should().BeEquivalentTo(1, 2, 3);
564573
}
@@ -608,7 +617,7 @@ public void Values_can_be_correctly_converted_to_List_of_int_without_the_parser_
608617
{
609618
var option = new Option("-x", arity: ArgumentArity.ZeroOrMore);
610619

611-
var value = option.Parse("-x 1 -x 2 -x 3").ValueForOption<List<int>>("-x");
620+
var value = option.Parse("-x 1 -x 2 -x 3").ValueForOption<List<int>>(option);
612621

613622
value.Should().BeEquivalentTo(1, 2, 3);
614623
}
@@ -618,7 +627,7 @@ public void Values_can_be_correctly_converted_to_IEnumerable_of_int_without_the_
618627
{
619628
var option = new Option("-x", arity: ArgumentArity.ZeroOrMore);
620629

621-
var value = option.Parse("-x 1 -x 2 -x 3").ValueForOption<IEnumerable<int>>("-x");
630+
var value = option.Parse("-x 1 -x 2 -x 3").ValueForOption<IEnumerable<int>>(option);
622631

623632
value.Should().BeEquivalentTo(1, 2, 3);
624633
}
@@ -630,7 +639,7 @@ public void Enum_values_can_be_correctly_converted_based_on_enum_value_name_with
630639

631640
var parseResult = option.Parse("-x Monday");
632641

633-
var value = parseResult.ValueForOption<DayOfWeek>("-x");
642+
var value = parseResult.ValueForOption<DayOfWeek>(option);
634643

635644
value.Should().Be(DayOfWeek.Monday);
636645
}
@@ -655,7 +664,7 @@ public void When_getting_values_and_specifying_a_conversion_type_that_is_not_sup
655664

656665
var result = option.Parse("-x not-an-int");
657666

658-
Action getValue = () => result.ValueForOption<int>("-x");
667+
Action getValue = () => result.ValueForOption<int>(option);
659668

660669
getValue.Should()
661670
.Throw<InvalidOperationException>()
@@ -672,7 +681,7 @@ public void When_getting_an_array_of_values_and_specifying_a_conversion_type_tha
672681

673682
var result = option.Parse("-x not-an-int -x 2");
674683

675-
Action getValue = () => result.ValueForOption<int[]>("-x");
684+
Action getValue = () => result.ValueForOption<int[]>(option);
676685

677686
getValue.Should()
678687
.Throw<InvalidOperationException>()

src/System.CommandLine.Tests/Invocation/CommandHandlerTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,16 @@ public async Task Method_parameters_of_type_ParseResult_receive_the_current_Pars
260260
{
261261
ParseResult boundParseResult = default;
262262

263+
var option = new Option<int>("-x");
263264
var command = new Command("command")
264265
{
265-
new Option<int>("-x")
266+
option
266267
};
267268
command.Handler = CommandHandler.Create<ParseResult>(result => { boundParseResult = result; });
268269

269270
await command.InvokeAsync("command -x 123", _console);
270271

271-
boundParseResult.ValueForOption("-x").Should().Be(123);
272+
boundParseResult.ValueForOption(option).Should().Be(123);
272273
}
273274

274275
[Fact]
@@ -307,15 +308,16 @@ public async Task Method_parameters_of_type_InvocationContext_receive_the_curren
307308
{
308309
InvocationContext boundContext = default;
309310

311+
var option = new Option<int>("-x");
310312
var command = new Command("command")
311313
{
312-
new Option<int>("-x")
314+
option
313315
};
314316
command.Handler = CommandHandler.Create<InvocationContext>(context => { boundContext = context; });
315317

316318
await command.InvokeAsync("command -x 123", _console);
317319

318-
boundContext.ParseResult.ValueForOption("-x").Should().Be(123);
320+
boundContext.ParseResult.ValueForOption(option).Should().Be(123);
319321
}
320322

321323

0 commit comments

Comments
 (0)