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' | | /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' | | /user_aliases [file path] | File path pointing to user aliases. | '%CMDER_ROOT%\config\user-liases.cmd' |
| /v | Enables verbose output. | not set | | /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 ### 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: 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. 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 ### 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 "%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. :: Use /v command line arg or set to > 0 for verbose output to aid in debugging.
endlocal
set verbose-output=0 set verbose-output=0
set debug-output=0 set debug-output=0
set max_depth=1 set max_depth=1
set "CMDER_FLAGS=" set "CMDER_USER_FLAGS= "
:: Find root dir :: Find root dir
if not defined CMDER_ROOT ( if not defined CMDER_ROOT (
@ -82,7 +83,7 @@ call "%cmder_root%\vendor\lib\lib_profile"
set SVN_SSH=%2 set SVN_SSH=%2
shift shift
) else ( ) else (
set "CMDER_FLAGS=%CMDER_FLAGS% %1" set "CMDER_USER_FLAGS=%1 %CMDER_USER_FLAGS%"
) )
shift shift
goto var_loop goto var_loop
@ -293,14 +294,14 @@ if not defined HOME set "HOME=%USERPROFILE%"
set "initialConfig=%CMDER_ROOT%\config\user-profile.cmd" set "initialConfig=%CMDER_ROOT%\config\user-profile.cmd"
if exist "%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 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 ( if defined CMDER_USER_CONFIG (
set "initialConfig=%CMDER_USER_CONFIG%\user-profile.cmd" set "initialConfig=%CMDER_USER_CONFIG%\user-profile.cmd"
if exist "%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 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.
echo :: the next two lines is for "%%flag_exists%%" shortcut, a custom arguments handler echo :: the next two lines is for "%%flag_exists%%" shortcut, a custom arguments handler
echo :: don't remove it if you need it echo :: don't remove it if you need it
echo set "CMDER_USER_FLAGS=%%*" echo call "%%cmder_root%%\vendor\lib\flag_exists" "/setPath"
echo call "%%cmder_root%%\vendor\lib\flag_exists"
echo. 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"
@ -328,7 +328,7 @@ echo.
echo :: arguments in this batch are passed from init.bat, you can quickly 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 "%%flag_exists%% /?" echo :: more useage can be seen by typing "%%flag_exists%% /?"
echo. echo.
echo :: %%flag_exists%% "/customOption" "your custom command" echo :: %%flag_exists%% "/customOption" "command/program"
echo. echo.
echo @echo off echo @echo off
) >"%initialConfig%" ) >"%initialConfig%"

View File

