Reinterpreting macros in command-line context
Posted: 10 Jun 2023 20:19
I have a script with an alias definition like this:
It obviously works. However, I would like to reinterpret it in order to get rid of all the indentation so that it's easier to parse visually from the command-line (via e.g. doskey /macros | findstr /birc:"!!="). Well then:
Two problems arise:
For reference my use-case can be found here.
Code: Select all
set ^"$true=(call )"
doskey ^!^! = (^
if "^!^"=="^!" (^
(echo(Delayed expansion is ON.)^
) else (^
(echo(Delayed expansion is OFF.)^
)^
) ^>con ^& %$true%
Code: Select all
setlocal EnableDelayedExpansion
if /i "%~f0"=="%~dpnx0" (
set ^"$p=%%<nul"
) else (
set ^"$p=^%<nul"
)
for /f "tokens=1,* delims== " %$p%l in ('doskey /macros:cmd.exe') do (
set "label=%$p%~l"
set "macro=%$p%~m"
set "macro=!macro: =!"
doskey !label! = !macro!
)
endlocal
- Hazardous characters: ^ and ! obviously have to be escaped (else illegal macro definition). Normally I would use goto in order to escape these in an eager expansion scope (DisableDelayedExpansion), but...
- I would like to source the script via pipe to cmd.exe (as another program will be in charge of producing it). This means I will be operating in command-line context, thus setlocal is out of the window.
For reference my use-case can be found here.