Search code examples
powershelljarplantuml

C-like macro expansion in PowerShell


Recently I've started fiddling around with PowerShell, and I bumped into an issue with running .jar files. Simply put, I'm using plantuml and I'd like to simply have a "plantuml" command to run it. Normally, running the program would be done by typing. java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar. This is of course quite a handful, and I'd like to shorten this to simply plantuml.

My current work-around is the following function: function plantuml($UmlPath, $ImgPath) { java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar $UmlPath $ImgPath } However, I cannot pass any parameters to the .jar file like this, because Powershell intercepts them and interprets them as function parameters. A current workaround for this is by wrapping them in quotation marks, but I find this ugly and I often forget.

Is there any way to simply be able to type plantuml so that PowerShell expands it to java -jar C:\Users\Name\Documents\WindowsPowerShell\Commands\plantuml.jar? The only similar question I found was this one, but it doesn't appear to have an actual answer.


Solution

  • I don't have plantuml or anything similar to test with, but you can get all the parameters passed to a function with the $args variable, so this approach might work:

    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
    }