最近,我开始摆弄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,但似乎没有一个真正的答案。
发布于 2016-12-02 19:20:36
我没有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
}https://stackoverflow.com/questions/40938765
复制相似问题