Last active
June 27, 2019 04:36
-
-
Save loopyd/604753697c4b55022f85408fca12f25b to your computer and use it in GitHub Desktop.
Command line options for windows batch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
setlocal enableDelayedExpansion enableExtensions | |
REM @filename getopts.cmd | |
REM @link <a href="https://gist.github.com/loopyd">gist.github.com/loopyd</a> | |
REM @editor loopyd <<a href="mailto:[email protected]">[email protected]</a>> LupineDream | |
REM @author <a href="https://stackoverflow.com/users/1012053/dbenham">dbenham</a> | |
REM @description Use is always free, with credit! \ | |
REM \ | |
REM You may use this script at your leasure for testing purposes. \ | |
REM \ | |
REM You are not to remove these comments from this file if you so choose to use \ | |
REM my code, please give credit where it is do by abiding by this simple request! \ | |
REM Thank you~! | |
REM Changelog: | |
REM 1.0 initial copypasta | |
REM 1.1 fixed typos | |
REM 1.2 fix argument shifting | |
set "options=-username:/ -option2:"" -option3:"three word default" -flag1: -flag2:" | |
for %%O in (%options%) do for /f "tokens=1,* delims=:" %%A in ("%%O") do set "%%A=%%~B" | |
:loop | |
if not "%~1"=="" ( | |
set "test=!options:*%~1:=! " | |
if "!test!"=="!options! " ( | |
echo. Error: Invalid option %~1 | |
) else if "!test:~0,1!"==" " ( | |
set "%~1=t" | |
) else ( | |
setlocal disableDelayedExpansion | |
set "val=%~2" | |
call :escapeVal | |
setlocal enableDelayedExpansion | |
for /f delims^=^ eol^= %%A in ("!val!") do endlocal&endlocal&set "%~1=%%A" ! | |
shift | |
) | |
shift | |
goto :loop | |
) | |
goto :endArgs | |
:escapeVal | |
set "val=%val:^=^^%" | |
set "val=%val:!=^!%" | |
exit /b | |
:endArgs | |
rem just output them for now. | |
set - |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment