Windows command line options (cmd.exe) and string manipulation

Consider this FOR loop in a Windows script package:

D:\MiLu\Dev\C++\temp :: type string.bat @ECHO OFF FOR %%a IN (%*) DO ECHO %%a 

He drops all arguments one by one. Really?

 D:\MiLu\Dev\C++\temp :: string.bat foo.obj bar.obj CPPFLAGS=/EHsc foo.obj bar.obj CPPFLAGS /EHsc 

It separates command line arguments not only into spaces (good), but also by = (not good). How can I prevent this?

What I want to achieve is simple: a wrapper around NMAKE.exe that points /nologo to nmake, and also - and this is a problem - to the compiler via the CFLAGS and CPPFLAGS environment variables, at the same time including any settings for CFLAGS and CPPFLAGS supplied on the command line.

In other words, I want the script to add /nologo to the command line input for CFLAGS and CPPFLAGS, even if they are not. Always /nologo ! Do not annoy me with your logo, friend compiler!

Update

Here's what I thought up based on Mike's answer:

 @ECHO OFF SETLOCAL SETLOCAL ENABLEDELAYEDEXPANSION FOR %%a IN (%*) DO ( SET var1=%%a ECHO %%a - !var1! - !var1:~0,1! IF "!var1:~0,1!" EQU "/" ( ECHO gefunden: %%a !var1! ) ) 

Going to continue tomorrow ...

Update 2

Well, considering that tomorrow is already here, I could continue as well ... so proudly it seemed to be a working solution. Feel free to comment on how to improve it.

 @ECHO OFF SETLOCAL SETLOCAL ENABLEDELAYEDEXPANSION SET files= SET CFLAGS=/nologo %CFLAGS% SET CPPFLAGS=/nologo %CPPFLAGS% SET state=normal FOR %%a IN (%*) DO ( SET curarg=%%a REM ECHO %%a - !curarg! - !curarg:~0,1! IF /I "%%a" EQU "CFLAGS" ( SET state=expecting_cflags ) ELSE IF /I "%%a" EQU "CPPFLAGS" ( SET state=expecting_cppflags ) ELSE ( IF "!curarg:~0,1!" EQU "/" ( REM ECHO gefunden: %%a !curarg! IF "!state!" EQU "expecting_cflags" ( REM ECHO expecting_cflags SET CFLAGS=!CFLAGS! !curarg! ) ELSE IF "!state!" EQU "expecting_cppflags" ( REM ECHO expecting_cppflags SET CPPFLAGS=!CPPFLAGS! !curarg! ) ELSE ( ECHO Logikfehler >&2 ) ) ELSE ( SET files=!files! !curarg! ) SET state=normal ) ) ECHO Dateien: !files! >&2 ECHO CFLAGS: !CFLAGS! >&2 ECHO CPPFLAGS: !CPPFLAGS! >&2 :: ECHO ON nmake /nologo %files% CFLAGS="%CFLAGS%" CPPFLAGS="%CPPFLAGS%" ENDLOCAL 
+4
source share
3 answers

One way to solve the problem is to specify "CPPFLAGS = / EHsc" and then use %% ~ a in the loop to get rid of double quotes.

Another way to solve the problem is to check the first character %% a, and if it is / , then add = to it. To do this, you need to setlocal enabledelayedexpansion , assign %% a to a variable, and then use the%: ~ 1.1% variable to extract the first character so you can compare it with / . For more information about this notation, type help set .

Update (after updating OP)

The following snippet seems to work, it is slightly simpler than the corresponding snippet in your solution, and it does not contain hard-coded argument names, so it is more universal:

 SET allargs= FOR %%a IN (%*) DO ( SET curarg=%%a IF "!curarg:~0,1!" EQU "/" ( SET allargs=!allargs!=!curarg! ) ELSE ( SET allargs=!allargs! !curarg! ) ) ECHO !allargs! 
+2
source

Have you tried the following?

 string.bat foo.obj bar.obj "CPPFLAGS=/EHsc" 

If you add the CPPFLAGS argument yourself, put it in quotation marks.

Link: http://www.robvanderwoude.com/parameters.php

+2
source

You can use for /f in a loop to force the argument list to be separated by spaces, rather than by equal signs. Try something like this:

 @echo off setlocal enabledelayedexpansion set params=%* :loop for /f "usebackq tokens=1,*" %%A in ('!params!') do ( echo %%A set params=%%B ) if not "!params!"=="" goto loop endlocal 
+2
source

Source: https://habr.com/ru/post/1393667/


All Articles