feat: add have.bat as a wrapper

This commit is contained in:
xiazeyu_2011 2018-05-18 20:32:05 +08:00
parent daa94c5462
commit bf6dae4a24
No known key found for this signature in database
GPG Key ID: F8162BE75DCCDC2D
2 changed files with 118 additions and 11 deletions

109
bin/have.bat Normal file
View File

@ -0,0 +1,109 @@
@echo off
setlocal
if "%~1" equ "" goto :wrongSyntax
if not defined CMDER_USER_FLAGS (
exit /b
)
set "haveBatNOT=false"
goto :parseArgument
:doShift
shift
:parseArgument
set "currenTarg=%~1"
if /i "%currenTarg%" == "/?" (
goto :help
) else if /i "%currenTarg%" equ "/help" (
goto :help
) else if /i "%currenTarg%" equ "/h" (
goto :help
) else if /i "%currenTarg%" equ "NOT" (
set "haveBatNOT=true"
goto :doShift
) else (
if "%~1" equ "" goto :wrongSyntax
if "%~2" equ "" goto :wrongSyntax
set "haveBatArgName=%~1"
set "haveBatCommand=%~2"
goto :detect
)
:detect
:: to avoid erroneous deteciton like "/do" "/doNOT", both have a "/do"
:: but if it works like "/do " "/doNOT ", "/do " won't match "/doN"
set "CMDER_USER_FLAGS=%CMDER_USER_FLAGS% "
set "haveBatArgName=%haveBatArgName% "
:: echo.
:: echo %CMDER_USER_FLAGS%
:: echo %haveBatNOT%
:: echo %haveBatArgName%
:: echo %haveBatCommand%
:: echo.
echo %CMDER_USER_FLAGS% | find /i "%haveBatArgName%">nul
if "%ERRORLEVEL%" == "0" (
if "%haveBatNOT%" == "false" (
call "%haveBatCommand%"
)
) else (
if "%haveBatNOT%" == "true" (
call "%haveBatCommand%"
)
)
exit /b
:wrongSyntax
echo The syntax of the command is incorrect.
echo.
echo use /? for help
echo.
exit /b
:help
echo have.bat
echo Handles with custom arguments for cmder's init.bat
echo written by xiazeyu, inspired DRSDavidSoft
echo.
echo Usage:
echo.
echo HAVE [NOT] argName command
echo.
echo NOT Specifies that have.bat should carry out
echo the command only if the condition is false.
echo.
echo argName Specifies which argument name is to detect.
echo.
echo command 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.
echo.
echo Examples:
echo.
echo these examples are expected to be writted in /config/user-profile.cmd
echo it will use the environment varible "CMDER_USER_FLAGS"
echo.
echo Case 1:
echo.
echo The following command in user-profile.cmd would execute "notepad.exe"
echo.
echo call have "/startNotepad" "cmd /c start notepad.exe"
echo.
echo if you pass parameter to init.bat like:
echo.
echo init.bat /startNotepad
echo.
echo Case 2:
echo.
echo The following command in user-profile.cmd would execute "notepad.exe"
echo.
echo call have NOT "/dontStartNotepad" "cmd /c start notepad.exe"
echo.
echo UNLESS you pass parameter to init.bat like:
echo.
echo init.bat /dontStartNotepad
echo.
exit /b

18
vendor/init.bat vendored
View File

@ -310,26 +310,24 @@ if not exist "%initialConfig%" (
echo :: use this file to run your own startup commands echo :: use this file to run your own startup commands
echo :: use in front of the command to prevent printing the command echo :: use in front of the command to prevent printing the command
echo. echo.
echo :: the next one lines is for "have" shortcut, a custom arguments handler
echo :: don't remove it if you need it
echo set "CMDER_USER_FLAGS=%*"
echo.
echo :: uncomment this to have the ssh agent load when cmder starts echo :: uncomment this to have the ssh agent load when cmder starts
echo :: call "%%GIT_INSTALL_ROOT%%/cmd/start-ssh-agent.cmd" echo :: call "%%GIT_INSTALL_ROOT%%/cmd/start-ssh-agent.cmd"
echo. echo.
echo :: uncomment this next two lines to use pageant as the ssh authentication agent echo :: uncomment the next two lines to use pageant as the ssh authentication agent
echo :: SET SSH_AUTH_SOCK=/tmp/.ssh-pageant-auth-sock echo :: SET SSH_AUTH_SOCK=/tmp/.ssh-pageant-auth-sock
echo :: call "%%GIT_INSTALL_ROOT%%/cmd/start-ssh-pageant.cmd" echo :: call "%%GIT_INSTALL_ROOT%%/cmd/start-ssh-pageant.cmd"
echo. echo.
echo :: you can add your plugins to the cmder path like so echo :: you can add your plugins to the cmder path like so
echo :: set "PATH=%%CMDER_ROOT%%\vendor\whatever;%%PATH%%" echo :: set "PATH=%%CMDER_ROOT%%\vendor\whatever;%%PATH%%"
echo. echo.
echo :: arguments in this batch are passed from init.bat, you can parse them like so: echo :: arguments in this batch are passed from init.bat, you can quickly parse them like so:
echo :: more useage can be seen by typing "have /?"
echo. echo.
echo :: echo %* | find /i "/customOption">nul echo :: have "/customOption" "your custom command"
echo :: if "%ERRORLEVEL%" == "0" (
echo :: echo found /customOption.
echo :: do something.
echo :: ) else (
echo :: echo not found /customOption.
echo :: do something.
echo :: )
echo. echo.
echo @echo off echo @echo off
) >"%initialConfig%" ) >"%initialConfig%"