AoikBatchCmdLineArgsParser
Batch command line arguments parser:
Tested working with:
- Windows 10 cmd.exe
Inspired by: this post
See the code repository.
Features
Supported to define a normal option or a flag option.
Supported to define an option key to contain
%,^,!andspace.Supported to define an option default value to contain
%,^,!andspace.Supported to supply an option actual value to contain
%,^,!andspace.Supported to supply an option actual value that is empty.
Prevented
^and!in an option definition from being treated specially so no escaping or quoting is needed.Prevented
-keysupplied from matching--keydefined.Provided both
DisableDelayedExpansionandEnableDelayedExpansionversions.Added extensive comments to explain how the parsing works.
Usage
The
OPT_DEFSvariable contains a list of option definitions, delimited by unquotedspace.An option definition is an
_OPT_KEY_:_OPT_DFT_pair. The option key and option default value are delimited by:colon. E.g.--title:testdefines a normal option taking a value. E.g.--versiondefines a flag option taking no value."double quote should not be used either in an option definition, or in the middle of a command line argument, because it would cause quote mismatch which interferes with Batch's execution.'single quote is used to quote option default values. It is not allowed to be part of an option default value, but is ok to be part of an option actual value.Command line argument
--key=valis automatically converted to two arguments--keyandvalby CMD. The parser does not handle the--key=valstyle directly. If the Batch script is not run by CMD, e.g. by a Cygwin program instead, only the--key valstyle works, the--key=valstyle not works.If the execution failed with the error
The syntax of the command is incorrectorThe system cannot find the batch label specified, it might be caused by a bug of Batch that has something to do with the number of characters in the code. To let the error go away, try adding some comment lines to the middle of the code.
Examples
To get result
--flag=1, define option--flag, supply argument--flag.To get result
--key=val, define option--key:val, supply argument--key valor--key=val.To get result
--key=, define option--key:'', supply argument--key ""or--key="".To get result
--%=%, define option--%%:%%, supply argument--% %or--%=%.To get result
--^=^, define option--^:^, supply argument"--^" "^"or"--^"="^".To get result
--!=!, define option--!:!, supply argument--! !or--!=!.To get result
--key with spaces=val with spaces, define option'--key with spaces':'val with spaces', supply argument"--key with spaces" "val with spaces"or"--key with spaces"="val with spaces".
Comments: