如何从 PowerShell 函数返回一个且仅一个值? [英] How to return one and only one value from a PowerShell function?

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

问题描述

我从这个堆栈溢出问题中了解到,PowerShell 返回语义是不同的,比方说,从 C# 的返回语义.引自上述问题:

I've learned from this Stack Overflow question, that PowerShell return semantics are different, let's say, from C#'s return semantics. Quote from the aforementioned question:

PowerShell 具有非常古怪的返回语义 - 至少从更传统的编程角度来看是这样.有两个主要想法可以让您了解:捕获并返回所有输出.return 关键字实际上只是表示一个逻辑退出点.

PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around: All output is captured, and returned. The return keyword really just indicates a logical exit point.

让我们看看这个例子:

function Calculate
{
   echo "Calculate"
   return 11
}

$result = Calculate

如果你回显 $result 你会意识到有些不对劲.你会期望这个:

If you echo $result you will realise that something is not right. You'd expect this:

11

但现实,你实际看到的,是不同的:

But the reality, what you actually see, is different:

Calculate
11

因此,您实际上会返回一个数组,而不是仅返回预期的返回值.

So instead of getting back only the intended return value, you actually get back an array.

你可以去掉 echo 语句而不污染返回值,但是如果你从你的函数中调用另一个函数,它会回显某些东西,那么你又会遇到麻烦.

You could get rid of the echo statements and not polluting the return value, but if you call another function from your function, which echoes something, then you're in trouble again.

如何编写只返回一件事的 PowerShell 函数?如果内置的 PowerShell 函数确实只返回一个值,为什么我不能这样做?

How can I write a PowerShell function that only returns one thing? If the built-in PowerShell functions do return only one value, why can't I do that?

我现在正在使用的解决方法,我很想摆脱:

The workaround that I'm using now and I'd love to get rid of:

function Calculate
{
   # Every function that returns has to echo something
   echo ""
   return 11
}

# The return values is the last value from the returning array
$result = (Calculate)[-1]

推荐答案

已经给出的几个答案没有完全回答您的问题,因为它们没有考虑到您可能会调用的其他 cmdlet--当您指出时输出——这可能会污染"你的输出.@Laezylion 的回答基本上是正确的,但我相信需要进一步阐述.

Several answers already given do not fully answer your question because they do not account for other cmdlets you might call--as you point out--that might "pollute" your output. The answer from @Laezylion is essentially correct but I believe bears further elaboration.

很明显,您很清楚您当前的解决方法——强制每个函数返回一个数组,然后获取最后一个元素——不是一个非常可靠的解决方案.(事实上​​,我认为将其称为混杂是安全的.:-) 正确的方法 - 以及对您问题的直接回答 - 很清楚......尽管乍一看这听起来像是同义反复:

It is clear that you appreciate that your current workaround--forcing every function to return an array then taking the last element--is not a very robust solution. (In fact, I think it is safe to call that a kludge. :-) The correct approach--and a direct answer to your question--is clear... though at first glance it sounds like a tautology:

Question: How do you ensure a function returns only one thing?
Answer: By ensuring that it returns only one thing.

让我解释一下.PowerShell 中有两种函数/cmdlet:(A) 返回数据的函数/cmdlet 和(B) 不返回数据但可能会报告进度或诊断消息的函数/cmdlet.正如您所见,当您尝试将两者混合时,就会出现问题.而这很容易在不经意间发生.因此,作为函数作者,您有责任了解函数中的每个函数/cmdlet 调用:具体来说,它是否返回任何内容,是真正的返回值还是仅仅是诊断输出?您期望来自 A 类函数的输出,但您需要警惕任何 B 类函数.对于任何确实返回某些内容的人,您必须将其分配给一个变量,将其输入管道,或者……让它消失.(有几种方法可以让它消失:通过管道将它传递给 Out-Null,将它转换为 void,将它重定向到 $null,或者将它分配给 $null.参见 Jason Archer 的 Stack Overflow 帖子 评估了每种风格的性能,建议您避免使用 Out-Null.)

Let me elucidate. There are really two kinds of functions/cmdlets in PowerShell: (A) those that return data and (B) those that do not return data but may instead report progress or diagnostic messages. Problems arise, as you have seen, when you try to mix the two. And this may easily happen inadvertently. Thus, it is your responsibility, as the function author, to understand each function/cmdlet call within your function: specifically, does it return anything, be it a true return value or just diagnostic output? You are expecting output from type A functions, but you need to be wary of any type B functions. For any one that does return something you must either assign it to a variable, feed it into a pipeline, or... make it go away. (There are several ways to make it go away: pipe it to Out-Null, cast it to void, redirect it to $null, or assign it to $null. See Jason Archer’s Stack Overflow post that evaluates the performance of each of these flavors, suggesting you shy away from Out-Null.)

是的,这种方法需要更多的努力,但您可以通过这样做获得更强大的解决方案.有关这个问题的更多讨论,这些 A/B 函数等,请参阅 大量 PowerShell 陷阱 最近发布在 Simple-Talk.com 上.

Yes, this approach takes more effort, but you get a more robust solution by doing so. For a bit more discussion on this very issue, these A/B functions, etc. see Question 1 on A Plethora of PowerShell Pitfalls recently published on Simple-Talk.com.

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

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