且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

相当于 PowerShell 中的 Bash 别名

更新时间:2021-09-12 05:09:39

PowerShell 别名不允许参数.
它们只能引用一个命令名称,它可以是一个 cmdlet 或函数的名称,也可以是一个脚本或可执行文件的名称/路径.

PowerShell aliases do not allow for arguments.
They can only refer to a command name, which can be the name of a cmdlet or a function, or the name / path of a script or executable.

为了得到你想要的东西,你需要定义一个函数:

To get what you are after, you need to define a function:

function django-admin-jy {
    jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args
}

这使用了自 PowerShell 2.0 以来可用的功能,称为参数 splatting:您可以将 @ 应用于引用数组或哈希表的变量名称.
在这种情况下,我们将其应用于 自动变量 名为 args,其中包含所有参数(未绑定到显式声明的参数 - 请参阅 about_Functions).

This uses a feature available since PowerShell 2.0 called argument splatting: you can apply @ to a variable name that references either an array or a hashtable.
In this case, we apply it to the automatic variable named args, which contains all arguments (that weren't bound to explicitly declared parameters - see about_Functions).

如果你想要一种真正通用的方法来创建带参数的别名函数,试试这个:

If you want a truly generic way to create aliases functions that take parameters, try this:

function New-BashStyleAlias([string]$name, [string]$command)
{
    $sb = [scriptblock]::Create($command)
    New-Item "Function:global:$name" -Value $sb | Out-Null
}

New-BashStyleAlias django-admin-jy 'jython.exe /path/to/jython-dev/dist/bin/django-admin.py @args'