Skip to content

Commit a2e5425

Browse files
authored
Remove string-based indexers (#1134)
* remove usage of string-based lookup methods from dotnet-suggest * remove ParseResult[string alias] indexer * remove CommandResult[string alias] indexer
1 parent c25f85b commit a2e5425

File tree

7 files changed

+352
-413
lines changed

7 files changed

+352
-413
lines changed

src/System.CommandLine.Suggest/SuggestionDispatcher.cs

Lines changed: 59 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -20,101 +20,74 @@ public class SuggestionDispatcher
2020
public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISuggestionStore suggestionStore = null)
2121
{
2222
_suggestionRegistration = suggestionRegistration ?? throw new ArgumentNullException(nameof(suggestionRegistration));
23+
2324
_suggestionStore = suggestionStore ?? new SuggestionStore();
2425

25-
Parser = new CommandLineBuilder()
26-
.UseVersionOption()
27-
.UseHelp()
28-
.UseParseDirective()
29-
.UseDebugDirective()
30-
.UseSuggestDirective()
31-
.UseParseErrorReporting()
32-
.UseExceptionHandler()
33-
34-
.AddCommand(ListCommand())
35-
.AddCommand(GetCommand())
36-
.AddCommand(RegisterCommand())
37-
.AddCommand(CompleteScriptCommand())
38-
39-
.Build();
40-
41-
Command GetCommand()
26+
CompleteScriptCommand = new Command("script", "Print complete script for specific shell")
4227
{
43-
var command = new Command("get")
44-
{
45-
ExecutableOption(),
46-
PositionOption()
47-
};
48-
command.Description = "Gets suggestions from the specified executable";
49-
command.Handler = CommandHandler.Create<ParseResult, IConsole>(Get);
50-
return command;
51-
}
52-
53-
Option ExecutableOption() =>
54-
new Option(new[] { "-e", "--executable" })
28+
new Argument<ShellType>
5529
{
56-
Argument = new Argument<string>().LegalFilePathsOnly(), Description = "The executable to call for suggestions"
57-
};
30+
Name = nameof(ShellType)
31+
}
32+
};
33+
CompleteScriptCommand.Handler = CommandHandler.Create<IConsole, ShellType>(SuggestionShellScriptHandler.Handle);
5834

59-
Option PositionOption() =>
60-
new Option(new[] { "-p", "--position" })
61-
{
62-
Argument = new Argument<int>(),
63-
Description = "The current character position on the command line"
64-
};
35+
ListCommand = new Command("list")
36+
{
37+
Description = "Lists apps registered for suggestions",
38+
Handler = CommandHandler.Create<IConsole>(
39+
c => c.Out.WriteLine(ShellPrefixesToMatch(_suggestionRegistration)))
40+
};
6541

66-
Command ListCommand() =>
67-
new Command("list")
68-
{
69-
Description = "Lists apps registered for suggestions",
70-
Handler = CommandHandler.Create<IConsole>(
71-
c => c.Out.WriteLine(ShellPrefixesToMatch(_suggestionRegistration)))
72-
};
42+
GetCommand = new Command("get", "Gets suggestions from the specified executable")
43+
{
44+
ExecutableOption,
45+
PositionOption
46+
};
47+
GetCommand.Handler = CommandHandler.Create<ParseResult, IConsole>(Get);
7348

74-
Command CompleteScriptCommand()
49+
RegisterCommand = new Command("register", "Registers an app for suggestions")
7550
{
76-
var command = new Command("script")
77-
{
78-
new Argument<ShellType>
79-
{
80-
Name = nameof(ShellType)
81-
}
82-
};
83-
84-
command.Handler = CommandHandler.Create<IConsole, ShellType>(SuggestionShellScriptHandler.Handle);
85-
command.Description = "Print complete script for specific shell";
86-
87-
return command;
88-
}
51+
new Option<string>("--command-path", "The path to the command for which to register suggestions"),
52+
new Option<string>("--suggestion-command", "The command to invoke to retrieve suggestions")
53+
};
54+
55+
RegisterCommand.Handler = CommandHandler.Create<string, string, IConsole>(Register);
8956

