【问题标题】:Cmdlet: Have user enter complex data-types on powershell automatic promptingCmdlet:让用户在 powershell 自动提示上输入复杂的数据类型
【发布时间】:2019-01-21 12:46:15
【问题描述】:

我想让我的用户为我的自定义数据类型“MyExampleType”输入数据。

伪代码:

 [Cmdlet("Set", "MyExampleData")]
    public class SetMyExampleData
    {
        [Parameter(Position = 0, Mandatory = true)]
        public string Name{ get; set; }

        [Parameter(Position = 1, Mandatory = true)]
        public MyExampleType ExampleData { get; set; }

据我了解,在自动提示时,powershell 希望用户在询问“ExampleData”时传递 MyExampleType 的实例。

是否有可能让 powershell 自己请求任何值?所以,如果 'MyExampleType' 看起来像这样:

public class MyExampleType
{
   public string Key { get; set; }
   public string Value { get; set; }

那个powershell然后会要求'Key',然后是'Value'?

我通常很难创建命令行开关来提供大量软件的基本配置(就向其中添加配置数据集而言),并且还不知道让用户输入更多数据的方法复杂的不仅仅是单个值。 Mabye,我一般在这里走错路了……

【问题讨论】:

    标签: c# powershell user-interface types


    【解决方案1】:

    您可以编写一个将所有字段作为参数的中间函数,它将这些参数分配给您的自定义类型对象属性,然后在没有任何参数的情况下调用此函数。在这种情况下,powershell 将要求输入每个参数的值。示例(假设 $obj 在其他地方定义):

    function fillMyType
    {
       param(
       [Parameter(Mandatory=$true)]$Key,
       [Parameter(Mandatory=$true)]$Value
       )
    
       $obj.Key=$Key
       $obj.Value=$Value
    }
    
    fillMyType
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 2011-12-22
      相关资源
      最近更新 更多