The selected answer works, but it may use some improvement.
- Parameters should probably be initialized with default values.
- It would be nice to save% 0, as well as the necessary arguments% 1 and% 2.
- It becomes painful to have an IF block for each parameter, especially as the number of options increases.
- It would be nice to have a simple and concise way to quickly determine all the parameters and default values ββin one place.
- It would be useful to maintain autonomous parameters that serve as checkboxes (no value after the value).
- We do not know if arg is enclosed in quotation marks. We also do not know if the arg value was passed using escaped characters. It is better to access the argument using% ~ 1 and enclose the assignment in quotation marks. Then the party can rely on the absence of closed quotes, but special characters are still generally safe without exiting. (This is not bullet proof, but it copes with most situations).
My solution is based on creating an OPTIONS variable that defines all the parameters and their default values. OPTIONS are also used to check if the delivery option is valid. A huge amount of code is saved by simply storing the parameter values ββin variables named in the same way as the parameter. The amount of code is constant regardless of how many options are defined; only the definition of OPTIONS should change.
EDIT . In addition, the loop code must change if the number of required positional arguments changes. For example, often when all the arguments are called, in this case you want to analyze the arguments starting from position 1, not 3. Thus, inside the loop: all 3 become 1 and 4 become 2.
@echo off setlocal enableDelayedExpansion :: Define the option names along with default values, using a <space> :: delimiter between options. I'm using some generic option names, but :: normally each option would have a meaningful name. :: :: Each option has the format -name:[default] :: :: The option names are NOT case sensitive. :: :: Options that have a default value expect the subsequent command line :: argument to contain the value. If the option is not provided then the :: option is set to the default. If the default contains spaces, contains :: special characters, or starts with a colon, then it should be enclosed :: within double quotes. The default can be undefined by specifying the :: default as empty quotes "". :: NOTE - defaults cannot contain * or ? with this solution. :: :: Options that are specified without any default value are simply flags :: that are either defined or undefined. All flags start out undefined by :: default and become defined if the option is supplied. :: :: The order of the definitions is not important. :: set "options=-username:/ -option2:"" -option3:"three word default" -flag1: -flag2:" :: Set the default option values for %%O in (%options%) do for /f "tokens=1,* delims=:" %%A in ("%%O") do set "%%A=%%~B" :loop :: Validate and store the options, one at a time, using a loop. :: Options start at arg 3 in this example. Each SHIFT is done starting at :: the first option so required args are preserved. :: if not "%~3"=="" ( set "test=!options:*%~3:=! " if "!test!"=="!options! " ( rem No substitution was made so this is an invalid option. rem Error handling goes here. rem I will simply echo an error message. echo Error: Invalid option %~3 ) else if "!test:~0,1!"==" " ( rem Set the flag option using the option name. rem The value doesn't matter, it just needs to be defined. set "%~3=1" ) else ( rem Set the option value using the option as the name. rem and the next arg as the value set "%~3=%~4" shift /3 ) shift /3 goto :loop ) :: Now all supplied options are stored in variables whose names are the :: option names. Missing options have the default value, or are undefined if :: there is no default. :: The required args are still available in %1 and %2 (and %0 is also preserved) :: For this example I will simply echo all the option values, :: assuming any variable starting with - is an option. :: set - :: To get the value of a single parameter, just remember to include the `-` echo The value of -username is: !-username!
There really isnβt much code here. Most of the code above is comments. Here is the same code, no comment.
@echo off setlocal enableDelayedExpansion 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 "%~3"=="" ( set "test=!options:*%~3:=! " if "!test!"=="!options! " ( echo Error: Invalid option %~3 ) else if "!test:~0,1!"==" " ( set "%~3=1" ) else ( set "%~3=%~4" shift /3 ) shift /3 goto :loop ) set - :: To get the value of a single parameter, just remember to include the `-` echo The value of -username is: !-username!
This solution provides Unix style arguments in the Windows package. This is not the norm for Windows - a batch usually has parameters preceding the required arguments, and the parameters are prefixed with / .
The methods used in this solution are easily adaptable for Windows style options.
- The analysis loop always searches for a parameter in
%1 , and it continues until arg 1 starts with / - Note that SET assignments must be enclosed in quotation marks if the name begins with
/ .
SET /VAR=VALUE fails SET "/VAR=VALUE" works. In any case, I already do this in my decision. - Windows standard style excludes the possibility of the first argument value starting with
/ . This restriction can be eliminated by using the implicit // option, which serves as a signal to exit the parsing cycle. For option // nothing will be saved. "
Update 2015-12-28: Support ! in parameter values
In the above code, each argument expands when slow expansion is enabled, which means that ! most likely deprived, or something like !var! expands. Alternatively, ^ can also be deleted if present ! . The following small modification of uncommented code removes the restriction, so ! and ^ are stored in parameter values.
@echo off setlocal enableDelayedExpansion 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 "%~3"=="" ( set "test=!options:*%~3:=! " if "!test!"=="!options! " ( echo Error: Invalid option %~3 ) else if "!test:~0,1!"==" " ( set "%~3=1" ) else ( setlocal disableDelayedExpansion set "val=%~4" call :escapeVal setlocal enableDelayedExpansion for /f delims^=^ eol^= %%A in ("!val!") do endlocal&endlocal&set "%~3=%%A" ! shift /3 ) shift /3 goto :loop ) goto :endArgs :escapeVal set "val=%val:^=^^%" set "val=%val:!=^!%" exit /b :endArgs set - :: To get the value of a single parameter, just remember to include the `-` echo The value of -username is: !-username!