Skip to content

Commit 00efff5

Browse files
committed
Added changes for clu helps
1 parent 8a770bb commit 00efff5

File tree

2 files changed

+115
-10
lines changed

2 files changed

+115
-10
lines changed

src/CLU/Microsoft.CLU/CommandLineParser/UnixCommandLineParser.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public bool Parse(ICommandBinder commandBinder, string[] arguments)
5656
{
5757
if (commandBinder.SupportsAutomaticHelp && arg.Equals("--help", System.StringComparison.OrdinalIgnoreCase))
5858
{
59-
PresentCommandHelp(commandBinder, arguments.Take(position).ToArray(), false);
59+
PresentCommandHelp(arguments.Take(position).ToArray());
6060
return false;
6161
}
6262

@@ -125,7 +125,7 @@ public bool Parse(ICommandBinder commandBinder, string[] arguments)
125125
commandBinder.SupportsAutomaticHelp &&
126126
arg.Equals("help", System.StringComparison.OrdinalIgnoreCase))
127127
{
128-
PresentCommandHelp(commandBinder, arguments.Skip(position + 1).ToArray(), true);
128+
PresentCommandCompletion(arguments.Skip(position + 1).ToArray());
129129
return false;
130130
}
131131

@@ -183,17 +183,42 @@ public string FormatParameterName(string name, string type, bool isMandatory, bo
183183
return builder.ToString();
184184
}
185185

186-
private void PresentCommandHelp(ICommandBinder commandBinder, string[] arguments, bool prefix)
186+
private void PresentCommandCompletion(string[] arguments)
187+
{
188+
foreach (var cmd in Help.CommandDispatchHelper.CompleteCommands(CLUEnvironment.GetPackagesRootPath(), arguments).OrderBy((cmd) => cmd.Commandline))
189+
{
190+
Console.WriteLine(cmd.Commandline);
191+
}
192+
}
193+
194+
private void PresentCommandHelp(string[] arguments)
187195
{
188196
// BUGBUG - NYI!
189197
if (arguments.Length == 0)
190198
{
191-
var concatenatedArgs = string.Join(" ", arguments);
192-
Console.WriteLine($"Look up help corresponding to the arguments '{concatenatedArgs}'");
199+
Console.WriteLine("TODO: Present help for Azure itself...");
193200
}
194201
else
195-
{
196-
Console.WriteLine("Present help for Azure itself...");
202+
{
203+
var helpFile = Help.CommandDispatchHelper.FindBestHelp(CLUEnvironment.GetPackagesRootPath(), arguments);
204+
if (helpFile != null)
205+
{
206+
// Found appropriate help...
207+
var cmdLine = string.Join(" ", arguments);
208+
Console.WriteLine($"Help for command {cmdLine}");
209+
Console.WriteLine();
210+
211+
foreach (var line in helpFile.GetHelpContent())
212+
{
213+
Console.WriteLine(line);
214+
}
215+
return;
216+
}
217+
else
218+
{
219+
var joinedArgs = string.Join(" ", arguments);
220+
Console.WriteLine($"Unable to find help for command {joinedArgs}, type az help to see available commands");
221+
}
197222
}
198223
}
199224

src/CLU/Microsoft.CLU/Help/CommandDispatchHelper.cs

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)