Tuesday, 8 September 2009

CMD/BAT: GetOptions

This is flexible and useful library to manipulate with arguments in CMD/BAT scripts.

SYNOPSIS

1. Minimal example
@echo off

:: Process command line options
call :getoptions %*

:: Show resulting variables and exit
set opts
goto :EOF

:: Put here the source code of the module from the end of the article
...
2. Extended example including the complete features of the module
:: Store in variables like myvar_XXX
set getoptions_name=myvar

:: Use the custom help output
set getoptions_help=myhelp

:: Automatical support for /h, /help and /man options
set getoptions_autohelp=1

:: Process command line options
call :getoptions %*

:: Perform a quick exit
if defined getoption_exit goto :EOF

:: Show resulting variables and exit
set myvar
goto :EOF

::The custom help output
:myhelp
echo This is the custom help output
goto :EOF

:: Put here the source code of the module from the end of the article
...

DESCRIPTION

This module implements an extended processing of options. It allows an arbitrary order of options both named like /key:value and unnamed ones simply like value.

CONFIGURE

This module allows to use several variables that affect on the processing of options. To affect on the processing you have to assigned one of them variables to the appropriate values.
getoptions_name
The string that starts options. If it is not defined the default value opts will be used. So each option will be stored in the variable started with opts_. Each named option looks like /key:value will be stored in the variable opts_/key with the value. Each unnamed variable will be stored in the numbered variables like follows: opts_1, etc. The number of options will be stored into two variables:
  • opts_total for the total number of options,
  • opts_count for the number of unnamed options.
getoptions_help
The label name (without the preceding colon) which will be used to output a help message. By default the getoptions_help will be used. This should be a label pointing to the subroutine which will output a help message.
getoptions_autohelp
Automatically provide support for the /h and /help options. To output a help message it requires the getoptions_autohelp to be defined with non-empty value. So when one of the options /h or /help will appear a help message will be outputted and the getoptions_exit variable will be defined.
getoptions_exit
This variable is used to perform a quick stop of the script execution if one of the help options /h or /help is defined.

SOURCE

The most recent source of the code below can be found by the following link cmd-bat::libs in google-code.
:: CMD/BAT GetOptions
:getoptions
if not defined getoptions_name set getoptions_name=opts
if not defined getoptions_help set getoptions_help=getoptions_help

set getoptions_i=0

set %getoptions_name%_total=0
set %getoptions_name%_count=0

:getoptions_loop_start

for /f "usebackq delims=: tokens=1,*" %%a in ( `echo %~1^|findstr "^/[a-z0-9_][a-z0-9_]*"` ) do (
    for %%h in ( /h /help /man ) do (
        if /i "%%~a" == "%%h" if defined getoptions_autohelp (
            call :%getoptions_help% %%~a
            set getoptions_exit=1
            goto :EOF
        )
    )

    if "%%~b" == "" (
        rem set %getoptions_name%_%%a=%%a
        call :getoptions_set "%getoptions_name%_%%a" "%%~a"
    ) else (
        rem set %getoptions_name%_%%a=%%b
        call :getoptions_set "%getoptions_name%_%%a" "%%~b"
    )

    goto getoptions_loop_continue
)

if "%~1" == "" goto getoptions_loop_break

set /a %getoptions_name%_count+=1

set /a getoptions_i+=1
rem set %getoptions_name%_%getoptions_i%=%1
call :getoptions_set "%getoptions_name%_%getoptions_i%" "%~1"

:getoptions_loop_continue
set /a %getoptions_name%_total+=1

shift
goto getoptions_loop_start
:getoptions_loop_break

set getoptions_i=

goto :EOF

:getoptions_set
set %~1=%~2
goto :EOF

:getoptions_help
echo.Usage:
echo.    %~n0 OPTIONS
echo.
echo.  where OPTIONS are in format like
echo.  VALUE or /NAME:VALUE
goto :EOF

No comments:

Post a Comment