你必须创建一个函数。幸运的是,使用标准参数创建“别名样式”函数并不困难:
Function launch_foo { java -jar foo --param1 --param2 --param-n $args }
You can now invoke the function either as launch_foo
or as launch_foo --param3
. Note that if you don't pass in any additional arguments, $args
will be $null
and will have no effect[1] on the default arguments specified in the function.
现在,Powershell 中存在别名的概念,但别名是 cmdlet 或命令的备用名称,您不能向别名定义添加参数。您可以使用别名作为备用可执行文件或 cmdlet 名称。别名的一些示例:
Set-Alias v vim
Set-Alias list Get-ChildItem
Set-Alias 2json ConvertTo-Json
v
会跑vim
, list
会跑Get-ChildItem
, and 2json
会跑ConvertTo-Json
.
[1] Note that this is only true for external commands. If you attempt to alias a cmdlet or function with parameters in this way, a $null
argument will be evaluated as a $null
value, which $args
would be $null
if not provided. Depending on the cmdlet this could throw an error, have no effect, or have an undesired effect. Depending on the cmdlet, you may also need to splat @args
to appropriately pass on the arguments.