Batch : Checking the number of parameters

后端 未结 3 1868
名媛妹妹
名媛妹妹 2021-02-18 22:55

I\'d like to make sure that when calling my batch, no more than 2 parameters are passed.

Is there an easy way to check that, or do I have to call SHIFT as many times as

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-18 23:59

    Here is my small example for gathering and parsing a list of arguments and passing to external command:

    @echo off
    setlocal enabledelayedexpansion
    
    if %1. EQU . (
        echo %~0 [-t NUM] FILE [FILE...]
        goto end
    )
    
    :args_loop
    if "%~1" EQU "-t" (
        set arg_t=%1
        set arg_t_val=%2
        shift
    ) else (
        set files=!files! %1
    )
    shift
    if %1. NEQ . goto args_loop
    
    :args_loop_end
    
    x:\path\to\external.exe %arg_t% %arg_t_val% %files%
    
    :end
    

提交回复
热议问题