2

Supposing you have the following code snippet:

@echo off
call :SUB /?
exit /B

:SUB
>&2 echo %*
exit /B

The call command recognises the /? switch and displays its help message. How can I pass that argument over to the sub-routine, so that it is only processed there but not by the call command? Is there a clever escape sequence to accomplish that?

So far I tried some escape sequences like call :SUB ^/?, call :SUB /^?, call :SUB ^/^?, call :SUB^ /?, and also the odd syntax call :SUB(/? (which does not throw a syntax error surprisingly; similar to the widely used syntax for safe echo(), but no luck so far.

In the command prompt, the same problem arises when writing something like call echo /?.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    `call :SUB "/?"`, if you can live with the quotes or can use `%~1` in your subroutine. – Stephan Aug 12 '16 at 06:01
  • Good idea, @Stephan, but this works only if `/?` is the only argument... – aschipfl Aug 12 '16 at 10:10
  • that's why it's a comment, not an answer. `;)` – Stephan Aug 12 '16 at 10:14
  • _this works only if /? is the only argument._ It depends on your subroutine. If you want to echo all args, it will. But if you want to echo each arg separately, it will too, like: `set "ars= %*" & echo %~1 !ars: %~1=!` – sambul35 Aug 13 '16 at 07:33
  • @sambul35, I used `echo` in the sub-routine just as an example; the problem is that `call` consumes the `/?` on its own... – aschipfl Aug 13 '16 at 13:14

2 Answers2

1

call is very greedy, even this will display the help screen

call :sub ^/;;===,,,,,?

But it can be done with a helper variable.

@echo off
set "var=/?"
call :sub %%var%%
exit /B

:sub
echo(Parameter: %*
exit /b

Additional info:
When the /? is used as a label there are other strange effects, then the /? can be used for anonymous functions.

@echo off
set "anonymous=/?"
call :%%anonymous%% argument >nul
if "%0"==":/?" (
  echo func arg=%1 > con
  exit /b
)
echo End
Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225
0

How can I pass that argument over to the sub-routine, so that it is only processed there but not by the call command?

See the example below:

@echo off
setlocal enabledelayedexpansion
call :SUB "/?" Hello
exit /b

:SUB
set "ars= %*"
echo %~1 & echo/ & echo !ars: %1=! 
exit /b

:: Output
Displays messages, or turns command-echoing on or off.

  ECHO [ON | OFF]
  ECHO [message]

Type ECHO without parameters to display the current echo setting.

 Hello
Pang
  • 9,564
  • 146
  • 81
  • 122
sambul35
  • 1,058
  • 14
  • 22