是否可以调用嵌套在另一个函数 (PowerShell) 中的函数?

Is it possible to call an function nested in another function (PowerShell)?

我很习惯Python函数可以放在类中单独调用。

但是,现在我必须在 PowerShell 中编写一些代码,如果在这里可以实现类似的功能,我找不到方法。

我正在尝试做的一个例子:

function a {
    Write-Host "a"

    function a_1() { Write-Host "a_1" }

    function a_2() { Write-Host "a_2" }
    
}

a      # Works
a.a_1  # Doesn't works
a_2    # Doesn't works

PowerShell(5 及更高版本)确实支持 classes,(参见 about_Classes)并且 class 方法可以是静态的。

例如:

class a {
    a() {
        Write-Host "a"
    }
    static [void]a_1()
    {
        Write-Host "a_1"
    }
    static [void]a_2()
    {
        Write-Host "a_2"
    }
}

[a]$a = [a]::new()
[a]::a_1()
[a]::a_2()

输出:

a
a_1
a_2

Only if the inner function have a scope modifier, i.e.: function script:a_1() { ... } or function global:a_2() { ... } – Santiago Squarzon

谢谢!这似乎与我正在搜索的内容非常接近,结合函数的创造性命名,这使得我有可能获得我习惯的东西。

我的解决方案:

function a {
    Write-Host "a"

    function script:a.a_1() { Write-Host "a_1" }

    function script:a.a_2() { Write-Host "a_2" }
    
}

a       # Call this so the functions get loaded
a.a_1   # Creative naming so it acts like I need it
a.a_2