如何从 Powershell 函数返回多个项目 [英] How to return several items from a Powershell function

查看:28
本文介绍了如何从 Powershell 函数返回多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个脚本中,我有几个函数需要分解SVN外部定义.这些可以是 ;<path>, -r;<网址><path>, 或 -r <rev><网址><path>,而我想出的提取数据的机制不仅仅是两行,所以我想把它放到一个单独的函数中.

In a script I have several functions that need to break down an SVN external definition. These can either be <url> <path>, -r<rev> <url> <path>, or -r <rev> <url> <path>, and the mechanics I came up with to extract the data is more than just two lines, so I want to put this into a separate function.

但是当我这样做时,我最终得到了三个包含函数本地相关数据的变量,我认为无法将它们全部三个都提供给调用者.

But when I do so, I end up with three variables containing the relevant data which are local to the function, and I see no way to get all three of them them to the caller.

在适当的编程语言中,我会返回一个包含所有三个值的复合类型,但我不知道如何在 Powershell 中执行此操作.当然,我可以将其拆分为三个单独的功能,但随后我又违反了DRY 规则.

In a proper programming language I would return a compound type containing all three values, but I don't see how to do that in Powershell. Of course, I could split this into three separate functions, but then I am back at violating the DRY rule.

那么我可以在 Powershell 中做什么?

So what can I do here in Powershell?

推荐答案

你可以返回一个 [string] 数组,然后让调用者拆分它或者返回一个 自定义对象 并且始终由调用者进行拆分.

You can return an array of [string] and then let the caller split it or return a custom object and always the caller do the split.

这篇关于如何从 Powershell 函数返回多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