Skip to content

Commit e99b4ee

Browse files
committed
remove ValueForArgument<T>(Argument) and ValueForOption<T>(Option)
1 parent 2dc41f9 commit e99b4ee

File tree

9 files changed

+180
-185
lines changed

9 files changed

+180
-185
lines changed

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -452,25 +452,27 @@ public void When_custom_conversion_fails_then_an_option_does_not_accept_further_
452452
[Fact]
453453
public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_value_throws()
454454
{
455-
var command = new Command("the-command")
455+
var option = new Option<int>(new[] { "-o", "--one" }, argumentResult =>
456456
{
457-
new Option<int>(new[] { "-o", "--one" }, argumentResult =>
458-
{
459-
if (int.TryParse(argumentResult.Tokens.Select(t => t.Value).Single(), out var value))
460-
{
461-
return value;
462-
}
457+
if (int.TryParse(argumentResult.Tokens.Select(t => t.Value).Single(), out var value))
458+
{
459+
return value;
460+
}
463461

464-
argumentResult.ErrorMessage = $"'{argumentResult.Tokens.Single().Value}' is not an integer";
462+
argumentResult.ErrorMessage = $"'{argumentResult.Tokens.Single().Value}' is not an integer";
465463

466-
return default;
467-
})
464+
return default;
465+
});
466+
467+
var command = new Command("the-command")
468+
{
469+
option
468470
};
469471

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

472-
Action getValue = () =>
473-
result.ValueForOption("-o");
474+
Action getValue = () =>
475+
result.ValueForOption(option);
474476

475477
getValue.Should()
476478
.Throw<InvalidOperationException>()

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

Lines changed: 47 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -182,46 +182,52 @@ public void Bool_does_not_parse_as_the_default_value_when_the_option_has_been_ap
182182
}
183183

184184
[Fact]
185-
public void By_default_an_option_with_zero_or_one_argument_parses_as_the_argument_string_value_by_default()
185+
public void By_default_an_option_with_zero_or_one_argument_parses_as_the_argument_string_value()
186186
{
187+
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
188+
187189
var command = new Command("the-command")
188190
{
189-
new Option("-x", arity: ArgumentArity.ZeroOrOne)
191+
option
190192
};
191193

192194
var result = command.Parse("the-command -x the-argument");
193195

194-
result.ValueForOption("-x")
196+
result.ValueForOption(option)
195197
.Should()
196198
.Be("the-argument");
197199
}
198200

199201
[Fact]
200-
public void By_default_an_option_with_exactly_one_argument_parses_as_the_argument_string_value_by_default()
202+
public void By_default_an_option_with_exactly_one_argument_parses_as_the_argument_string_value()
201203
{
204+
var option = new Option("-x", arity: ArgumentArity.ExactlyOne);
205+
202206
var command = new Command("the-command")
203207
{
204-
new Option("-x", arity: ArgumentArity.ExactlyOne)
208+
option
205209
};
206210

207211
var result = command.Parse("the-command -x the-argument");
208212

209-
result.ValueForOption("-x")
213+
result.ValueForOption(option)
210214
.Should()
211215
.Be("the-argument");
212216
}
213217

