Created
July 31, 2019 23:48
-
-
Save kodybrown/2caa509f7d0e57f18f2ee70c99ef7568 to your computer and use it in GitHub Desktop.
batch file boilerplate template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>template.bat /? | |
test v1.23 | |
This is a sample batch file template, | |
providing command-line arguments and flags. | |
USAGE: | |
test.bat [flags] "required argument" "optional argument" | |
/?, --help shows this help | |
/v, --version shows the version | |
/e, --verbose shows detailed output | |
-f, --flag value specifies a named parameter value | |
>template.bat <- throws missing argument error | |
(same as /?, plus..) | |
**** **** | |
**** MISSING "REQUIRED ARGUMENT" **** | |
**** **** | |
>template.bat -v | |
1.23 | |
>template.bat --version | |
test v1.23 | |
This is a sample batch file template, | |
providing command-line arguments and flags. | |
>template.bat -e arg1 | |
**** DEBUG IS ON | |
UnNamedArgument: "arg1" | |
UnNamedOptionalArg: not provided | |
NamedFlag: not provided | |
>template.bat --flag "my flag" arg1 arg2 | |
UnNamedArgument: "arg1" | |
UnNamedOptionalArg: "arg2" | |
NamedFlag: "my flag" | |
>template.bat --verbose "argument #1" --flag "my flag" second | |
**** DEBUG IS ON | |
UnNamedArgument: "argument #1" | |
UnNamedOptionalArg: "second" | |
NamedFlag: "my flag" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@::!/dos/rocks | |
@echo off | |
goto :init | |
:header | |
echo %__NAME% v%__VERSION% | |
echo This is a sample batch file template, | |
echo providing command-line arguments and flags. | |
echo. | |
goto :eof | |
:usage | |
echo USAGE: | |
echo %__BAT_NAME% [flags] "required argument" "optional argument" | |
echo. | |
echo. /?, --help shows this help | |
echo. /v, --version shows the version | |
echo. /e, --verbose shows detailed output | |
echo. -f, --flag value specifies a named parameter value | |
goto :eof | |
:version | |
if "%~1"=="full" call :header & goto :eof | |
echo %__VERSION% | |
goto :eof | |
:missing_argument | |
call :header | |
call :usage | |
echo. | |
echo **** **** | |
echo **** MISSING "REQUIRED ARGUMENT" **** | |
echo **** **** | |
echo. | |
goto :eof | |
:init | |
set "__NAME=%~n0" | |
set "__VERSION=1.23" | |
set "__YEAR=2017" | |
set "__BAT_FILE=%~0" | |
set "__BAT_PATH=%~dp0" | |
set "__BAT_NAME=%~nx0" | |
set "OptHelp=" | |
set "OptVersion=" | |
set "OptVerbose=" | |
set "UnNamedArgument=" | |
set "UnNamedOptionalArg=" | |
set "NamedFlag=" | |
:parse | |
if "%~1"=="" goto :validate | |
if /i "%~1"=="/?" call :header & call :usage "%~2" & goto :end | |
if /i "%~1"=="-?" call :header & call :usage "%~2" & goto :end | |
if /i "%~1"=="--help" call :header & call :usage "%~2" & goto :end | |
if /i "%~1"=="/v" call :version & goto :end | |
if /i "%~1"=="-v" call :version & goto :end | |
if /i "%~1"=="--version" call :version full & goto :end | |
if /i "%~1"=="/e" set "OptVerbose=yes" & shift & goto :parse | |
if /i "%~1"=="-e" set "OptVerbose=yes" & shift & goto :parse | |
if /i "%~1"=="--verbose" set "OptVerbose=yes" & shift & goto :parse | |
if /i "%~1"=="--flag" set "NamedFlag=%~2" & shift & shift & goto :parse | |
if not defined UnNamedArgument set "UnNamedArgument=%~1" & shift & goto :parse | |
if not defined UnNamedOptionalArg set "UnNamedOptionalArg=%~1" & shift & goto :parse | |
shift | |
goto :parse | |
:validate | |
if not defined UnNamedArgument call :missing_argument & goto :end | |
:main | |
if defined OptVerbose ( | |
echo **** DEBUG IS ON | |
) | |
echo UnNamedArgument: "%UnNamedArgument%" | |
if defined UnNamedOptionalArg echo UnNamedOptionalArg: "%UnNamedOptionalArg%" | |
if not defined UnNamedOptionalArg echo UnNamedOptionalArg: not provided | |
if defined NamedFlag echo NamedFlag: "%NamedFlag%" | |
if not defined NamedFlag echo NamedFlag: not provided | |
:end | |
call :cleanup | |
exit /B | |
:cleanup | |
REM The cleanup function is only really necessary if you | |
REM are _not_ using SETLOCAL. | |
set "__NAME=" | |
set "__VERSION=" | |
set "__YEAR=" | |
set "__BAT_FILE=" | |
set "__BAT_PATH=" | |
set "__BAT_NAME=" | |
set "OptHelp=" | |
set "OptVersion=" | |
set "OptVerbose=" | |
set "UnNamedArgument=" | |
set "UnNamedArgument2=" | |
set "NamedFlag=" | |
goto :eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment