@@ -20,101 +20,74 @@ public class SuggestionDispatcher
20
20
public SuggestionDispatcher ( ISuggestionRegistration suggestionRegistration , ISuggestionStore suggestionStore = null )
21
21
{
22
22
_suggestionRegistration = suggestionRegistration ?? throw new ArgumentNullException ( nameof ( suggestionRegistration ) ) ;
23
+
23
24
_suggestionStore = suggestionStore ?? new SuggestionStore ( ) ;
24
25
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" )
42
27
{
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 >
55
29
{
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 ) ;
58
34
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
+ } ;
65
41
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 ) ;
73
48
74
- Command CompleteScriptCommand ( )
49
+ RegisterCommand = new Command ( "register" , "Registers an app for suggestions" )
75
50
{
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 ) ;
89
56
90
- Command RegisterCommand ( )
57
+ var root = new RootCommand
91
58
{
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
+ }
93
75
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 ; }
103
77
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 ; }
110
79
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 ; }
118
91
119
92
public Parser Parser { get ; }
120
93
@@ -145,7 +118,7 @@ private void Register(
145
118
146
119
private void Get ( ParseResult parseResult , IConsole console )
147
120
{
148
- var commandPath = parseResult . ValueForOption < FileInfo > ( "-e" ) ;
121
+ var commandPath = parseResult . ValueForOption ( ExecutableOption ) ;
149
122
150
123
Registration suggestionRegistration ;
151
124
if ( commandPath . FullName == DotnetMuxer . Path . FullName )
@@ -157,7 +130,7 @@ private void Get(ParseResult parseResult, IConsole console)
157
130
suggestionRegistration = _suggestionRegistration . FindRegistration ( commandPath ) ;
158
131
}
159
132
160
- var position = parseResult . CommandResult [ "--position" ] ? . GetValueOrDefault < int > ( ) ?? short . MaxValue ;
133
+ var position = parseResult . ValueForOption ( PositionOption ) ;
161
134
162
135
if ( suggestionRegistration == null )
163
136
{
@@ -200,10 +173,9 @@ private static string ShellPrefixesToMatch(
200
173
201
174
IEnumerable < string > Prefixes ( )
202
175
{
203
-
204
176
foreach ( var r in registrations )
205
177
{
206
- var fileNameWithoutExtension = Path . GetFileNameWithoutExtension ( r . ExecutablePath ) ;
178
+ var fileNameWithoutExtension = Path . GetFileNameWithoutExtension ( r . ExecutablePath ) ;
207
179
208
180
yield return fileNameWithoutExtension ;
209
181
@@ -309,4 +281,4 @@ public static string FormatSuggestionArguments(
309
281
return $ "{ suggestDirective } \" { commandLine . Escape ( ) } \" ";
310
282
}
311
283
}
312
- }
284
+ }
0 commit comments