Given an optional port argument, where the port number can vary in length, how do I obtain the port number from the batch script's command line arguments?
Example:
foo.bat --foo bar --port 80 --bar foo
Should output:
80
I got this far, trying to use substring.
set CMD_LINE_ARGS=%*
@rem Remove all chars and port arg
set PORT_ARG_REMOVED=%CMD_LINE_ARGS:*-port =%
@rem Obviously, this is where I fail to remove trailing chars
set PORT_NUM=%PORT_ARG_REMOVED: *=%
echo %PORT_NUM%
Edit
The answer I chose is because it fits with my very particular use case, where all arguments were being passed through to the command that I was wrapping. And, I only needed the value for a particular optional argument. No looping required.
There are some very nice answers here for dealing with optional argument parsing in general. So, feel free to upvote everyone.