Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 711 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I pass arguments to a batch file?

#21
This technique has a bit of an overhead as you'll see, but it makes my batch files very easy to understand and quick to implement. As well as supporting the following structures:

```cmd
>template.bat [-f] [--flag] [--namedvalue value] arg1 [arg2][arg3][...]
```
The jist of it is having the `:init`, `:parse`, and `:main` functions.

## template.bat ##




```bat
@::!/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 **** MISSING "REQUIRED ARGUMENT" ****
echo.
goto :eof

:init
set "__NAME=%~n0"
set "__VERSION=1.24"
set "__YEAR=2023"

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 /i "%~1"=="-f" 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
```








| Example usage | returns |
| ---------------------------- | -------------- |
|template.bat<br><br><br><br><br><br><br><br><br><br><br><br><br><br>|template v1.24<br>This is a sample batch file template,<br>providing command-line arguments and flags.<br><br>USAGE:<br>template.bat [flags] "required argument" "optional argument"<br> <br>/?, --help shows this help<br>/v, --version shows the version<br>/e, --verbose shows detailed output<br>-f, --flag value specifies a named parameter value<br><br>**** MISSING "REQUIRED ARGUMENT" ****<br>|
|template.bat /? <br><br><br><br><br><br><br><br><br><br><br><br>|template v1.24<br>This is a sample batch file template,<br>providing command-line arguments and flags.<br><br>USAGE:<br>template.bat [flags] "required argument" "optional argument"<br> <br>/?, --help shows this help<br>/v, --version shows the version<br>/e, --verbose shows detailed output<br>-f, --flag value specifies a named parameter value|
|template.bat --v | 1.24 |
|template.bat --version <br><br><br>| template v1.24<br>This is a sample batch file template,<br>providing command-line arguments and flags. |
|template.bat -e arg1 <br><br><br><br>|**** DEBUG IS ON<br>UnNamedArgument: "arg1"<br>UnNamedOptionalArg: not provided<br>NamedFlag: not provided|
|template.bat --flag "my flag" arg1 arg2 <br> <i>(without -e)</i><br><br> | UnNamedArgument: "arg1" <br>UnNamedOptionalArg: "arg2"<br>NamedFlag: "my flag" |
|template.bat --verbose "argument #1" --flag "my flag" second<br><br><br>|**** DEBUG IS ON<br>UnNamedArgument: "argument #1"<br>UnNamedOptionalArg: "second"<br>NamedFlag: "my flag"|
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through