Page 1 of 1

Entering batch argument groups during execution

Posted: 11 Nov 2020 20:38
by sambul36
Below is given a snippet from a lengthy batch. In this snippet a user is expected to enter batch arguments in a simple one line sequence without further questions asked. However, it works only when each argument is represented by one value. How to make it work for an argument represented by several values, such as random device ID sequence 1,3,5,9,15 all to be considered as one "value" array? In particular how to enter the next argument after such sequence?

Code: Select all

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "mes24=Do you want to enter batch arguments? Y/N"
set "mes25=Batch arguments (examples: s n y / t n / o 5 y / e 1 3) "
if "%1"=="" (
	choice /c yn /n /m "%mes24%" /t 10 /d n
		if !errorlevel! equ 1 (
			echo/ & set /p "camn=%mes25% > "
			if defined camn ("%~f0" !camn!)))
For example, I want to enter 3 argument groups: (e), (1 3 5 20), (y). How to do it in that sequence without making the code more complex?

Re: Entering batch argument groups during execution

Posted: 12 Nov 2020 08:34
by T3RRY
keeping to a similar style, here is one example of how to achieve this:

Code: Select all

@Echo off
(Set LF=^

%Do Not Modify%)
If Not "!![" == "[" Setlocal EnableExtensions EnableDelayedExpansion
:CheckArgs
If "%~1" == "" (
 Choice /N /M "Use Batch Args Y/N?"
 If !Errorlevel! Equ 1 (
  For %%n in (1 2 3)Do Set /P "Arg[%%n]=arg%%n type Description:"
 ) Else Goto :NoArgs
) Else Goto :Args
For %%n in (1 2 3)Do if "!Arg[%%n]!" == "" (
 Echo/Arg %%n missing
 Goto :CheckArgs
)
"%~f0" "%Arg[1]%" "%Arg[2]%" "%Arg[3]%"
Exit /B 0
:Args
 Echo/%*
:NoArgs
 Echo/Done

Re: Entering batch argument groups during execution

Posted: 13 Nov 2020 23:34
by sambul36
Thanks. Are any other approaches available to enter batch arguments in groups aiming for the shortest or less deviating from the above example code possible?

I found simpler approach: take an array parameter in quotes. Then process that parameter %~n as an array, which you have to do anyway:

Code: Select all

set /p "camn=%mes25% > " e "1 3 5 20" y
if defined camn ("%~f0" !camn!)
if not [%2]==[] (
	for %%k in (%~2) do ... )