@@ -27,7 +27,7 @@ public static IEnumerable<CommandInfo> FindCommands(string pkgRoot, string[] arg
2727 return CommandDispatchHelper . MatchScore ( cmd , semiColonSeparatedArgs ) >= cmd . Length ;
2828 } ;
2929
30- return FindMatches ( pkgRoot , args , matcher ) ;
30+ return FindCommandMatches ( pkgRoot , args , matcher ) ;
3131 }
3232
3333 /// <summary>
@@ -51,10 +51,34 @@ public static IEnumerable<CommandInfo> CompleteCommands(string pkgRoot, string[]
5151 return CommandDispatchHelper . MatchScore ( cmd , semiColonSeparatedArgs ) >= semiColonSeparatedArgs . Length ;
5252 } ;
5353
54- return FindMatches ( pkgRoot , args , matcher ) ;
54+ return FindCommandMatches ( pkgRoot , args , matcher ) ;
5555 }
5656
57- public static IEnumerable < CommandInfo > FindMatches ( string pkgRoot , string [ ] args , Func < string , bool > matchFunc )
57+ public static HelpInfo FindBestHelp ( string pkgRoot , string [ ] args )
58+ {
59+ var semiColonSeparatedArgs = String . Join ( ";" , args ) + ";" ;
60+
61+ Func < string , bool > matcher = ( hlp ) =>
62+ {
63+ return hlp . Length <= semiColonSeparatedArgs . Length && CommandDispatchHelper . MatchScore ( hlp , semiColonSeparatedArgs ) == hlp . Length ;
64+ } ;
65+
66+ return FindHelpMatches ( pkgRoot , args , matcher ) . OrderByDescending ( ( hi ) => CommandDispatchHelper . MatchScore ( hi . Discriminators , semiColonSeparatedArgs ) ) . ThenBy ( ( hi ) => hi . Discriminators ) . FirstOrDefault ( ) ;
67+ }
68+
69+ public static IEnumerable < HelpInfo > FindHelpMatches ( string pkgRoot , string [ ] args , Func < string , bool > matchFunc )
70+ {
71+ foreach ( var helpInfo in GetPackages ( pkgRoot ) . SelectMany ( ( p ) => p . GetHelp ( ) ) )
72+ {
73+ var semiColonSeparatedCommand = helpInfo . Discriminators + ";" ;
74+ if ( matchFunc ( semiColonSeparatedCommand ) )
75+ {
76+ yield return helpInfo ;
77+ }
78+ }
79+ }
80+
81+ public static IEnumerable < CommandInfo > FindCommandMatches ( string pkgRoot , string [ ] args , Func < string , bool > matchFunc )
5882 {
5983 foreach ( var commandIndex in GetCommandIndexes ( pkgRoot ) . SelectMany ( ( s ) => { return s ; } ) )
6084 {
@@ -130,6 +154,13 @@ public string IndexPath
130154 }
131155 }
132156
157+ public string HelpPath
158+ {
159+ get
160+ {
161+ return System . IO . Path . Combine ( Path , Version , "content" , "help" ) ;
162+ }
163+ }
133164 public virtual IEnumerable < CommandInfo > GetCommands ( )
134165 {
135166 if ( System . IO . File . Exists ( IndexPath ) )
@@ -145,12 +176,61 @@ public virtual IEnumerable<CommandInfo> GetCommands()
145176 return new CommandInfo [ ] { } ;
146177 }
147178 }
179+
180+ public virtual IEnumerable < HelpInfo > GetHelp ( )
181+ {
182+ if ( System . IO . Directory . Exists ( HelpPath ) )
183+ {
184+ return System . IO . Directory . EnumerateFiles ( HelpPath , "*.hlp" , System . IO . SearchOption . TopDirectoryOnly ) . Select ( ( f ) =>
185+ {
186+ return new HelpInfo ( f ) ;
187+ } ) ;
188+ }
189+ else
190+ {
191+ return new HelpInfo [ ] { } ;
192+ }
193+ }
194+ }
195+
196+ public class HelpInfo
197+ {
198+ private string _path ;
199+
200+ public HelpInfo ( string path )
201+ {
202+ _path = path ;
203+
204+ Discriminators = System . IO . Path . GetFileNameWithoutExtension ( path ) . Replace ( '.' , ';' ) ;
205+ }
206+
207+ public IEnumerable < string > GetHelpContent ( )
208+ {
209+ if ( System . IO . File . Exists ( _path ) )
210+ {
211+ return System . IO . File . ReadAllLines ( _path ) ;
212+ }
213+ else
214+ {
215+ return new string [ ] { } ;
216+ }
217+ }
218+
219+ public string Discriminators { get ; private set ; }
148220 }
149221
150222 public class CommandInfo
151223 {
152224 public PkgInfo Package { get ; set ; }
153225 public string Discriminators { get ; set ; }
226+
227+ public string Commandline
228+ {
229+ get
230+ {
231+ return Discriminators . Replace ( ';' , ' ' ) ;
232+ }
233+ }
154234 }
155235 }
156236}
0 commit comments