Skip to content

Commit d626d4d

Browse files
committed
added type completion using "--complete" parameter
1 parent c93c6fb commit d626d4d

File tree

5 files changed

+90
-20
lines changed

5 files changed

+90
-20
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
this is poor "az --help"
3+
4+
help me build my content !!!!!!!!!!!!!

src/CLU/Microsoft.CLU/CommandBinder/CmdletBinderAndCommand.cs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,73 @@ public bool TryBindSwitch(string name)
172172
/// </summary>
173173
/// <param name="args">The command-line arguments to be considered in the help logic.</param>
174174
/// <returns>A list of lines containing help information.</returns>
175-
public IEnumerable<string> ListCommands(string[] args)
175+
public IEnumerable<string> ListCommands(string[] args, bool autoComplete)
176176
{
177177
#if PSCMDLET_HELP
178178
return CmdletHelp.Generate(parser.FormatParameterName, _discriminatorBinder.Modules, args, prefix);
179179
#else
180180
var commands = CommandDispatchHelper
181181
.CompleteCommands(new HelpPackageFinder(CLUEnvironment.GetPackagesRootPath()), args).ToArray();
182+
//
183+
string strCmdLine = string.Join(";", args);
184+
System.Collections.IDictionary commandPortion = new Dictionary<String, int>();
182185
if (commands.Length > 0)
183186
{
184187
foreach (var command in commands)
185188
{
186-
yield return command.Discriminators.Replace(';', ' ');
189+
if (!autoComplete) //--complete was not typed in commandline
190+
{
191+
yield return command.Discriminators.Replace(';', ' ');
192+
}
193+
else // "--complete" was typed in commandline
194+
{
195+
string strMatchedPart = command.Discriminators;
196+
string strUnmatchedPart = strMatchedPart.Substring(strCmdLine.Length);
197+
string result = "";
198+
int lastPosition, nextPosition;
199+
if (strUnmatchedPart.Length > 0)
200+
{
201+
bool isSemiColon = String.Equals(';', strUnmatchedPart.ElementAt(0));
202+
if (!isSemiColon)
203+
{
204+
//last command portion is incomplete, e.g "az vm c" so get back to previous position of ";"
205+
lastPosition = strCmdLine.LastIndexOf(";") + 1;
206+
nextPosition = strUnmatchedPart.IndexOf(";");
207+
if (nextPosition < 0)
208+
{
209+
result = strMatchedPart.Substring(lastPosition);//no command word portion left
210+
}
211+
else
212+
{
213+
//remaining command word
214+
result = strMatchedPart.Substring(lastPosition, nextPosition
215+
+ strCmdLine.Length - lastPosition);
216+
}
217+
}
218+
else
219+
{
220+
//last command portion is complete, e.g "az vm" so process next command word
221+
nextPosition = strUnmatchedPart.IndexOf(";", 1);
222+
if (nextPosition >= 0)
223+
{
224+
result = strUnmatchedPart.Substring(1, nextPosition - 1);//multiple command words exist
225+
}
226+
else
227+
{
228+
result = strUnmatchedPart.Substring(1); //only command word left
229+
}
230+
}
231+
}
232+
if (commandPortion.Contains(result))
233+
{
234+
continue;
235+
}
236+
else
237+
{
238+
commandPortion.Add(result, 1);
239+
yield return result.Replace(';', ' ');
240+
}
241+
}
187242
}
188243
}
189244
else
@@ -193,9 +248,9 @@ public IEnumerable<string> ListCommands(string[] args)
193248
#endif
194249
}
195250

196-
#endregion
251+
#endregion
197252

198-
#region ICommand implementation
253+
#region ICommand implementation
199254

200255
/// <summary>
201256
/// Tells whether the command is synchronous or asynchronous.
@@ -280,7 +335,7 @@ public Task InvokeAsync()
280335
throw new InvalidOperationException(Strings.CmdletBinderAndCommand_InvokeAsync_CmdletNotSupportAsyncInvoke);
281336
}
282337

283-
#endregion
338+
#endregion
284339

285340
public void MarkStaticParameterBindFinished()
286341
{
@@ -346,7 +401,7 @@ private Cmdlet CreateCmdlet(Type cmdletType, string assemblyLocation)
346401
/// <param name="cmdletType">The cmdlet types</param>
347402
private void InitCmdlet(Type cmdletType, string assemblyLocation)
348403
{
349-
_cmdlet = CreateCmdlet(cmdletType,assemblyLocation);
404+
_cmdlet = CreateCmdlet(cmdletType, assemblyLocation);
350405
_cmdletMetadata = CmdletMetadata.Load(_cmdlet);
351406
_staticParametersBindState = new ParameterBindState(_cmdletMetadata.Type);
352407
_staticParametersBindHandler = new BindHandler(_cmdlet, _staticParametersBindState);
@@ -665,6 +720,6 @@ private static bool MatchesParameterSet(ParameterMetadata parameter, string para
665720
/// </summary>
666721
private bool _positionalArgumentsBounded;
667722

668-
#endregion
723+
#endregion
669724
}
670725
}

src/CLU/Microsoft.CLU/CommandBinder/ICommandBinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ public interface ICommandBinder
3838
/// <summary>
3939
/// List the set of possible command matches for the given set of commands
4040
/// </summary>
41-
IEnumerable<string> ListCommands(string[] args);
41+
IEnumerable<string> ListCommands(string[] args, bool autoComplete);
4242
}
4343
}

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ public bool Parse(ICommandBinder commandBinder, string[] arguments)
4747
}
4848

