最近我开始摆弄 PowerShell,并且在运行 .jar 文件时遇到了问题。简而言之,我正在使用 plantuml,并且我只想使用“plantuml”命令来运行它。通常,运行程序是通过键入来完成的。
java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar
。这当然是相当多的,我想将其缩短为简单的plantuml
。
我当前的解决方法是以下函数:
function plantuml($UmlPath, $ImgPath) {
java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar $UmlPath $ImgPath
}
但是,我无法像这样将任何参数传递给 .jar 文件,因为 Powershell 会拦截它们并将它们解释为函数参数。当前的解决方法是将它们用引号括起来,但我发现这很难看,而且我经常忘记。
有什么方法可以简单地输入
plantuml
以便 PowerShell 将其扩展为 java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar
?我发现的唯一类似问题是this one,但似乎没有实际答案。
我没有 plantuml 或类似的测试工具,但您可以使用
$args
变量将所有参数传递给函数,因此这种方法可能有效:
function plantuml {
# Array of your default arguments to Java.exe to start plantuml
$arguments = @('-jar',
'C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar')
# All arguments passed to this function, umlpath, imgpath, and anything else
# are in the special variable $args, add those into the array as well.
$arguments += $args
Start-Process Java -ArgumentList $arguments
}