Advertisement
AlvinSeville7cf

repeat.bat

Jan 20th, 2021
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 6.44 KB | None | 0 0
  1. @echo off
  2. setlocal
  3.  
  4. call :init
  5.  
  6. :main_loop
  7.     set "option=%~1"
  8.     set "value=%~2"
  9.  
  10.     set /a "is_help=%false%"
  11.     if "%option%" == "-h" set /a "is_help=%true%"
  12.     if "%option%" == "--help" set /a "is_help=%true%"
  13.  
  14.     if "%is_help%" == "%true%" (
  15.         call :help
  16.         exit /b %ec_success%
  17.     )
  18.  
  19.     set /a "is_version=%false%"
  20.     if "%option%" == "-v" set /a "is_version=%true%"
  21.     if "%option%" == "--version" set /a "is_version=%true%"
  22.  
  23.     if "%is_version%" == "%true%" (
  24.         call :version
  25.         exit /b %ec_success%
  26.     )
  27.  
  28.     set /a "is_interactive=%false%"
  29.     if "%option%" == "-i" set /a "is_interactive=%true%"
  30.     if "%option%" == "--interactive" set /a "is_interactive=%true%"
  31.  
  32.     if "%is_interactive%" == "%true%" (
  33.         call :interactive
  34.         exit /b %ec_success%
  35.     )
  36.  
  37.     set "string=%~1"
  38.     set "delimiter=%~2"
  39.     set "count=%~3"
  40.     set "next_argument=%~4"
  41.  
  42.     if not "%next_argument%" == "" (
  43.         echo %em_too_many_arguments%
  44.         exit /b %ec_too_many_arguments%
  45.     )
  46.  
  47.     call :repeat_string_syntax_check "%string%" "%delimiter%" "%count%"
  48.     set /a "temp_errorlevel=%errorlevel%"
  49.     if %temp_errorlevel% gtr 0 exit /b %temp_errorlevel%
  50.  
  51.     call :repeat_string string "%string%" "%count%"
  52.     echo.%string%
  53.     exit /b %ec_success%
  54.  
  55. :init
  56.     set /a "ec_success=0"
  57.  
  58.     set /a "ec_too_many_arguments=10"
  59.     set /a "ec_asterisk_expected=20"
  60.     set /a "ec_count_number_expected=21"
  61.  
  62.     set "em_too_many_arguments=Other options or string repetitions are not allowed after first string repetition construction."
  63.     set "em_asterisk_expected=Asterisk delimiter is not specified after string to repeat."
  64.     set "em_count_number_expected=Repetition count is not specified after asterisk delimiter."
  65.  
  66.     set /a "true=0"
  67.     set /a "false=1"
  68.  
  69.     set "prompt=>>> "
  70.  
  71.     call :set_esc
  72. exit /b %ec_success%
  73.  
  74. :help
  75.     echo Prints string repetition.
  76.     echo.
  77.     echo Syntax:
  78.     echo    repeat [options] string * count
  79.     echo.
  80.     echo Options:
  81.     echo    -h^|--help - writes help and exits
  82.     echo    -v^|--version - writes version and exits
  83.     echo    -i^|--interactive - fall in interactive mode
  84.     echo.
  85.     echo If string is specified before some option then it is ignored.
  86.     echo.
  87.     echo Interactive mode commands:
  88.     echo    q^|quit - exits
  89.     echo    c^|clear - clears screen
  90.     echo    h^|help - writes help
  91.     echo.
  92.     echo Examples:
  93.     echo    - repeat --help
  94.     echo    - repeat abc * 10
  95.     echo    - repeat abc * 10 --help (--help option is ignored)
  96. exit /b %ec_success%
  97.  
  98. :version
  99.     echo 1.0 ^(c^) 2021 year
  100. exit /b %ec_success%
  101.  
  102. :interactive
  103.     set /a "i_last_errorlevel=0"
  104.  
  105.     :interactive_loop
  106.         set /a "i_color_code=32"
  107.         if not %i_last_errorlevel% == 0 set /a "i_color_code=31"
  108.         set /p "i_command=%esc%[%i_color_code%m%i_last_errorlevel% %prompt%%esc%[0m"
  109.         call :separate_into_arguments i_string i_delimiter i_count i_next_argument %i_command%
  110.  
  111.         setlocal enabledelayedexpansion
  112.         if "!i_command!" == "" (
  113.             setlocal disabledelayedexpansion
  114.             goto interactive_loop
  115.         )
  116.         setlocal disabledelayedexpansion
  117.        
  118.         set "i_comment_regex=^#.*$"
  119.         echo %i_string%| findstr /R "%i_comment_regex%" 2> nul > nul && goto interactive_loop
  120.  
  121.         call set "i_command=%%i_command:!!=%i_previous_command%%%"
  122.         call :separate_into_arguments i_string i_delimiter i_count i_next_argument %i_command%
  123.  
  124.         set /a "i_is_quit=%false%"
  125.         if "%i_string%" == "q" set /a "i_is_quit=%true%"
  126.         if "%i_string%" == "quit" set /a "i_is_quit=%true%"
  127.  
  128.         if "%i_is_quit%" == "%true%" exit /b %ec_success%
  129.    
  130.         set /a "i_is_clear=%false%"
  131.         if "%i_string%" == "c" set /a "i_is_clear=%true%"
  132.         if "%i_string%" == "clear" set /a "i_is_clear=%true%"
  133.  
  134.         if "%i_is_clear%" == "%true%" (
  135.             cls
  136.             goto interactive_loop
  137.         )
  138.  
  139.         set /a "i_is_help=%false%"
  140.         if "%i_string%" == "h" set /a "i_is_help=%true%"
  141.         if "%i_string%" == "help" set /a "i_is_help=%true%"
  142.  
  143.         if "%i_is_help%" == "%true%" (
  144.             call :help
  145.             goto interactive_loop
  146.         )
  147.  
  148.         set "i_previous_command=%i_command%"
  149.  
  150.         if not "%i_next_argument%" == "" (
  151.             echo %em_too_many_arguments%
  152.             set /a "i_last_errorlevel=%ec_too_many_arguments%"
  153.             goto interactive_loop
  154.         )
  155.  
  156.         call :repeat_string_syntax_check "%i_string%" "%i_delimiter%" "%i_count%"
  157.         set /a "i_last_errorlevel=%errorlevel%"
  158.         if %i_last_errorlevel% neq 0 goto interactive_loop
  159.  
  160.         call :repeat_string i_string "%i_string%" "%i_count%"
  161.         echo.%i_string%
  162.         goto interactive_loop
  163. exit /b %ec_success%
  164.  
  165. :separate_into_arguments
  166.     set "sia_string_variable_name=%~1"
  167.     set "sia_delimiter_variable_name=%~2"
  168.     set "sia_count_variable_name=%~3"
  169.     set "sia_next_argument_variable_name=%~4"
  170.     set "sia_string=%~5"
  171.     set "sia_delimiter=%~6"
  172.     set "sia_count=%~7"
  173.     set "sia_next_argument=%~8"
  174.  
  175.     set "%sia_string_variable_name%=%sia_string%"
  176.     set "%sia_delimiter_variable_name%=%sia_delimiter%"
  177.     set "%sia_count_variable_name%=%sia_count%"
  178.     set "%sia_next_argument_variable_name%=%sia_next_argument%"
  179. exit /b %ec_success%
  180.  
  181. :repeat_string_syntax_check
  182.     set "rssc_string=%~1"
  183.     set "rssc_delimiter=%~2"
  184.     set "rssc_count=%~3"
  185.  
  186.     if not "%rssc_delimiter%" == "*" (
  187.         echo %em_asterisk_expected%
  188.         exit /b %ec_asterisk_expected%
  189.     )
  190.  
  191.     set "rssc_count_regex=^[0-9][0-9]*$"
  192.     echo %rssc_count%| findstr /r "%rssc_count_regex%" 2> nul > nul
  193.     if errorlevel 1 (
  194.         echo %em_count_number_expected%
  195.         exit /b %em_count_number_expected%
  196.     )
  197. exit /b %ec_success%
  198.  
  199. :repeat_string
  200.     set "rs_variable_name=%~1"
  201.     set "rs_string=%~2"
  202.     set "rs_count=%~3"
  203.  
  204.     set /a "rs_i=0"
  205.     set "rs_string_result="
  206.  
  207.     :rs_repetition_loop
  208.         if %rs_i% lss %rs_count% (
  209.             set "rs_string_result=%rs_string_result%%rs_string%"
  210.             set /a "rs_i+=1"
  211.             goto rs_repetition_loop
  212.         )
  213.  
  214.     set "%rs_variable_name%=%rs_string_result%"
  215. exit /b %ec_success%
  216.  
  217. :set_esc
  218.     for /f "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  219.         set "esc=%%b"
  220.         exit /b 0
  221.     )
  222. exit /b %ec_success%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement