mirror of
https://github.com/cmderdev/cmder.git
synced 2025-06-15 14:17:50 +08:00
refactor: reduce global varible useage, fixed quote issue, added parameters support
This commit is contained in:
73
vendor/lib/flag_exists.cmd
vendored
73
vendor/lib/flag_exists.cmd
vendored
@ -1,59 +1,67 @@
|
||||
@echo off
|
||||
set "flag_exists=%~dp0flag_exists"
|
||||
setlocal
|
||||
|
||||
if "%~1" equ "" goto :wrongSyntax
|
||||
|
||||
if not defined CMDER_USER_FLAGS (
|
||||
:: in case nothing was passed to %CMDER_USER_FLAGS%
|
||||
set "CMDER_USER_FLAGS= "
|
||||
)
|
||||
|
||||
set "feNOT=false"
|
||||
set "feNot=false"
|
||||
goto :parseArgument
|
||||
|
||||
:doShift
|
||||
shift
|
||||
|
||||
:parseArgument
|
||||
set "currenTarg=%~1"
|
||||
if /i "%currenTarg%" == "/?" (
|
||||
set "currenArgu=%~1"
|
||||
if /i "%currenArgu%" equ "/setPath" (
|
||||
:: set %flag_exists% shortcut
|
||||
endlocal
|
||||
set "flag_exists=%~dp0flag_exists"
|
||||
) else if /i "%currenArgu%" == "/?" (
|
||||
goto :help
|
||||
) else if /i "%currenTarg%" equ "/help" (
|
||||
) else if /i "%currenArgu%" equ "/help" (
|
||||
goto :help
|
||||
) else if /i "%currenTarg%" equ "/h" (
|
||||
) else if /i "%currenArgu%" equ "/h" (
|
||||
goto :help
|
||||
) else if /i "%currenTarg%" equ "NOT" (
|
||||
set "feNOT=true"
|
||||
) else if /i "%currenArgu%" equ "NOT" (
|
||||
set "feNot=true"
|
||||
goto :doShift
|
||||
) else (
|
||||
if "%~1" equ "" goto :wrongSyntax
|
||||
if "%~2" equ "" goto :wrongSyntax
|
||||
set "feArgName=%~1"
|
||||
set "feFlagName=%~1"
|
||||
set "feCommand=%~2"
|
||||
if not "%~3" equ "" (
|
||||
set "feParam=%~3"
|
||||
)
|
||||
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 "feArgName=%feArgName% "
|
||||
:: 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% "
|
||||
:: echo.
|
||||
:: echo %CMDER_USER_FLAGS%
|
||||
:: echo %feNOT%
|
||||
:: echo %feArgName%
|
||||
:: echo %feFlagName%
|
||||
:: echo %feCommand%
|
||||
:: echo %feParam%
|
||||
:: echo.
|
||||
echo %CMDER_USER_FLAGS% | find /i "%feArgName%">nul
|
||||
echo %CMDER_USER_FLAGS% | find /i "%feFlagName%">nul
|
||||
if "%ERRORLEVEL%" == "0" (
|
||||
if "%feNOT%" == "false" (
|
||||
call "%feCommand%"
|
||||
call %feCommand% %feParam%
|
||||
)
|
||||
) else (
|
||||
if "%feNOT%" == "true" (
|
||||
call "%feCommand%"
|
||||
call %feCommand% %feParam%
|
||||
)
|
||||
)
|
||||
endlocal
|
||||
exit /b
|
||||
|
||||
:wrongSyntax
|
||||
@ -61,6 +69,7 @@ echo The syntax of the command is incorrect.
|
||||
echo.
|
||||
echo use /? for help
|
||||
echo.
|
||||
endlocal
|
||||
exit /b
|
||||
|
||||
:help
|
||||
@ -72,17 +81,26 @@ echo written by xiazeyu, inspired DRSDavidSoft.
|
||||
echo.
|
||||
echo Usage:
|
||||
echo.
|
||||
echo %%flag_exists%% [NOT] argName command
|
||||
echo %%flag_exists%% [/setPath] [NOT] flagName command/program [parameters]
|
||||
echo.
|
||||
echo NOT Specifies that %%flag_exists%% should carry out
|
||||
echo the command only if the condition is false.
|
||||
echo setPath Generate a global varible %%flag_exists%% for
|
||||
echo quicker use. Following arguments will be ignored.
|
||||
echo.
|
||||
echo argName Specifies which argument name is to detect.
|
||||
echo NOT Specifies that %%flag_exists%% should carry out
|
||||
echo the command only if the flag is missing.
|
||||
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 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.
|
||||
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.
|
||||
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.
|
||||
echo.
|
||||
echo Examples:
|
||||
echo.
|
||||
@ -93,7 +111,7 @@ echo Case 1:
|
||||
echo.
|
||||
echo The following command in user-profile.cmd would execute "notepad.exe"
|
||||
echo.
|
||||
echo call %%flag_exists%% "/startNotepad" "cmd /c start notepad.exe"
|
||||
echo call %%flag_exists%% "/startNotepad" "start" "notepad.exe"
|
||||
echo.
|
||||
echo if you pass parameter to init.bat like:
|
||||
echo.
|
||||
@ -103,10 +121,11 @@ echo Case 2:
|
||||
echo.
|
||||
echo The following command in user-profile.cmd would execute "notepad.exe"
|
||||
echo.
|
||||
echo call %%flag_exists%% NOT "/dontStartNotepad" "cmd /c start notepad.exe"
|
||||
echo call %%flag_exists%% NOT "/dontStartNotepad" "start" "notepad.exe"
|
||||
echo.
|
||||
echo UNLESS you pass parameter to init.bat like:
|
||||
echo.
|
||||
echo init.bat /dontStartNotepad
|
||||
echo.
|
||||
endlocal
|
||||
exit /b
|
||||
|
Reference in New Issue
Block a user