4949
int position = 0;
50-
for (; _positionIndicator < arguments.Length; _positionIndicator++)
50+
int argLength = arguments.Length;
51+
if (String.Equals(arguments[argLength - 1], "--complete"))
52+
{
53+
argLength = argLength - 1;
54+
}
55+
for (; _positionIndicator < argLength; _positionIndicator++)
5156
{
5257
var arg = arguments[_positionIndicator];
5358

@@ -122,8 +127,8 @@ public bool Parse(ICommandBinder commandBinder, string[] arguments)
122127
}
123128
else
124129
{
125-
if (position == 0 &&
126-
commandBinder.SupportsAutomaticHelp &&
130+
if (position == 0 &&
131+
commandBinder.SupportsAutomaticHelp &&
127132
arg.Equals("help", System.StringComparison.OrdinalIgnoreCase))
128133
{
129134
PresentCommandCompletion(arguments.Skip(position + 1).ToArray());
@@ -167,7 +172,7 @@ public void SeekBack(uint offset)
167172
/// </example>
168173
public string FormatParameterName(string name, string type, bool isMandatory, bool isPositional)
169174
{
170-
Func<string,string> nameFunc = n => n.Length == 1 ? "-" + n : "--" + n.ToLowerInvariant();
175+
Func<string, string> nameFunc = n => n.Length == 1 ? "-" + n : "--" + n.ToLowerInvariant();
171176

172177
var builder = new System.Text.StringBuilder();
173178
if (!isMandatory)
@@ -202,7 +207,8 @@ private void PresentCommandHelp(string[] arguments)
202207
String rootPath = CLUEnvironment.GetPackagesRootPath();
203208
try
204209
{
205-
var files = from file in System.IO.Directory.EnumerateFiles(@rootPath, "az.hlp", System.IO.SearchOption.AllDirectories)
210+
var files = from file in System.IO.Directory.EnumerateFiles(@rootPath, "az.hlp",
211+
System.IO.SearchOption.AllDirectories)
206212
from line in System.IO.File.ReadLines(file)
207213
select new
208214
{
@@ -219,13 +225,14 @@ from line in System.IO.File.ReadLines(file)
219225
{
220226
Console.WriteLine(e.Message);
221227
}
222-
228+
223229
return;
224-
230+
225231
}
226232
else
227-
{
228-
var helpFile = CommandDispatchHelper.FindBestHelp(new HelpPackageFinder(CLUEnvironment.GetPackagesRootPath()), arguments);
233+
{
234+
var helpFile = CommandDispatchHelper.FindBestHelp(new HelpPackageFinder
235+
(CLUEnvironment.GetPackagesRootPath()), arguments);
229236
if (helpFile != null)
230237
{
231238
// Found appropriate help...

src/CLU/Microsoft.CLU/CommandModel/CmdletCommandModel.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ public CommandModelErrorCode Run(ConfigurationDictionary commandConfiguration, s
2323
{
2424
Debug.Assert(commandConfiguration != null);
2525
Debug.Assert(arguments != null);
26-
2726
if (arguments.Length < 1)
2827
{
2928
throw new ArgumentException(Strings.CmdletCommandModel_Run_MissingMinimalArguments);
3029
}
31-
30+
bool isComplete = false;
31+
//assuming --complete will always be typed at the end
32+
if (String.Equals(arguments[arguments.Length - 1], "--complete"))
33+
{
34+
isComplete = true;
35+
}
3236
Init(commandConfiguration);
3337
IPipe<string> pipe = new ConsolePipe(CLUEnvironment.Console);
3438
HostStreamInfo hostStreamInfo = new HostStreamInfo
@@ -92,7 +96,7 @@ public CommandModelErrorCode Run(ConfigurationDictionary commandConfiguration, s
9296
}
9397
catch (CommandNotFoundException)
9498
{
95-
var helplines = binderAndCommand.ListCommands(binderAndCommand.Discriminators.ToArray());
99+
var helplines = binderAndCommand.ListCommands(binderAndCommand.Discriminators.ToArray(), isComplete);
96100
foreach (var entry in helplines)
97101
{
98102
CLUEnvironment.Console.WriteLine(entry);

0 commit comments

Comments
 (0)