PowerShell 缩进 [英] powershell indentation

查看:65
本文介绍了PowerShell 缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个部署应用程序的大型脚本.此脚本基于多个嵌套函数调用.

有没有办法根据深度识别"输出?

例如,我有:

函数 myFn(){写主机开始 myfn"myFnNested()写主机结束 myfn"}函数 myFnNested(){写主机开始 myFnNested"写主机结束 myFnNested"}写主机开始我的脚本"写主机我的脚本结束"

脚本的输出将是:

<块引用>

myscript 的开始myfn 的开始myfnNested 的开始结束 myFnNested我的Fn结束我的脚本结束

我想要实现的是这个输出:

<块引用>

myscript 的开始myfn 的开始myfnNested 的开始结束 myFnNested我的Fn结束我的脚本结束

因为我不想几乎不编码空格的数量(因为我不知道复杂脚本中的深度级别).我怎样才能简单地达到我的目标?

也许是这样?

函数 myFn(){缩进()写主机开始 myfn"myFnNested()写主机结束 myfn"取消缩进()}函数 myFnNested(){缩进()写主机开始 myFnNested"写主机结束 myFnNested"取消缩进()}写主机开始我的脚本"写主机我的脚本结束"

解决方案

查看这个脚本 http://poshcode.org/scripts/3386.html

如果你加载了 Write-Verbose 包装器,你可以设置 $WriteHostAutoIndent = $true 然后调用 Write-Host,它会根据堆栈深度缩进.因此,鉴于您最初定义的这些函数:

函数 myFn(){写主机开始 myfn"myFnNested写主机结束 myfn"}函数 myFnNested(){写主机开始 myFnNested"写主机结束 myFnNested"}

无需更改,您只需点源脚本文件,其中包含 Write-Host 包装函数:

C:\PS>.C:\Users\Jaykul\Documents\WindowsPowerShell\PoshCode\3386.ps1

然后在调用函数之前只设置首选项变量:

C:\PS>$WriteHostAutoIndent = $trueC:\PS>我的Fnmyfn 的开始myFnNested 的开始结束 myFnNestedmyfn 结束

漂亮的缩进输出,就像魔术一样;-)

I'm writing a large script that deploys an application. This script is based on several nested function calls.

Is there any way to "ident" the output based on the depth?

For example, I have:

function myFn()
{
    Write-Host "Start of myfn"
    myFnNested()
    Write-Host "End of myfn"
}
function myFnNested()
{
    Write-Host "Start of myFnNested"
    Write-Host "End of myFnNested"
}

Write-Host "Start of myscript"
Write-Host "End of myscript"

The output of the script will be :

Start of myscript
Start of myfn
Start of myfnNested
End of myFnNested
End of myFn
End of myscript

What I want to achieve is this output :

Start of myscript
  Start of myfn
    Start of myfnNested
    End of myFnNested
  End of myFn
End of myscript

As I don't want to hardly code the number of spaces (since I does not know the depth level in complex script). How can I simply reach my goal ?

Maybe something like this?

function myFn()
{
    Indent()
    Write-Host "Start of myfn"
    myFnNested()
    Write-Host "End of myfn"
    UnIndent()
}
function myFnNested()
{
    Indent()
    Write-Host "Start of myFnNested"
    Write-Host "End of myFnNested"
    UnIndent()
}

Write-Host "Start of myscript"
Write-Host "End of myscript"

解决方案

Check out this script http://poshcode.org/scripts/3386.html

If you load up that Write-Verbose wrapper, you can set $WriteHostAutoIndent = $true and then just call Write-Host and it will be indented based on stack depth. So given these functions as you defined them originally:

function myFn()
{
   Write-Host "Start of myfn"
   myFnNested
   Write-Host "End of myfn"
}
function myFnNested()
{
   Write-Host "Start of myFnNested"
   Write-Host "End of myFnNested"
}

With no changes, you can just dot-source a script file with that Write-Host wrapper function in it:

C:\PS> . C:\Users\Jaykul\Documents\WindowsPowerShell\PoshCode\3386.ps1

And then merely set the preference variable before you call your function:

C:\PS> $WriteHostAutoIndent = $true
C:\PS> myFn
  Start of myfn
    Start of myFnNested
    End of myFnNested
  End of myfn

Beautiful indented output, like magic ;-)

这篇关于PowerShell 缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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