refactor: reduce global varible useage, fixed quote issue, added parameters support

This commit is contained in:
xiazeyu 2018-08-06 13:09:59 +08:00
parent b3200efa7e
commit f6c2d9c31e
No known key found for this signature in database
GPG Key ID: F8162BE75DCCDC2D
3 changed files with 55 additions and 35 deletions

View File

@ -148,6 +148,7 @@ You may find some Monokai color schemes for mintty to match Cmder [here](https:/
| /svn_ssh [path to ssh.exe] | Define %SVN_SSH% so we can use git svn with ssh svn repositories. | '%GIT_INSTALL_ROOT%\bin\ssh.exe' |
| /user_aliases [file path] | File path pointing to user aliases. | '%CMDER_ROOT%\config\user-liases.cmd' |
| /v | Enables verbose output. | not set |
| (custom arguments) | User defined arguments processed by `flag_exists`. Type `%flag_exists% /?` for more useage. | not set |
### Cmder Shell User Config
Single user portable configuration is possible using the cmder specific shell config files. Edit the below files to add your own configuration:
@ -278,7 +279,7 @@ init.bat
the `notepad.exe` won't be executed.
Detailed usage of `%flag_exists%` can be seen by typing `%flag_exists% /?` in cmder.
To see detailed usage of `%flag_exists%`, type `%flag_exists% /?` in cmder.
### Integrating Cmder with [Hyper](https://github.com/zeit/hyper), [Microsoft VS Code](https://code.visualstudio.com/), and your favorite IDEs

14
vendor/init.bat vendored
View File

@ -7,10 +7,11 @@
:: !!! Use "%CMDER_ROOT%\config\user-profile.cmd" to add your own startup commands
:: Use /v command line arg or set to > 0 for verbose output to aid in debugging.
endlocal
set verbose-output=0
set debug-output=0
set max_depth=1
set "CMDER_FLAGS="
set "CMDER_USER_FLAGS= "
:: Find root dir
if not defined CMDER_ROOT (
@ -82,7 +83,7 @@ call "%cmder_root%\vendor\lib\lib_profile"
set SVN_SSH=%2
shift
) else (
set "CMDER_FLAGS=%CMDER_FLAGS% %1"
set "CMDER_USER_FLAGS=%1 %CMDER_USER_FLAGS%"
)
shift
goto var_loop
@ -293,14 +294,14 @@ if not defined HOME set "HOME=%USERPROFILE%"
set "initialConfig=%CMDER_ROOT%\config\user-profile.cmd"
if exist "%CMDER_ROOT%\config\user-profile.cmd" (
REM Create this file and place your own command in there
call "%CMDER_ROOT%\config\user-profile.cmd" %CMDER_FLAGS%
call "%CMDER_ROOT%\config\user-profile.cmd"
)
if defined CMDER_USER_CONFIG (
set "initialConfig=%CMDER_USER_CONFIG%\user-profile.cmd"
if exist "%CMDER_USER_CONFIG%\user-profile.cmd" (
REM Create this file and place your own command in there
call "%CMDER_USER_CONFIG%\user-profile.cmd" %CMDER_FLAGS%
call "%CMDER_USER_CONFIG%\user-profile.cmd"
)
)
@ -312,8 +313,7 @@ echo :: use in front of the command to prevent printing the command
echo.
echo :: the next two lines is for "%%flag_exists%%" shortcut, a custom arguments handler
echo :: don't remove it if you need it
echo set "CMDER_USER_FLAGS=%%*"
echo call "%%cmder_root%%\vendor\lib\flag_exists"
echo call "%%cmder_root%%\vendor\lib\flag_exists" "/setPath"
echo.
echo :: uncomment this to have the ssh agent load when cmder starts
echo :: call "%%GIT_INSTALL_ROOT%%/cmd/start-ssh-agent.cmd"
@ -328,7 +328,7 @@ echo.
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 "%%flag_exists%% /?"
echo.
echo :: %%flag_exists%% "/customOption" "your custom command"
echo :: %%flag_exists%% "/customOption" "command/program"
echo.
echo @echo off
) >"%initialConfig%"

View File

@ -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