Skip to content

Commit a4f28f9

Browse files
committed
obsolete and stop using ParseResultExtensions.HasOption(ParseResult, string)
1 parent d4cdec2 commit a4f28f9

File tree

5 files changed

+80
-131
lines changed

5 files changed

+80
-131
lines changed

src/System.CommandLine.Tests/OptionTests.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,22 @@ public void When_an_option_alias_is_added_and_contains_whitespace_then_an_inform
207207
[InlineData("/")]
208208
public void When_options_use_different_prefixes_they_still_work(string prefix)
209209
{
210+
var optionA = new Option<string>(prefix + "a");
211+
var optionB = new Option(prefix + "b");
212+
var optionC = new Option<string>(prefix + "c");
213+
210214
var rootCommand = new RootCommand
211215
{
212-
new Option<string>(prefix + "a"),
213-
new Option(prefix + "b"),
214-
new Option<string>(prefix + "c")
216+
optionA,
217+
optionB,
218+
optionC
215219
};
220+
216221
var result = rootCommand.Parse(prefix + "c value-for-c " + prefix + "a value-for-a");
217222

218-
result.ValueForOption(prefix + "a").Should().Be("value-for-a");
219-
result.ValueForOption(prefix + "c").Should().Be("value-for-c");
220-
result.HasOption(prefix + "b").Should().BeFalse();
223+
result.ValueForOption(optionA).Should().Be("value-for-a");
224+
result.HasOption(optionB).Should().BeFalse();
225+
result.ValueForOption(optionC).Should().Be("value-for-c");
221226
}
222227

223228
[Fact]