90-
Command RegisterCommand()
57+
var root = new RootCommand
9158
{
92-
var description = "Registers an app for suggestions";
59+
ListCommand,
60+
GetCommand,
61+
RegisterCommand,
62+
CompleteScriptCommand
63+
};
64+
65+
Parser = new CommandLineBuilder(root)
66+
.UseVersionOption()
67+
.UseHelp()
68+
.UseParseDirective()
69+
.UseDebugDirective()
70+
.UseSuggestDirective()
71+
.UseParseErrorReporting()
72+
.UseExceptionHandler()
73+
.Build();
74+
}
9375

94-
var command = new Command("register")
95-
{
96-
Description = description,
97-
Handler = CommandHandler.Create<string, string, IConsole>(Register)
98-
};
99-
command.Add(CommandPathOption());
100-
command.Add(SuggestionCommandOption());
101-
return command;
102-
}
76+
private Command CompleteScriptCommand { get; }
10377

104-
Option CommandPathOption() =>
105-
new Option("--command-path")
106-
{
107-
Argument = new Argument<string>(),
108-
Description = "The path to the command for which to register suggestions"
109-
};
78+
private Command GetCommand { get; }
11079

111-
Option SuggestionCommandOption() =>
112-
new Option("--suggestion-command")
113-
{
114-
Argument = new Argument<string>(),
115-
Description = "The command to invoke to retrieve suggestions"
116-
};
117-
}
80+
private Option<FileInfo> ExecutableOption { get; } =
81+
new Option<FileInfo>(new[] { "-e", "--executable" }, "The executable to call for suggestions")
82+
.LegalFilePathsOnly();
83+
84+
private Command ListCommand { get; }
85+
86+
private Option<int> PositionOption { get; } = new Option<int>(new[] { "-p", "--position" },
87+
description: "The current character position on the command line",
88+
getDefaultValue: () => short.MaxValue);
89+
90+
private Command RegisterCommand { get; }
11891

11992
public Parser Parser { get; }
12093

