Can't parse arguments to variable
Posted: 12 Apr 2020 11:45
I am making a script that parses
to
The /SET... always starts with /Set and does not include double quotes.
and /OP... may or may not contain double quotes and multiple spaces.
This is the code I made to parse the arguments into variables.
This code can parse
into
However, this code cannot parse the arguments below.
This problem occurs when the IF or SET command is processing spaces or double quotes.
I can't find a better way to parse arguments into variables without error.
Is there another good way to parse arguments into variables?
Code: Select all
/SET1 OP1 OP2 /SET2 OP3
Code: Select all
/SET1 "WORD" OP1 OP2
/SET2 "WORD" OP3
and /OP... may or may not contain double quotes and multiple spaces.
This is the code I made to parse the arguments into variables.
Code: Select all
@ECHO OFF
:: Also, This code is a part of and script.
SETLOCAL ENABLEDELAYEDEXPANSION
SET "OP=%~1 "JobName""
IF /I NOT "!OP:~0,4!" == "/Set" (
ECHO Unrecognized option: "%~1"
ENDLOCAL & EXIT /B 1
)
IF NOT [%1] == [] SETLOCAL DISABLEDELAYEDEXPANSION & GOTO ARGLoop
:START
ECHO DONE
PAUSE
EXIT /B
:ARGLoop
SHIFT
IF [%1] ==[] (
PROGRAM %OP% 2> NUL || ECHO Command Failed: "PROGRAM %OP%"
ENDLOCAL & GOTO START
)
SET "ARG=%1"
IF /I "%ARG:~0,4%" == "/Set" (
PROGRAM %OP% 2> NUL || ECHO Command Failed: "PROGRAM %OP%"
SET "OP=%1 "JobName""
) ELSE (SET "OP=%OP% %1")
GOTO ARGLoop
Code: Select all
/SetHttpMethod OPTIONS /SetMaxDownloadTime 10
Code: Select all
/SetHttpMethod "JobName" OPTIONS
/SetMaxDownloadTime "JobName" 10
Code: Select all
/SetCustomHeaders "User-Agent: Opera/9.60 (Windows NT 6.0; U; en) Presto/2.1.1" /SetHttpMethod OPTIONS /SetMaxDownloadTime 10
I can't find a better way to parse arguments into variables without error.
Is there another good way to parse arguments into variables?