0

I want to pass optional parameters to the script like that:

mybatfile.bat firstParameter -r 000 -m "Some message here"

I've got solution from different question, but I have problem with -m "Some message here"

SET firstParameter=%1
SET message=""

SET roomNumber=""

SHIFT

:loop
IF NOT "%1"=="" (
    IF "%1"=="-r" (
        SET roomNumber=%2        
    )

    IF "%1"=="-m" (         
        set message=%~2
    )

    SHIFT
    GOTO :loop
)

%~2 only gets first part of the word because it see every word as another parameter. I also find out I could use small hack inside if, like that

    for %%x in (%*) do (
        echo "%%~x"
    )

And here take first "%%~x" after "-m" appear. But how to do that using script?

mike00
  • 438
  • 1
  • 6
  • 17
  • You are talking about DOS, right? – FDavidov Nov 01 '16 at 15:38
  • 1
    @FDavidov - No, he is talking about a Windows batch script, which is ***not*** DOS. – dbenham Nov 01 '16 at 17:38
  • @dbenham, you got this one? If so, I am not going to fix it. – Squashman Nov 01 '16 at 17:44
  • Safe parsing of arguments can be a pain. You have no code to even validate that the next argument after one of your options is actually data and not another option. – Squashman Nov 01 '16 at 18:06
  • 1
    @Squashman - Yes, it can be a pain, but there is no failsafe way to prove whether the next argument is a value or another option. Depending on the application, a valid option value may match an option name. But there are lots of other niggling issues to deal with, which is why I wrote an easy to configure [optional argument parser](http://stackoverflow.com/a/8162578/1012053) – dbenham Nov 01 '16 at 18:34

1 Answers1

3

You are misdiagnosing your problem. The quotes around the message parameter will preserve the space literals.

But your test for no parameter is wrong, and your are missing some SHIFT commands.

Below is a minimal correction of your code.

@echo off
setlocal
set firstParameter=%1
set "message="
set "roomNumber="

:loop
shift
if not "%~1"=="" (
  if "%~1"=="-r" (
    set roomNumber=%2
    shift
  )
  if "%~1"=="-m" (
    set message=%2
    shift
  )
  goto :loop
)

echo firstParameter = %firstparameter%
echo roomNumber     = %roomNumber%
echo message        = %message%

Sample usage:

C:>mybatfile firstParameter -r 000 -m "Some message here"
firstParameter = firstParameter
roomNumber     = 000
message        = "Some message here"

You really should consider my argument parsing template at https://stackoverflow.com/a/8162578/1012053. Most of that code remains constant, no matter how many optional arguments you define. It supports default values for unused parameters, and is very robust.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Tried use that but got empty room number when used like:... -r 000 -m "some test". But when switched parameters order -m "some test" -r 000, then room number appear but text message disappear :) – mike00 Nov 01 '16 at 21:30
  • @mike00 - The code I supplied works - The "Sample usage" shows the exact output that I get. It sounds like you either forgot to call with the first parameter before -r, or else you modified the code incorrectly. Make sure that your logic has the correct number and placement of SHIFT, and that your call has the correct number of required arguments at the front. – dbenham Nov 01 '16 at 21:59
  • yea :) forgot about first mandatory param. Thank you for solution! – mike00 Nov 01 '16 at 22:27