@@ -145,7 +118,7 @@ private void Register(
145118

146119
private void Get(ParseResult parseResult, IConsole console)
147120
{
148-
var commandPath = parseResult.ValueForOption<FileInfo>("-e");
121+
var commandPath = parseResult.ValueForOption(ExecutableOption);
149122

150123
Registration suggestionRegistration;
151124
if (commandPath.FullName == DotnetMuxer.Path.FullName)
@@ -157,7 +130,7 @@ private void Get(ParseResult parseResult, IConsole console)
157130
suggestionRegistration = _suggestionRegistration.FindRegistration(commandPath);
158131
}
159132

160-
var position = parseResult.CommandResult["--position"]?.GetValueOrDefault<int>() ?? short.MaxValue;
133+
var position = parseResult.ValueForOption(PositionOption);
161134

162135
if (suggestionRegistration == null)
163136
{
@@ -200,10 +173,9 @@ private static string ShellPrefixesToMatch(
200173

201174
IEnumerable<string> Prefixes()
202175
{
203-
204176
foreach (var r in registrations)
205177
{
206-
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension (r.ExecutablePath);
178+
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(r.ExecutablePath);
207179

208180
yield return fileNameWithoutExtension;
209181

@@ -309,4 +281,4 @@ public static string FormatSuggestionArguments(
309281
return $"{suggestDirective} \"{commandLine.Escape()}\"";
310282
}
311283
}
312-
}
284+
}

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

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -143,37 +143,31 @@ public void Argument_bool_will_default_to_true_when_no_argument_is_passed()
143143
[Fact]
144144
public void Argument_parses_as_the_default_value_when_the_option_has_not_been_applied()
145145
{
146+
var option = new Option<int>("-x", () => 123);
147+
146148
var command = new Command("something")
147149
{
148-
new Option("-x")
149-
{
150-
Argument = new Argument<int>(() => 123)
151-
}
150+
option
152151
};
153152

154153
var result = command.Parse("something");
155154

156-
var option = result.CommandResult["-x"];
157-
158-
option.GetValueOrDefault().Should().Be(123);
155+
result.ValueForOption(option).Should().Be(123);
159156
}
160157

161158
[Fact]
162159
public void Argument_does_not_parse_as_the_default_value_when_the_option_has_been_applied()
163160
{
161+
var option = new Option<int>("-x", () => 123);
162+
164163
var command = new Command("something")
165164
{
166-
new Option("-x")
167-
{
168-
Argument = new Argument<int>(() => 123)
169-
}
165+
option
170166
};
171167

172168
var result = command.Parse("something -x 456");
173169

174-
var option = result.CommandResult["-x"];
175-
176-
option.GetValueOrDefault().Should().Be(456);
170+
result.ValueForOption(option).Should().Be(456);
177171
}
178172

179173
[Theory]
@@ -183,18 +177,16 @@ public void Argument_does_not_parse_as_the_default_value_when_the_option_has_bee
183177
[InlineData("the-command -x=true")]
184178
public void Bool_does_not_parse_as_the_default_value_when_the_option_has_been_applied(string commandLine)
185179
{
180+
var option = new Option<bool>("-x");
181+
186182
var command = new Command("the-command")
187183
{
188-
new Option("-x")
189-
{
190-
Argument = new Argument<bool>(() => false)
191-
}
184+
option
192185
};
193186

194187
command
195188
.Parse(commandLine)
196-
.CommandResult["-x"]
197-
.GetValueOrDefault()
189+
.ValueForOption(option)
198190
.Should()
199191
.Be(true);
200192
}
@@ -449,61 +441,51 @@ public void By_default_an_option_without_arguments_parses_as_false_when_it_is_no
449441
[Fact]
450442
public void An_option_with_a_default_value_parses_as_the_default_value_when_the_option_has_not_been_applied()
451443
{
444+
var option = new Option<string>("-x", () => "123");
445+
452446
var command = new Command("something")
453447
{
454-
new Option("-x")
455-
{
456-
Argument = new Argument<string>(() => "123")
457-
}
448+
option
458449
};
459450

460451
var result = command.Parse("something");
461452

462-
var option = result.CommandResult["-x"];
463-
464-
option.GetValueOrDefault()
453+
result.ValueForOption(option)
465454
.Should()
466455
.Be("123");
467456
}
468457

469458
[Fact]
470459
public void A_default_value_of_a_non_string_type_can_be_specified()
471460
{
461+
var option = new Option<int>("-x", () => 123);
462+
472463
var command = new Command("something")
473464
{
474-
new Option("-x")
475-
{
476-
Argument = new Argument<int>(() => 123)
477-
}
465+
option
478466
};
479467

480-
var result = command.Parse("something");
481-
482-
var option = result.CommandResult["-x"];
483-
484-
option.GetValueOrDefault()
485-
.Should()
486-
.Be(123);
468+
command.Parse("something")
469+
.ValueForOption(option)
470+
.Should()
471+
.Be(123);
487472
}
488473

489474
[Fact]
490475
public void A_default_value_with_a_custom_constructor_can_be_specified_for_an_option_argument()
491476
{
492477
var directoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
493478

479+
var option = new Option<DirectoryInfo>("-x", () => directoryInfo);
480+
494481
var command = new Command("something")
495482
{
496-
new Option("-x")
497-
{
498-
Argument = new Argument<DirectoryInfo>(() => directoryInfo)
499-
}
483+
option
500484
};
501485

502486
var result = command.Parse("something");
503487

504-
var option = result.CommandResult["-x"];
505-
506-
option.GetValueOrDefault<DirectoryInfo>().Should().Be(directoryInfo);
488+
result.ValueForOption(option).Should().Be(directoryInfo);
507489
}
508490

509491
[Fact]
@@ -606,7 +588,7 @@ public void Values_can_be_correctly_converted_to_nullable_int_with_a_value_witho
606588
}
607589
};
608590

609-
var value = option.Parse("-x 123").ValueForOption<int?>("-x");
591+
var value = option.Parse("-x 123").ValueForOption<int?>(option);
610592

611593
value.Should().Be(123);
612594
}
@@ -622,7 +604,7 @@ public void Values_can_be_correctly_converted_to_decimal_without_the_parser_spec
622604
}
623605
};
624606

625-
var value = option.Parse("-x 123.456").ValueForOption<decimal>("-x");
607+
var value = option.Parse("-x 123.456").ValueForOption<decimal>(option);
626608

627609
value.Should().Be(123.456m);
628610
}

0 commit comments

Comments
 (0)