0

I'm trying to make a batch script that behave almost like a Linux command in terms of arguments. Here is the idea of the script.

When I run the script with the scenarios described in the code it seems to work fine. The problem that I have is coming when I tried to test the program with wrong parameters. For the 1st parameter being either -manual or -automat and the 2nd parameter being wrong the behave is normal, the program prints "Invalid Argument".

The problem that I encounter is when the 1st argument is not -manual or -automat. In this case I get the error: goto was unexpected at this time.

Does any1 have any idea why this is happening and how can I solve the problem?

@echo off

IF %1!==! goto Result0

IF %1==-manual IF %2!==! goto Result1_manual
IF %1==-automat IF %2!==! goto Result1_auto

IF %1==-manual IF %2==1 goto Result2_manual
IF %1==-manual IF %2==2 goto Result3_manual
IF %1==-automat IF %2==1 goto Result2_auto
IF %1==-automat IF %2==2 goto Result3_auto

:done
echo "Invalid argument"
pause
cmd /k

:Result0
echo "Result0"
pause
cmd /k

:Result1_manual
echo "Result1_manual"
pause
cmd /k

:Result2_manual
echo "Result2_manual"
pause
cmd /k

:Result3_manual
echo "Result3_manual"
pause
cmd /k

:Result1_auto
echo "Result1_auto"
pause
cmd /k

:Result2_auto
echo "Result2_auto"
pause
cmd /k

:Result3_auto
echo "Result3_auto"
pause
cmd /k
  • 2
    I suggest to read my answer on [What is the difference between “…” and x“…” in an IF condition in a Windows batch file?](https://stackoverflow.com/a/52418905/3074564) Then you should know what are you doing wrong. The first __IF__ condition should be `IF "%~1" == "" goto Result0`, the second one should be `IF /I "%~1" == "-manual" IF "%~2" == "" goto Result1_manual` and so on. See also [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564) with details about string comparison using command __IF__. You missed the spaces around `==`. – Mofi Sep 22 '20 at 05:51
  • 1
    Further I recommend to open a [command prompt](https://www.howtogeek.com/235101/), run `cmd /?` and `call /?` and `if /?` and `goto /?` and read each output help carefully and completely from top of first to bottom of last page to get more knowledge about the [Windows Commands](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) used in your batch file described even better at [SS64.com - A-Z index of Windows CMD commands](https://ss64.com/nt/). – Mofi Sep 22 '20 at 05:55
  • Parsing and validating batch arguments can quickly become a pain. See [my answer](https://stackoverflow.com/a/8162578/1012053) to [Windows Bat file optional argument parsing](https://stackoverflow.com/q/3973824/1012053) for an elegant solution. For example, my [JREPL.BAT](https://www.dostips.com/forum/viewtopic.php?t=6044) utility uses a variant of that technique, and it was easy to support 49 different named options!. – dbenham Sep 22 '20 at 16:45

1 Answers1

0
If "%1"=="-manual" goto Result1_manual

If parameters might be missing enclose with another character. If %1 in blank and you don't

If ==-manual goto Result1_manual

an illegal syntax, and if you do

 If ""=="-manual" goto Result1_manual

a legal syntax that resolves to false.

 IF %2!==!

will only be true if %2 is blank. If that is your intention don't use the quote character. It sometimes has special meaning.

  • 1
    You should use `"%~1"` etc. rather than `"%1"`. And `!` is not the quote character (`"` is), it is the exclamation mark… – aschipfl Sep 22 '20 at 07:32