214218
[Fact]
215219
public void When_exactly_one_argument_is_expected_and_none_are_provided_then_getting_value_throws()
216220
{
221+
var option = new Option("-x", arity: ArgumentArity.ExactlyOne);
222+
217223
var command = new Command("the-command")
218224
{
219-
new Option("-x", arity: ArgumentArity.ExactlyOne)
225+
option
220226
};
221227

222228
var result = command.Parse("the-command -x");
223229

224-
Action getValue = () => result.ValueForOption("-x");
230+
Action getValue = () => result.ValueForOption(option);
225231

226232
getValue.Should()
227233
.Throw<InvalidOperationException>()
@@ -234,14 +240,16 @@ public void When_exactly_one_argument_is_expected_and_none_are_provided_then_get
234240
[Fact]
235241
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()
236242
{
243+
var option = new Option("-x", arity: ArgumentArity.ZeroOrMore);
244+
237245
var command = new Command("the-command")
238246
{
239-
new Option("-x", arity: ArgumentArity.ZeroOrMore)
247+
option
240248
};
241249

242250
var result = command.Parse("the-command -x");
243251

244-
result.ValueForOption("-x")
252+
result.ValueForOption(option)
245253
.Should()
246254
.BeAssignableTo<IReadOnlyCollection<string>>()
247255
.Which
@@ -262,7 +270,7 @@ public void
262270

263271
var result = command.Parse("the-command");
264272

265-
result.ValueForOption("-x")
273+
result.ValueForOption(option)
266274
.Should()
267275
.BeAssignableTo<IReadOnlyCollection<string>>()
268276
.Which
@@ -273,14 +281,16 @@ public void
273281
[Fact]
274282
public void When_one_or_more_arguments_of_unspecified_type_are_expected_and_none_are_provided_then_getting_value_throws()
275283
{
284+
var option = new Option("-x", arity: ArgumentArity.OneOrMore);
285+
276286
var command = new Command("the-command")
277287
{
278-
new Option("-x", arity: ArgumentArity.OneOrMore)
288+
option
279289
};
280290

281291
var result = command.Parse("the-command -x");
282292

283-
Action getValue = () => result.ValueForOption("-x");
293+
Action getValue = () => result.ValueForOption(option);
284294

285295
getValue.Should()
286296
.Throw<InvalidOperationException>()
@@ -293,27 +303,31 @@ public void When_one_or_more_arguments_of_unspecified_type_are_expected_and_none
293303
[Fact]
294304
public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_multiple_arguments_parses_as_a_sequence_of_strings()
295305
{
306+
var option = new Option("-x", arity: ArgumentArity.ZeroOrMore);
307+
296308
var command = new Command("the-command")
297309
{
298-
new Option("-x", arity: ArgumentArity.ZeroOrMore)
310+
option
299311
};
300312

301313
command.Parse("the-command -x arg1 -x arg2")
302-
.ValueForOption("-x")
314+
.ValueForOption(option)
303315
.Should()
304316
.BeEquivalentTo(new[] { "arg1", "arg2" });
305317
}
306318

307319
[Fact]
308320
public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_one_argument_parses_as_a_sequence_of_strings()
309321
{
322+
var option = new Option("-x", arity: ArgumentArity.ZeroOrMore);
323+
310324
var command = new Command("the-command")
311325
{
312-
new Option("-x", arity: ArgumentArity.ZeroOrMore)
326+
option
313327
};
314328

315329
command.Parse("the-command -x arg1")
316-
.ValueForOption("-x")
330+
.ValueForOption(option)
317331
.Should()
318332
.BeEquivalentTo(new[] { "arg1" });
319333
}
@@ -324,24 +338,26 @@ public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_on
324338
[InlineData("c c c")]
325339
public void When_command_argument_has_arity_greater_than_one_it_captures_arguments_before_and_after_option(string commandLine)
326340
{
341+
var argument = new Argument<string[]>("the-arg")
342+
{
343+
Arity = ArgumentArity.ZeroOrMore
344+
};
345+
327346
var command = new Command("the-command")
328347
{
329348
new Option<string>("-a"),
330-
new Argument<string>("the-arg")
331-
{
332-
Arity = ArgumentArity.ZeroOrMore
333-
}
349+
argument
334350
};
335351

336352
var result = command.Parse(commandLine);
337353

338-
result.ValueForArgument("the-arg")
354+
result.ValueForArgument(argument)
339355
.Should()
340356
.BeEquivalentTo(new[] { "c", "c", "c" });
341357
}
342358

343359
[Fact]
344-
public void The_default_value_of_an_option_with_no_arguments_is_false()
360+
public void The_default_value_of_an_option_with_no_arguments_is_true()
345361
{
346362
var option = new Option("-x");
347363

@@ -353,8 +369,7 @@ public void The_default_value_of_an_option_with_no_arguments_is_false()
353369

354370
var result = command.Parse("-x");
355371

356-
result.FindResultFor(option)
357-
.GetValueOrDefault()
372+
result.ValueForOption(option)
358373
.Should()
359374
.Be(true);
360375
}
@@ -371,9 +386,9 @@ public void By_default_an_option_without_arguments_parses_as_false_when_it_is_no
371386

372387
var result = command.Parse("something");
373388

374-
result.ValueForOption<bool>(option)
389+
result.ValueForOption(option)
375390
.Should()
376-
.BeFalse();
391+
.Be(false);
377392
}
378393

379394
[Fact]
@@ -442,83 +457,28 @@ public void A_default_value_with_a_custom_constructor_can_be_specified_for_a_com
442457

443458
result.Errors.Should().BeEmpty();
444459

445-
var value = result.ValueForArgument("the-arg");
460+
var value = result.ValueForArgument(argument);
446461

447462
value.Should().Be(directoryInfo);
448463
}
449464

450465
[Fact]
451-
public void An_option_argument_with_a_default_argument_can_be_converted_to_the_requested_type()
466+
public void Specifying_an_option_argument_overrides_the_default_value()
452467
{
453-
var option = new Option<string>("-x", () => "123");
468+
var option = new Option<int>("-x", () => 123);
454469

455470
var command = new Command("something")
456471
{
457472
option
458473
};
459474

460-
var result = command.Parse("something");
461-
462-
var value = result.ValueForOption<int>(option);
463-
464-
value.Should().Be(123);
465-
}
466-
467-
[Fact]
468-
public void Specifying_an_option_argument_overrides_the_default_value()
469-
{
470-
var command = new Command("something")
471-
{
472-
new Option<int>("-x", () => 123)
473-
};
474-
475475
var result = command.Parse("something -x 456");
476476

477-
var value = result.ValueForOption<int>("-x");
477+
var value = result.ValueForOption(option);
478478

479479
value.Should().Be(456);
480480
}
481-
482-
[Fact]
483-
public void Values_can_be_correctly_converted_to_int_without_the_parser_specifying_a_custom_converter()
484-
{
485-
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
486-
487-
var value = option.Parse("-x 123").ValueForOption<int>(option);
488-
489-
value.Should().Be(123);
490-
}
491-
492-
[Fact]
493-
public void Values_can_be_correctly_converted_to_nullable_int_with_no_value_without_the_parser_specifying_a_custom_converter()
494-
{
495-
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
496-
497-
var value = option.Parse("").ValueForOption<int?>("-x");
498-
499-
value.Should().BeNull();
500-
}
501-
502-
[Fact]
503-
public void Values_can_be_correctly_converted_to_nullable_int_with_a_value_without_the_parser_specifying_a_custom_converter()
504-
{
505-
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
506-
507-
var value = option.Parse("-x 123").ValueForOption<int?>(option);
508-
509-
value.Should().Be(123);
510-
}
511-
512-
[Fact]
513-
public void Values_can_be_correctly_converted_to_decimal_without_the_parser_specifying_a_custom_converter()
514-
{
515-
var option = new Option("-x", arity: ArgumentArity.ZeroOrOne);
516-
517-
var value = option.Parse("-x 123.456").ValueForOption<decimal>(option);
518-
519-
value.Should().Be(123.456m);
520-
}
521-
481+
522482
[Fact]
523483
public void Values_can_be_correctly_converted_to_double_without_the_parser_specifying_a_custom_converter()
524484
{

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,17 @@ 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");
264+
263265
var command = new Command("command")
264266
{
265-
new Option<int>("-x")
267+
option
266268
};
267269
command.Handler = CommandHandler.Create<ParseResult>(result => { boundParseResult = result; });
268270

269271
await command.InvokeAsync("command -x 123", _console);
270272

271-
boundParseResult.ValueForOption("-x").Should().Be(123);
273+
boundParseResult.ValueForOption(option).Should().Be(123);
272274
}
273275

274276
[Fact]
@@ -307,15 +309,17 @@ public async Task Method_parameters_of_type_InvocationContext_receive_the_curren
307309
{
308310
InvocationContext boundContext = default;
309311

312+
var option = new Option<int>("-x");
313+
310314
var command = new Command("command")
311315
{
312-
new Option<int>("-x")
316+
option
313317
};
314318
command.Handler = CommandHandler.Create<InvocationContext>(context => { boundContext = context; });
315319

316320
await command.InvokeAsync("command -x 123", _console);
317321

318-
boundContext.ParseResult.ValueForOption("-x").Should().Be(123);
322+
boundContext.ParseResult.ValueForOption(option).Should().Be(123);
319323
}
320324

321325

0 commit comments

Comments
 (0)