title: Batch script: navigation menu with highlighted selection
body: <p>I am obsessed with idea of mimicking functionality of 8-bit era programs by virtue of pure command line. So I want to make an array of menu options which can be selected with nominal (since there are no keymap for arrowkeys) left and right keys and confirmed with Enter. To keep things clear there is a working example I wrote. <strong>Please pay attention for ANSI escape codes I use A LOT, not sure if ESC control character would display in browser.</strong></p>
  
  <pre><code>@ECHO OFF
  
  ECHO Chose and option with [36mZ[0m for left, [36mX[0m for right, [36mC[0m for confirm
  
  :PICKMENU
  SETLOCAL
  SET menumsg="[7mExit [0m Next"
  SET menumsgDef="[7mExit [0m Next"
  SET menumsgNxt="[0mExit [7m Next"
  :subpick
  CHOICE /N /C zxc /m:[3;1H%menumsg%
  IF %errorlevel% EQU 0 ECHO Just in case of errors &amp; GOTO PICKMENU
  IF %errorlevel% EQU 1 SET menumsg=%menumsgDef% &amp; GOTO subpick
  IF %errorlevel% EQU 2 SET menumsg=%menumsgNxt% &amp; GOTO subpick
  IF %errorlevel% EQU 3 GOTO confirm
  :confirm
  IF %menumsg% EQU %menumsgNxt% (ECHO [0mNext option is chosen &amp; GOTO PICKMENU) ELSE (ECHO ^Exit is chosen! &amp; TIMEOUT 2 &gt;NUL &amp; EXIT)
  ENDLOCAL 
  
  PAUSE
  </code></pre>
  
  <p>My problem begins when I try to wrap this menu in some kind of a function, where I could pass arguments instead of making hodgie code every time I need menu or dialog:</p>
  
  <pre><code>@ECHO OFF
  
  REM ----So I start with setting variables with idea of argument string tokenisation on the mind----
  
  SET $teststring=OptionOne OptionTwo OptionThree 
  SET /A $currentitem=1
  
  REM ----I would use function that I found here on Stackoverflow to count amount of options in my string.----
  REM ----This is magic for me, without this solution I would be using FOR loop and delims----
  
  CALL :COUNTOPTIONS %$teststring%
  
  :COUNTOPTIONS
  SETLOCAL EnableDelayedExpansion
  SET /A Count=0
  :repeatme
  IF NOT "%1"=="" (
  SET /A Count+=1
  SHIFT
  GOTO :repeatme
  )
  ENDLOCAL &amp; SET $optionsamount=%Count%
  
  ECHO The number of words is %$optionsamount%
  
  PAUSE
  
  :PICKCURRENT
  
  REM ----So I got maximum number of options, I guess it would be useful since by design user could stumble upon the
  REM ----edge of menu by occasionaly pressing "left" or "right" controls too much. Thats how I limit the variable...
  
  IF %$currentitem% LSS 1 SET /A $currentitem=1
  IF %$currentitem% GTR %$optionsamount% SET /A $currentitem=%$optionsamount%
  ECHO %$optionsamount%
  
  REM ----This is where I completely lost it. I am trying to echo "options" string as separated items to the target location...-----
  
  FOR /F "tokens=*" %%A IN ("%$teststring%") DO ECHO [1;1H%%A
  
  REM ----...and echo "currently selected" item into the same place...
  
  FOR /F "tokens=%$currentitem%" %%B IN ("%$itemstring%") DO SET $selector=[1;1H[7m%%B[0m
  
  REM ----...and make it interactive with choice----
  
  CHOICE /N /C zxc /m:%$selector%
  IF %errorlevel% EQU 0 ECHO oops &amp; GOTO PICKITEM
  IF %errorlevel% EQU 1 SET /A "$currentitem=$currentitem-1" &amp; GOTO PICKCURRENT
  IF %errorlevel% EQU 2 SET /A "$currentitem=$currentitem+1" &amp; GOTO PICKCURRENT
  IF %errorlevel% EQU 3 GOTO confirm
  
  PAUSE
  </code></pre>
  
  <p>(I shoveled script structure in favor of illustrating what am I doing).</p>
  
  <p>Obviously, my problem is in lack of understanding loops and functions, but reading trough reference on ss64 and intense googling were not that enlightening so far. So I would appreciate pointing to education materials as well as comments on my code. I'd like to emphasize that the problem statement doesn't imply usage of additional libraries or resource kits.</p>