src/System.CommandLine.Tests/SymbolResultTests.cs renamed to src/System.CommandLine.Tests/ParseResultTests.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace System.CommandLine.Tests
99
{
10-
public class SymbolResultTests
10+
public class ParseResultTests
1111
{
1212
[Fact]
1313
public void An_option_with_a_default_value_and_no_explicitly_provided_argument_has_an_empty_arguments_property()
@@ -26,27 +26,30 @@ public void An_option_with_a_default_value_and_no_explicitly_provided_argument_h
2626
[Fact]
2727
public void HasOption_can_be_used_to_check_the_presence_of_an_option()
2828
{
29+
var option = new Option(new[] { "-h", "--help" });
30+
2931
var command = new Command("the-command")
3032
{
31-
new Option(new[] { "-h", "--help" })
33+
option
3234
};
3335

3436
var result = command.Parse("the-command -h");
3537

36-
result.HasOption("--help").Should().BeTrue();
38+
result.HasOption(option).Should().BeTrue();
3739
}
3840

3941
[Fact]
4042
public void HasOption_can_be_used_to_check_the_presence_of_an_implicit_option()
4143
{
44+
var option = new Option<int>(new[] { "-c", "--count" }, () => 5);
4245
var command = new Command("the-command")
4346
{
44-
new Option<int>(new[] { "-c", "--count" }, () => 5)
47+
option
4548
};
4649

4750
var result = command.Parse("the-command");
4851

49-
result.HasOption("--count").Should().BeTrue();
52+
result.HasOption(option).Should().BeTrue();
5053
}
5154

5255
[Fact]

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 13 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ namespace System.CommandLine.Tests
1818
{
1919
public partial class ParserTests
2020
{
21-
[Fact]
22-
public void An_option_without_a_long_form_can_be_checked_for_using_a_prefix()
23-
{
24-
var option = new Option("--flag");
25-
26-
var result = option.Parse("--flag");
27-
28-
result.FindResultFor(option).Should().NotBeNull();
29-
}
30-
3121
[Fact]
3222
public void An_option_can_be_checked_by_object_instance()
3323
{
@@ -41,87 +31,19 @@ public void An_option_can_be_checked_by_object_instance()
4131
}
4232

4333
[Fact]
44-
public void An_option_without_a_long_form_can_be_checked_for_without_using_a_prefix()
45-
{
46-
var result = new Parser(
47-
new Option("--flag"))
48-
.Parse("--flag");
49-
50-
result.HasOption("--flag").Should().BeTrue();
51-
}
52-
53-
[Fact]
54-
public void When_invoked_by_its_short_form_an_option_with_an_alias_can_be_checked_for_by_its_short_form()
55-
{
56-
var result = new Parser(
57-
new Option(new[] { "-o", "--one" }))
58-
.Parse("-o");
59-
60-
result.HasOption("-o").Should().BeTrue();
61-
}
62-
63-
[Fact]
64-
public void When_invoked_by_its_long_form_an_option_with_an_alias_can_be_checked_for_by_its_short_form()
65-
{
66-
var result = new Parser(
67-
new Option(new[] { "-o", "--one" }))
68-
.Parse("--one");
69-
70-
result.HasOption("-o").Should().BeTrue();
71-
}
72-
73-
[Fact]
74-
public void When_invoked_by_its_short_form_an_option_with_an_alias_can_be_checked_for_by_its_long_form()
34+
public void Two_options_are_parsed_correctly()
7535
{
76-
var result = new Parser(
77-
new Option(new[] { "-o", "--one" }))
78-
.Parse("-o");
36+
var optionOne = new Option(new[] { "-o", "--one" });
7937

80-
result.HasOption("--one").Should().BeTrue();
81-
}
38+
var optionTwo = new Option(new[] { "-t", "--two" });
8239

83-
[Fact]
84-
public void When_invoked_by_its_long_form_an_option_with_an_alias_can_be_checked_for_by_its_long_form()
85-
{
8640
var result = new Parser(
87-
new Option(new[] { "-o", "--one" }))
88-
.Parse("--one");
89-
90-
result.HasOption("--one").Should().BeTrue();
91-
}
92-
93-
[Fact]
94-
public void Two_options_are_parsed_correctly()
95-
{
96-
ParseResult result = new Parser(
97-
new Option(
98-
new[] { "-o", "--one" }),
99-
new Option(
100-
new[] { "-t", "--two" })
101-
)
41+
optionOne,
42+
optionTwo)
10243
.Parse("-o -t");
10344

104-
result.HasOption("-o").Should().BeTrue();
105-
result.HasOption("--one").Should().BeTrue();
106-
result.HasOption("-t").Should().BeTrue();
107-
result.HasOption("--two").Should().BeTrue();
108-
}
109-
110-
[Fact]
111-
public void Parse_result_contains_arguments_to_options()
112-
{
113-
var optionOne = new Option(new[] { "-o", "--one" }) { Arity = ArgumentArity.ExactlyOne };
114-
115-
var optionTwo = new Option(new[] { "-t", "--two" }) { Arity = ArgumentArity.ExactlyOne };
116-
117-
var parser = new Parser(
118-
optionOne,
119-
optionTwo);
120-
121-
var result = parser.Parse("-o args_for_one -t args_for_two");
122-
123-
result.FindResultFor(optionOne).Tokens.Single().Value.Should().Be("args_for_one");
124-
result.FindResultFor(optionTwo).Tokens.Single().Value.Should().Be("args_for_two");
45+
result.HasOption(optionOne).Should().BeTrue();
46+
result.HasOption(optionTwo).Should().BeTrue();
12547
}
12648

12749
[Fact]
@@ -172,10 +94,11 @@ public void Two_options_cannot_have_conflicting_aliases()
17294
[Fact]
17395
public void A_double_dash_delimiter_specifies_that_no_further_command_line_args_will_be_treated_as_options()
17496
{
175-
var result = new Parser(new Option(new[] { "-o", "--one" }))
97+
var option = new Option(new[] { "-o", "--one" });
98+
var result = new Parser(option)
17699
.Parse("-o \"some stuff\" -- -x -y -z -o:foo");
177100

178-
result.HasOption("-o")
101+
result.HasOption(option)
179102
.Should()
180103
.BeTrue();
181104

@@ -1150,13 +1073,12 @@ public void Commands_can_have_default_argument_values()
11501073
public void When_an_option_with_a_default_value_is_not_matched_then_the_option_can_still_be_accessed_as_though_it_had_been_applied()
11511074
{
11521075
var command = new Command("command");
1153-
command.AddOption(
1154-
new Option<string>(new[] { "-o", "--option" }, () => "the-default"));
1076+
var option = new Option<string>(new[] { "-o", "--option" }, () => "the-default");
1077+
command.AddOption(option);
11551078

11561079
ParseResult result = command.Parse("command");
11571080

1158-
result.HasOption("-o").Should().BeTrue();
1159-
result.HasOption("--option").Should().BeTrue();
1081+
result.HasOption(option).Should().BeTrue();
11601082
result.ValueForOption<string>("-o").Should().Be("the-default");
11611083
result.ValueForOption("-o").Should().Be("the-default");
11621084
}

src/System.CommandLine.Tests/ResponseFileTests.cs

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ private string ResponseFile(params string[] lines)
4444
[Fact]
4545
public void When_response_file_specified_it_loads_options_from_response_file()
4646
{
47-
var result = new Option("--flag")
48-
.Parse($"@{ResponseFile("--flag")}");
47+
var option = new Option("--flag");
4948

50-
result.HasOption("--flag").Should().BeTrue();
49+
var result = option.Parse($"@{ResponseFile("--flag")}");
50+
51+
result.HasOption(option).Should().BeTrue();
5152
}
5253

5354
[Fact]
@@ -58,15 +59,18 @@ public void When_response_file_is_specified_it_loads_options_with_arguments_from
5859
"--flag2",
5960
"123");
6061

62+
var optionOne = new Option("--flag");
63+
64+
var optionTwo = new Option<int>("--flag2");
6165
var result = new RootCommand
6266
{
63-
new Option("--flag"),
64-
new Option<int>("--flag2")
67+
optionOne,
68+
optionTwo
6569
}
6670
.Parse($"@{responseFile}");
6771

68-
result.HasOption("--flag").Should().BeTrue();
69-
result.ValueForOption("--flag2").Should().Be(123);
72+
result.HasOption(optionOne).Should().BeTrue();
73+
result.ValueForOption(optionTwo).Should().Be(123);
7074
result.Errors.Should().BeEmpty();
7175
}
7276

@@ -180,6 +184,9 @@ public void Response_file_can_contain_blank_lines()
180184
[Fact]
181185
public void Response_file_can_contain_comments_which_are_ignored_when_loaded()
182186
{
187+
var optionOne = new Option("--flag");
188+
var optionTwo = new Option("--flag2");
189+
183190
var responseFile = ResponseFile(
184191
"# comment one",
185192
"--flag",
@@ -189,43 +196,49 @@ public void Response_file_can_contain_comments_which_are_ignored_when_loaded()
189196
"--flag2");
190197

191198
var result = new RootCommand
192-
{
193-
new Option("--flag"),
194-
new Option("--flag2")
195-
}.Parse($"@{responseFile}");
199+
{
200+
optionOne,
201+
optionTwo
202+
}.Parse($"@{responseFile}");
196203

197-
result.HasOption("--flag").Should().BeTrue();
198-
result.HasOption("--flag2").Should().BeTrue();
204+
result.HasOption(optionOne).Should().BeTrue();
205+
result.HasOption(optionTwo).Should().BeTrue();
199206
result.Errors.Should().BeEmpty();
200207
}
201208

202209
[Fact]
203210
public void When_response_file_does_not_exist_then_error_is_returned()
204211
{
212+
var optionOne = new Option("--flag");
213+
var optionTwo = new Option("--flag2");
214+
205215
var result = new RootCommand
206216
{
207-
new Option("--flag"),
208-
new Option("--flag2")
217+
optionOne,
218+
optionTwo
209219
}.Parse("@nonexistent.rsp");
210220

211-
result.HasOption("--flag").Should().BeFalse();
212-
result.HasOption("--flag2").Should().BeFalse();
221+
result.HasOption(optionOne).Should().BeFalse();
222+
result.HasOption(optionTwo).Should().BeFalse();
213223
result.Errors.Should().HaveCount(1);
214224
result.Errors.Single().Message.Should().Be("Response file not found 'nonexistent.rsp'");
215225
}
216226

217227
[Fact]
218228
public void When_response_filepath_is_not_specified_then_error_is_returned()
219229
{
230+
var optionOne = new Option("--flag");
231+
var optionTwo = new Option("--flag2");
232+
220233
var result = new RootCommand
221234
{
222-
new Option("--flag"),
223-
new Option("--flag2")
235+
optionOne,
236+
optionTwo
224237
}
225238
.Parse("@");
226239

227-
result.HasOption("--flag").Should().BeFalse();
228-
result.HasOption("--flag2").Should().BeFalse();
240+
result.HasOption(optionOne).Should().BeFalse();
241+
result.HasOption(optionTwo).Should().BeFalse();
229242
result.Errors.Should().HaveCount(1);
230243
result.Errors
231244
.Single()
@@ -238,17 +251,19 @@ public void When_response_filepath_is_not_specified_then_error_is_returned()
238251
public void When_response_file_cannot_be_read_then_specified_error_is_returned()
239252
{
240253
var nonexistent = Path.GetTempFileName();
254+
var optionOne = new Option("--flag");
255+
var optionTwo = new Option("--flag2");
241256

242257
using (File.Open(nonexistent, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
243258
{
244259
var result = new RootCommand
245260
{
246-
new Option("--flag"),
247-
new Option("--flag2")
261+
optionOne,
262+
optionTwo
248263
}.Parse($"@{nonexistent}");
249264

250-
result.HasOption("--flag").Should().BeFalse();
251-
result.HasOption("--flag2").Should().BeFalse();
265+
result.HasOption(optionOne).Should().BeFalse();
266+
result.HasOption(optionTwo).Should().BeFalse();
252267
result.Errors.Should().HaveCount(1);
253268
result.Errors.Single().Message.Should().StartWith($"Error reading response file '{nonexistent}'");
254269
}
@@ -262,19 +277,22 @@ public void When_response_file_parse_as_space_separated_returns_expected_values(
262277
{
263278
var responseFile = ResponseFile(input);
264279

280+
var optionOne = new Option<string>("--flag");
281+
var optionTwo = new Option<int>("--flag2");
282+
265283
var rootCommand = new RootCommand
266284
{
267-
new Option<string>("--flag"),
268-
new Option<int>("--flag2")
285+
optionOne,
286+
optionTwo
269287
};
270288
var parser = new CommandLineBuilder(rootCommand)
271289
.ParseResponseFileAs(ResponseFileHandling.ParseArgsAsSpaceSeparated)
272290
.Build();
273291

274292
var result = parser.Parse($"@{responseFile}");
275293

276-
result.ValueForOption("--flag").Should().Be("first value");
277-
result.ValueForOption("--flag2").Should().Be(123);
294+
result.ValueForOption(optionOne).Should().Be("first value");
295+
result.ValueForOption(optionTwo).Should().Be(123);
278296
}
279297

280298
[Fact]

src/System.CommandLine/Parsing/ParseResultExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public static bool HasOption(
202202
return parseResult.FindResultFor(option) is { };
203203
}
204204

205+
[Obsolete("This method is obsolete and will be removed in a future version. Please use ParseResultExtensions.HasOption(ParseResult, IOption) instead. For details see https://github.com/dotnet/command-line-api/issues/1127")]
205206
public static bool HasOption(
206207
this ParseResult parseResult,
207208
string alias)

0 commit comments

Comments
 (0)