cmder/vendor/bin/cexec.cmd

136 lines
3.5 KiB
Batchfile
Raw Normal View History

2018-05-18 20:32:05 +08:00
@echo off
setlocal
if "%~1" equ "" goto :wrongSyntax
if not defined CMDER_USER_FLAGS (
:: in case nothing was passed to %CMDER_USER_FLAGS%
2018-05-18 21:02:36 +08:00
set "CMDER_USER_FLAGS= "
2018-05-18 20:32:05 +08:00
)
set "feNot=false"
2018-05-18 20:32:05 +08:00
goto :parseArgument
:doShift
shift
:parseArgument
set "currenArgu=%~1"
if /i "%currenArgu%" equ "/setPath" (
:: set %flag_exists% shortcut
endlocal
2018-09-16 02:55:54 +08:00
set "ccall=call %~dp0cexec.cmd"
set "cexec=%~dp0cexec.cmd"
) else if /i "%currenArgu%" == "/?" (
2018-09-16 04:10:35 +08:00
goto :help
) else if /i "%currenArgu%" equ "/help" (
2018-05-18 20:32:05 +08:00
goto :help
) else if /i "%currenArgu%" equ "/h" (
2018-05-18 20:32:05 +08:00
goto :help
) else if /i "%currenArgu%" equ "NOT" (
set "feNot=true"
2018-05-18 20:32:05 +08:00
goto :doShift
) else (
if "%~1" equ "" goto :wrongSyntax
if "%~2" equ "" goto :wrongSyntax
set "feFlagName=%~1"
set "feCommand=%~2"
if not "%~3" equ "" (
set "feParam=%~3"
)
2018-05-18 20:32:05 +08:00
goto :detect
)
:detect
:: to avoid erroneous deteciton like "/do" "/doNOT", which both have a "/do"
:: we added a space after the flag name, like "/do ", which won't match "/doN"
set "feFlagName=%feFlagName% "
2018-05-18 20:32:05 +08:00
:: echo.
:: echo %CMDER_USER_FLAGS%
:: echo %feNOT%
:: echo %feFlagName%
:: echo %feCommand%
:: echo %feParam%
2018-05-18 20:32:05 +08:00
:: echo.
echo %CMDER_USER_FLAGS% | find /i "%feFlagName%">nul
2018-05-18 20:32:05 +08:00
if "%ERRORLEVEL%" == "0" (
if "%feNOT%" == "false" (
call %feCommand% %feParam%
2018-09-16 02:55:54 +08:00
exit /b 0
2018-05-18 20:32:05 +08:00
)
) else (
if "%feNOT%" == "true" (
call %feCommand% %feParam%
2018-09-16 02:55:54 +08:00
exit /b 0
2018-05-18 20:32:05 +08:00
)
)
endlocal
2018-09-16 02:55:54 +08:00
exit /b 1
2018-05-18 20:32:05 +08:00
:wrongSyntax
echo The syntax of the command is incorrect.
echo.
echo use /? for help
echo.
endlocal
2018-05-18 20:32:05 +08:00
exit /b
:help
echo.
2018-09-16 02:55:54 +08:00
echo CExec - Conditional Exec
echo.
echo Handles with custom arguments for cmder's init.bat.
echo written by xiazeyu, inspired DRSDavidSoft.
2018-05-18 20:32:05 +08:00
echo.
echo Usage:
echo.
2018-09-16 02:55:54 +08:00
echo cexec [NOT] flagName command/program [parameters]
echo.
2018-09-16 02:55:54 +08:00
echo /setPath Generate a global varible %%cexec%% for
echo quicker use. Following arguments will be ignored.
echo.
2018-09-16 02:55:54 +08:00
echo NOT Specifies that cexec should carry out
echo the command only if the flag is missing.
2018-05-18 20:32:05 +08:00
echo.
2018-09-16 02:55:54 +08:00
echo /[flagName] Specifies which flag name is to detect. It's recommand
echo to use a pair of double quotation marks to wrap
echo your flag name to avoid exceed expectation.
2018-05-18 20:32:05 +08:00
echo.
echo command/program Specifies the command to carry out if the
echo argument name is detected. It's recommand to
echo use a pair of double quotation marks to
echo wrap your command to avoid exceed expectation.
2018-05-18 20:32:05 +08:00
echo.
echo parameters These are the parameters passed to the command/program.
echo It's recommand to use a pair of double quotation marks
echo to wrap your flag name to avoid exceed expectation.
2018-05-18 20:32:05 +08:00
echo.
echo Examples:
echo.
2018-09-16 02:55:54 +08:00
echo These examples are expected to be written in %cmder_root%/config/user-profile.cmd
echo CExec evaluates the environment varible "CMDER_USER_FLAGS" and conditionally
echo caries out actions based on flags that are passed.
2018-05-18 20:32:05 +08:00
echo.
echo Case 1:
echo.
echo The following command in user-profile.cmd would execute "notepad.exe"
echo.
2018-09-16 02:55:54 +08:00
echo call cexec "/startNotepad" "start" "notepad.exe"
2018-05-18 20:32:05 +08:00
echo.
2018-09-16 02:55:54 +08:00
echo If you pass parameter to init.bat like:
2018-05-18 20:32:05 +08:00
echo.
echo init.bat /startNotepad
echo.
echo Case 2:
echo.
echo The following command in user-profile.cmd would execute "notepad.exe"
echo.
2018-09-16 02:55:54 +08:00
echo call cexec NOT "/dontStartNotepad" "start" "notepad.exe"
2018-05-18 20:32:05 +08:00
echo.
echo UNLESS you pass parameter to init.bat like:
echo.
echo init.bat /dontStartNotepad
echo.
endlocal
2018-05-18 20:32:05 +08:00
exit /b