How can I pass arguments to a batch file?

后端 未结 18 2167
陌清茗
陌清茗 2020-11-22 02:53

I need to pass an ID and a password to a batch file at the time of running rather than hardcoding them into the file.

Here\'s what the command line looks like:

18条回答
  •  太阳男子
    2020-11-22 03:22

    A friend was asking me about this subject recently, so I thought I'd post how I handle command-line arguments in batch files.

    This technique has a bit of 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:

    >template.bat [-f] [--flag] [/f] [--namedvalue value] arg1 [arg2][arg3][...]
    

    The jist of it is having the :init, :parse, and :main functions.

    Example usage

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

    template.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 ****                                   ****
        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
    

提交回复
热议问题