1
1
using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
2
2
using Microsoft . Azure . Commands . Common . Authentication . Models ;
3
3
using Microsoft . Azure . Commands . ResourceManager . Common ;
4
+ using System . Collections ;
5
+ using System . Collections . Generic ;
4
6
using System . Linq ;
5
7
using System . Management . Automation ;
8
+ using System . Management . Automation . Language ;
6
9
7
10
namespace Microsoft . Azure . Commands . Profile . Common
8
11
{
9
12
/// <summary>
10
13
/// This attribute will allow the user to autocomplete the values for valid Azure Environment names when applied to Environment related cmdlet parameters.
11
14
/// </summary>
12
- public class EnvironmentCompleterAttribute : ArgumentCompleterAttribute
15
+ public class EnvironmentCompleterAttribute : ArgumentCompleterAttribute , IArgumentCompleter
13
16
{
14
17
/// <summary>
15
18
/// Initializes a new instance of <see cref="EnvironmentCompleterAttribute" /> .
16
19
/// </summary>
17
- public EnvironmentCompleterAttribute ( ) : base ( CreateScriptBlock ( ) )
20
+ public EnvironmentCompleterAttribute ( ) : base ( typeof ( EnvironmentCompleterAttribute ) )
18
21
{
19
22
}
20
23
@@ -28,13 +31,16 @@ public static string[] GetEnvironments()
28
31
return profileClient . ListEnvironments ( null ) . Select ( x => x . Name ) . ToArray ( ) ;
29
32
}
30
33
31
- private static ScriptBlock CreateScriptBlock ( )
34
+ /// <summary>
35
+ /// Implementations CompleteArgument function of the <see cref="IArgumentCompleter"/>.
36
+ /// </summary>
37
+ public IEnumerable < CompletionResult > CompleteArgument ( string commandName , string parameterName , string wordToComplete , CommandAst commandAst , IDictionary fakeBoundParameters )
32
38
{
33
- string script = "param($commandName, $parameterName, $ wordToComplete, $commandAst, $fakeBoundParameter) \n " +
34
- "$environments = [Microsoft.Azure.Commands.Profile.Common.EnvironmentCompleterAttribute]::GetEnvironments() \n " +
35
- "$environments | Where-Object { $_ -Like \" $wordToComplete* \" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }" ;
36
- ScriptBlock scriptBlock = ScriptBlock . Create ( script ) ;
37
- return scriptBlock ;
39
+ IEnumerable < string > names = GetEnvironments ( ) . Where ( env => env . ToLower ( ) . StartsWith ( wordToComplete . ToLower ( ) ) ) ;
40
+ foreach ( string name in names )
41
+ {
42
+ yield return new CompletionResult ( name , name , CompletionResultType . ParameterValue , name ) ;
43
+ }
38
44
}
39
45
}
40
46
}
0 commit comments