@@ -670,14 +670,20 @@ private static List<CompletionResult> GetParameterCompletionResults(string param
670670 {
671671 Diagnostics . Assert ( bindingInfo . InfoType . Equals ( PseudoBindingInfoType . PseudoBindingSucceed ) , "The pseudo binding should succeed" ) ;
672672 List < CompletionResult > result = new List < CompletionResult > ( ) ;
673+ Assembly commandAssembly = null ;
674+ if ( bindingInfo . CommandInfo is CmdletInfo cmdletInfo )
675+ {
676+ commandAssembly = cmdletInfo . CommandMetadata . CommandType . Assembly ;
677+ }
673678
674679 if ( parameterName == string . Empty )
675680 {
676681 result = GetParameterCompletionResults (
677682 parameterName ,
678683 bindingInfo . ValidParameterSetsFlags ,
679684 bindingInfo . UnboundParameters ,
680- withColon ) ;
685+ withColon ,
686+ commandAssembly ) ;
681687 return result ;
682688 }
683689
@@ -699,7 +705,8 @@ private static List<CompletionResult> GetParameterCompletionResults(string param
699705 parameterName ,
700706 bindingInfo . ValidParameterSetsFlags ,
701707 bindingInfo . UnboundParameters ,
702- withColon ) ;
708+ withColon ,
709+ commandAssembly ) ;
703710 }
704711
705712 return result ;
@@ -714,7 +721,8 @@ private static List<CompletionResult> GetParameterCompletionResults(string param
714721 parameterName ,
715722 bindingInfo . ValidParameterSetsFlags ,
716723 bindingInfo . BoundParameters . Values ,
717- withColon ) ;
724+ withColon ,
725+ commandAssembly ) ;
718726 }
719727
720728 return result ;
@@ -778,19 +786,34 @@ private static List<CompletionResult> GetParameterCompletionResults(string param
778786 parameterName ,
779787 bindingInfo . ValidParameterSetsFlags ,
780788 bindingInfo . UnboundParameters ,
781- withColon ) ;
789+ withColon ,
790+ commandAssembly ) ;
782791 return result ;
783792 }
784793
785794 MergedCompiledCommandParameter param = bindingInfo . BoundParameters [ matchedParameterName ] ;
786795
787796 WildcardPattern pattern = WildcardPattern . Get ( parameterName + "*" , WildcardOptions . IgnoreCase ) ;
788797 string parameterType = "[" + ToStringCodeMethods . Type ( param . Parameter . Type , dropNamespaces : true ) + "] " ;
798+
799+ string helpMessage = string . Empty ;
800+ if ( param . Parameter . CompiledAttributes is not null )
801+ {
802+ foreach ( Attribute attr in param . Parameter . CompiledAttributes )
803+ {
804+ if ( attr is ParameterAttribute pattr && TryGetParameterHelpMessage ( pattr , commandAssembly , out string attrHelpMessage ) )
805+ {
806+ helpMessage = $ " - { attrHelpMessage } ";
807+ break ;
808+ }
809+ }
810+ }
811+
789812 string colonSuffix = withColon ? ":" : string . Empty ;
790813 if ( pattern . IsMatch ( matchedParameterName ) )
791814 {
792- string completionText = "-" + matchedParameterName + colonSuffix ;
793- string tooltip = parameterType + matchedParameterName ;
815+ string completionText = $ "- { matchedParameterName } { colonSuffix } " ;
816+ string tooltip = $ " { parameterType } { matchedParameterName } { helpMessage } " ;
794817 result . Add ( new CompletionResult ( completionText , matchedParameterName , CompletionResultType . ParameterName , tooltip ) ) ;
795818 }
796819 else
@@ -804,27 +827,67 @@ private static List<CompletionResult> GetParameterCompletionResults(string param
804827 $ "-{ alias } { colonSuffix } ",
805828 alias ,
806829 CompletionResultType . ParameterName ,
807- parameterType + alias ) ) ;
830+ $ " { parameterType } { alias } { helpMessage } " ) ) ;
808831 }
809832 }
810833 }
811834
812835 return result ;
813836 }
814837
838+ #nullable enable
839+ /// <summary>
840+ /// Try and get the help message text for the parameter attribute.
841+ /// </summary>
842+ /// <param name="attr">The attribute to check for the help message.</param>
843+ /// <param name="assembly">The assembly to lookup resources messages, this should be the assembly the cmdlet is defined in.</param>
844+ /// <param name="message">The help message if it was found otherwise null.</param>
845+ /// <returns>True if the help message was set or false if not.></returns>
846+ private static bool TryGetParameterHelpMessage (
847+ ParameterAttribute attr ,
848+ Assembly ? assembly ,
849+ [ NotNullWhen ( true ) ] out string ? message )
850+ {
851+ message = null ;
852+
853+ if ( attr . HelpMessage is not null )
854+ {
855+ message = attr . HelpMessage ;
856+ return true ;
857+ }
858+
859+ if ( assembly is null || attr . HelpMessageBaseName is null || attr . HelpMessageResourceId is null )
860+ {
861+ return false ;
862+ }
863+
864+ try
865+ {
866+ message = ResourceManagerCache . GetResourceString ( assembly , attr . HelpMessageBaseName , attr . HelpMessageResourceId ) ;
867+ return message is not null ;
868+ }
869+ catch ( Exception )
870+ {
871+ return false ;
872+ }
873+ }
874+ #nullable disable
875+
815876 /// <summary>
816877 /// Get the parameter completion results by using the given valid parameter sets and available parameters.
817878 /// </summary>
818879 /// <param name="parameterName"></param>
819880 /// <param name="validParameterSetFlags"></param>
820881 /// <param name="parameters"></param>
821882 /// <param name="withColon"></param>
883+ /// <param name="commandAssembly">Optional assembly used to lookup parameter help messages.</param>
822884 /// <returns></returns>
823885 private static List < CompletionResult > GetParameterCompletionResults (
824886 string parameterName ,
825887 uint validParameterSetFlags ,
826888 IEnumerable < MergedCompiledCommandParameter > parameters ,
827- bool withColon )
889+ bool withColon ,
890+ Assembly commandAssembly = null )
828891 {
829892 var result = new List < CompletionResult > ( ) ;
830893 var commonParamResult = new List < CompletionResult > ( ) ;
@@ -840,6 +903,7 @@ private static List<CompletionResult> GetParameterCompletionResults(
840903
841904 string name = param . Parameter . Name ;
842905 string type = "[" + ToStringCodeMethods . Type ( param . Parameter . Type , dropNamespaces : true ) + "] " ;
906+ string helpMessage = null ;
843907 bool isCommonParameter = Cmdlet . CommonParameters . Contains ( name , StringComparer . OrdinalIgnoreCase ) ;
844908 List < CompletionResult > listInUse = isCommonParameter ? commonParamResult : result ;
845909
@@ -855,20 +919,27 @@ private static List<CompletionResult> GetParameterCompletionResults(
855919 {
856920 foreach ( var attr in compiledAttributes )
857921 {
858- var pattr = attr as ParameterAttribute ;
859- if ( pattr != null && pattr . DontShow )
922+ if ( attr is ParameterAttribute pattr )
860923 {
861- showToUser = false ;
862- addCommonParameters = false ;
863- break ;
924+ if ( pattr . DontShow )
925+ {
926+ showToUser = false ;
927+ addCommonParameters = false ;
928+ break ;
929+ }
930+
931+ if ( helpMessage is null && TryGetParameterHelpMessage ( pattr , commandAssembly , out string attrHelpMessage ) )
932+ {
933+ helpMessage = $ " - { attrHelpMessage } ";
934+ }
864935 }
865936 }
866937 }
867938
868939 if ( showToUser )
869940 {
870- string completionText = "-" + name + colonSuffix ;
871- string tooltip = type + name ;
941+ string completionText = $ "- { name } { colonSuffix } " ;
942+ string tooltip = $ " { type } { name } { helpMessage } " ;
872943 listInUse . Add ( new CompletionResult ( completionText , name , CompletionResultType . ParameterName ,
873944 tooltip ) ) ;
874945 }
0 commit comments