如何在Autorest中通过Directives实现PowerShell参数动态补全
配置Autorest实现PowerShell参数动态补全
核心配置逻辑
要让Get-BsnOsmCliNsi的NetsliceInstanceId参数调用Get-BsnOsmCliNSinstance实现动态补全,需在configuration.yaml中通过Autorest directives为目标参数添加[ArgumentCompleter]属性,将另一个cmdlet的输出作为补全数据源。
有效配置示例
方式一:全局匹配参数配置
在configuration.yaml中添加以下配置,匹配所有名为NetsliceInstanceId的参数:
directives: - from: swagger-document where: $.paths.*.parameters[?(@.name=='NetsliceInstanceId')] transform: > $["x-ms-parameter-info"] = $["x-ms-parameter-info"] || {}; $["x-ms-parameter-info"]["powershell"] = $["x-ms-parameter-info"]["powershell"] || {}; $["x-ms-parameter-info"]["powershell"]["argumentCompleter"] = { "scriptBlock": "[System.Management.Automation.ArgumentCompleterAttribute]::new({ param($wordToComplete, $commandAst, $cursorPosition) (Get-BsnOsmCliNSinstance).NetsliceInstanceId | Where-Object { $_ -like \"$wordToComplete*\" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } })" };
方式二:精准定位目标Cmdlet参数
如果仅需为Get-BsnOsmCliNsi的参数配置,可调整匹配条件指向对应接口路径:
directives: - from: swagger-document where: $.paths."/nsi/{NetsliceInstanceId}".get.parameters[?(@.name=='NetsliceInstanceId')] transform: > $["x-ms-parameter-info"] = $["x-ms-parameter-info"] || {}; $["x-ms-parameter-info"]["powershell"] = @{ argumentCompleter = @{ scriptBlock = "[System.Management.Automation.ArgumentCompleterAttribute]::new({ param($wordToComplete, $commandAst, $cursorPosition) (Get-BsnOsmCliNSinstance).NetsliceInstanceId | Where-Object { $_ -like `"$wordToComplete*`" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } })" } }
配置验证步骤
- 确认
Get-BsnOsmCliNSinstance能正常返回包含NetsliceInstanceId属性的对象,补全逻辑依赖该cmdlet的输出。 - 重新运行Autorest生成PowerShell代码,检查生成的
Get-BsnOsmCliNsi代码中,NetsliceInstanceId参数是否带有自动生成的[ArgumentCompleter]属性。 - 导入生成的PowerShell模块后,输入
Get-BsnOsmCliNsi -NetsliceInstanceId并按Tab键,即可触发动态补全。
内容的提问来源于stack exchange,提问作者Amir Hossein Mir Mohammad
相关产品推荐
相关产品推荐