@ -1,59 +1,67 @@
@echo off @echo off
set "flag_exists=%~dp0flag_exists"
setlocal setlocal
if "%~1" equ "" goto :wrongSyntax if "%~1" equ "" goto :wrongSyntax
if not defined CMDER_USER_FLAGS ( if not defined CMDER_USER_FLAGS (
:: in case nothing was passed to %CMDER_USER_FLAGS%
set "CMDER_USER_FLAGS= " set "CMDER_USER_FLAGS= "
) )
set "feNOT=false" set "feNot=false"
goto :parseArgument goto :parseArgument
:doShift :doShift
shift shift
:parseArgument :parseArgument
set "currenTarg=%~1" set "currenArgu=%~1"
if /i "%currenTarg%" == "/?" ( if /i "%currenArgu%" equ "/setPath" (
:: set %flag_exists% shortcut
endlocal
set "flag_exists=%~dp0flag_exists"
) else if /i "%currenArgu%" == "/?" (
goto :help goto :help
) else if /i "%currenTarg%" equ "/help" ( ) else if /i "%currenArgu%" equ "/help" (
goto :help goto :help
) else if /i "%currenTarg%" equ "/h" ( ) else if /i "%currenArgu%" equ "/h" (
goto :help goto :help
) else if /i "%currenTarg%" equ "NOT" ( ) else if /i "%currenArgu%" equ "NOT" (
set "feNOT=true" set "feNot=true"
goto :doShift goto :doShift
) else ( ) else (
if "%~1" equ "" goto :wrongSyntax if "%~1" equ "" goto :wrongSyntax
if "%~2" equ "" goto :wrongSyntax if "%~2" equ "" goto :wrongSyntax
set "feArgName=%~1" set "feFlagName=%~1"
set "feCommand=%~2" set "feCommand=%~2"
if not "%~3" equ "" (
set "feParam=%~3"
)
goto :detect goto :detect
) )
:detect :detect
:: to avoid erroneous deteciton like "/do" "/doNOT", both have a "/do" :: to avoid erroneous deteciton like "/do" "/doNOT", which both have a "/do"
:: but if it works like "/do " "/doNOT ", "/do " won't match "/doN" :: we added a space after the flag name, like "/do ", which won't match "/doN"
set "CMDER_USER_FLAGS=%CMDER_USER_FLAGS% " set "feFlagName=%feFlagName% "
set "feArgName=%feArgName% "
:: echo. :: echo.
:: echo %CMDER_USER_FLAGS% :: echo %CMDER_USER_FLAGS%
:: echo %feNOT% :: echo %feNOT%
:: echo %feArgName% :: echo %feFlagName%
:: echo %feCommand% :: echo %feCommand%
:: echo %feParam%
:: echo. :: echo.
echo %CMDER_USER_FLAGS% | find /i "%feArgName%">nul echo %CMDER_USER_FLAGS% | find /i "%feFlagName%">nul
if "%ERRORLEVEL%" == "0" ( if "%ERRORLEVEL%" == "0" (
if "%feNOT%" == "false" ( if "%feNOT%" == "false" (
call "%feCommand%" call %feCommand% %feParam%
) )
) else ( ) else (
if "%feNOT%" == "true" ( if "%feNOT%" == "true" (
call "%feCommand%" call %feCommand% %feParam%
) )
) )
endlocal
exit /b exit /b
:wrongSyntax :wrongSyntax
@ -61,6 +69,7 @@ echo The syntax of the command is incorrect.
echo. echo.
echo use /? for help echo use /? for help
echo. echo.
endlocal
exit /b exit /b
:help :help
@ -72,17 +81,26 @@ echo written by xiazeyu, inspired DRSDavidSoft.
echo. echo.
echo Usage: echo Usage:
echo. echo.
echo %%flag_exists%% [NOT] argName command echo %%flag_exists%% [/setPath] [NOT] flagName command/program [parameters]
echo. echo.
echo NOT Specifies that %%flag_exists%% should carry out echo setPath Generate a global varible %%flag_exists%% for
echo the command only if the condition is false. echo quicker use. Following arguments will be ignored.
echo. 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.
echo command Specifies the command to carry out if the echo flagName Specifies which flag name is to detect. It's recommand
echo argument name is detected. It's recommand to echo to use a pair of double quotation marks to wrap
echo use a pair of double quotation marks to echo your flag name to avoid exceed expectation.
echo wrap your command 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.
echo Examples: echo Examples:
echo. echo.
@ -93,7 +111,7 @@ echo Case 1:
echo. echo.
echo The following command in user-profile.cmd would execute "notepad.exe" echo The following command in user-profile.cmd would execute "notepad.exe"
echo. echo.
echo call %%flag_exists%% "/startNotepad" "cmd /c start notepad.exe" echo call %%flag_exists%% "/startNotepad" "start" "notepad.exe"
echo. echo.
echo if you pass parameter to init.bat like: echo if you pass parameter to init.bat like:
echo. echo.
@ -103,10 +121,11 @@ echo Case 2:
echo. echo.
echo The following command in user-profile.cmd would execute "notepad.exe" echo The following command in user-profile.cmd would execute "notepad.exe"
echo. echo.
echo call %%flag_exists%% NOT "/dontStartNotepad" "cmd /c start notepad.exe" echo call %%flag_exists%% NOT "/dontStartNotepad" "start" "notepad.exe"
echo. echo.
echo UNLESS you pass parameter to init.bat like: echo UNLESS you pass parameter to init.bat like:
echo. echo.
echo init.bat /dontStartNotepad echo init.bat /dontStartNotepad
echo. echo.
endlocal
exit /b exit /b