kitensei
kitensei

Reputation: 2530

Adding /? switch in batch?

Do someone knows how to add the action to be triggered when calling my batch file with the /? argument ? I've always used the -h to display usages, but for once I need my -h arg for something else.

EDIT : Actually I've tried by parsing attributes like this

for %%i in (%*) do ....

But the /? argument was skipped, I'll try yours solutions to see if it's different.

Btw, why when you parse %%i the /? args is skipped ?

Upvotes: 6

Views: 2636

Answers (4)

TwoXTwentyOne
TwoXTwentyOne

Reputation: 15

Here's an actual use case for those visuals out there!

@ECHO OFF

:parameterLoop
IF /I "%1"=="/install" GOTO install
IF /I "%1"=="/uninstall" GOTO uninstall
IF /I "%1"=="/repair" GOTO repair
IF /I "%1"=="" (
    ECHO.
    ECHO Please use the following commands to proceed:
    ECHO.
    ECHO Use /INSTALL to setup the software
    ECHO.
    ECHO Use /UNINSTALL to remove the software
    ECHO.
    ECHO Use /REPAIR to repair the software
    ECHO.
    GOTO end
)

:install
TASKKILL /F /IM vpnui* /T
TASKKILL /F /IM DartOffline* /T
MSIEXEC /I "anyconnect-win-XXX-core-vpn-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /I "anyconnect-win-XXX-dart-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /I "anyconnect-win-XXX-posture-predeploy-k9.msi" /QN /NORESTART
COPY /Y "production.xml" "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client\Profile\production.xml"
GOTO end

:uninstall
TASKKILL /F /IM vpnui* /T
TASKKILL /F /IM DartOffline* /T
DEL /F /S /Q "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client\Profile\production.xml"
MSIEXEC /X "anyconnect-win-XXX-core-vpn-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /X "anyconnect-win-XXX-dart-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /X "anyconnect-win-XXX-posture-predeploy-k9.msi" /QN /NORESTART
RMDIR /S /Q "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client"
GOTO end

:repair
TASKKILL /F /IM vpnui* /T
TASKKILL /F /IM DartOffline* /T
DEL /F /S /Q "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client\Profile\production.xml"
MSIEXEC /X "anyconnect-win-XXX-core-vpn-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /X "anyconnect-win-XXX-dart-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /X "anyconnect-win-XXX-posture-predeploy-k9.msi" /QN /NORESTART
RMDIR /S /Q "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client"
MSIEXEC /I "anyconnect-win-XXX-core-vpn-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /I "anyconnect-win-XXX-dart-predeploy-k9.msi" /QN /NORESTART
MSIEXEC /I "anyconnect-win-XXX-posture-predeploy-k9.msi" /QN /NORESTART
COPY /Y "production.xml" "%ProgramData%\Cisco\Cisco AnyConnect Secure Mobility Client\Profile\production.xml"
GOTO end

:end 
EXIT /B

REM Src https://stackoverflow.com/questions/8179425/adding-switch-in-batch

Essentially with that command I can install my software using the /install or /uninstall or /repair switches... way useful in intune and making custom MSI's with AdvancedInstaller!

Also if you type nothing a little help menu appears instructing the user which commands are available :)

Upvotes: 0

jeb
jeb

Reputation: 82390

The /? seems to be simply skipped by the for %%i in (%*) but it's the wildcard functionality of the for-loop, it tries to find a file that matches /? which will fail.

You can not use ? or * in a "normal" for-loop, without modifying the result.

You could use the SHIFT command to access all your parameters.

:parameterLoop
if "%~1"=="/?" call :help
if "%~1"=="-h" call :help
if "%~1"=="-o" call :other
shift
if not "%~1"=="" goto :parameterLoop

If you also want to display the selected option, you got a problem with the echo command, as this will normally show the help instead of /?.

You can avoid this by using echo(%1 instead of echo %1.

Upvotes: 6

rekire
rekire

Reputation: 47965

You could try this:

@echo off
if "%1"=="/?" goto print_help
goto normal_start

:print_help
echo Here is your help
goto end

:normal_start
echo I'm working

:end

Upvotes: 1

VinayC
VinayC

Reputation: 49225

You check your command line arguments (%1, %2 etc) against the /? string and if true then print help using ECHO command. For example,

@ECHO OFF
IF "%1"=="/?" (
    ECHO "Help Line 1"
    ECHO "Help Line 2"
) ELSE (
    ECHO "Do Your Action"
)

Upvotes: 4

Related Questions