You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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', $_) } })"
        }
      }

配置验证步骤

  1. 确认Get-BsnOsmCliNSinstance能正常返回包含NetsliceInstanceId属性的对象,补全逻辑依赖该cmdlet的输出。
  2. 重新运行Autorest生成PowerShell代码,检查生成的Get-BsnOsmCliNsi代码中,NetsliceInstanceId参数是否带有自动生成的[ArgumentCompleter]属性。
  3. 导入生成的PowerShell模块后,输入Get-BsnOsmCliNsi -NetsliceInstanceId 并按Tab键,即可触发动态补全。

内容的提问来源于stack exchange,提问作者Amir Hossein Mir Mohammad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 13:43:16