Skip to content

Instantly share code, notes, and snippets.

@liorean
Last active February 10, 2020 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liorean/9c823c6a435a04bf5f61037353f75bfb to your computer and use it in GitHub Desktop.
Save liorean/9c823c6a435a04bf5f61037353f75bfb to your computer and use it in GitHub Desktop.
Parameter parsing
@echo off
setlocal enableextensions enabledelayedexpansion
title Parameter parsing
rem Handle database as UTF-8 instead of some system setting dependent ANSI code page.
for /f "tokens=2 delims=:." %%x in ('chcp') do set cp=%%x
chcp 65001 > nul
rem
rem Parse parameters, handle arguments
rem
set "raw="
set "flag="
set "opt="
set opt_def=default_value
set "man="
rem No arguments given is an error
if [%1] == [] goto :too_few_args
rem Otherwise enter arguments parsing loop
:parse_arg
set arg=%1
set lead=!arg:~0,1!
for %%x in (- /) do if [%%x] == [!lead!] goto :param
rem Argument didn't have a parameter decorator, so treat as raw argument.
:raw_arg
if defined raw goto :too_many_raw_args
if not defined raw set raw=!arg!
rem If raw argument is a file for us to open, check if it exists.
if not exist !raw! goto :file_not_found
goto :next
rem Argument had parameter decorator
:param
set param=!arg:~1!
if /i "!param!" == "?" goto :help
for %%x in (h help) do if [%%x] == [!param!] goto :help
for %%x in (f flag) do if [%%x] == [!param!] (
set flag=y
goto :next
)
for %%x in (F notflag) do if [%%x] == [!param!] (
set "flag="
goto :next
)
for %%x in (o opt) do if [%%x] == [!param!] (
set val_param=opt
goto :value
)
for %%x in (d opt_def) do if [%%x] == [!param!] (
set val_param=opt_def
goto :value
)
for %%x in (m man) do if [%%x] == [!param!] (
set val_param=man
goto :value
)
rem Maybe argument was not a parameter but a file that has a leading hyphen?
if defined raw goto :invalid_param
if not exist !arg! goto :invalid_param
set raw=!arg!
goto :next
rem If parameter takes a value send to specific handler
:value
shift /1
if [%1]==[] goto :missing_value
set value=%1
goto :value_!val_param!
:value_opt
rem Test allowed values here
set opt=!value!
goto :next
:value_opt_def
rem Test allowed values here
set opt_def=!value!
goto :next
:value_man
rem Test allowed values here
set man=!value!
goto :next
rem Advance to next argument if one exists
:next
shift /1
if [%1]==[] goto :final
goto :parse_arg
rem Final steps of the arguments parser
:final
if not defined man goto :man_missing
goto :program
rem
rem Program body
rem
:program
echo:%0
echo:raw argument: %raw%
echo:flag parameter: %flag%
echo:optional value parameter: %opt%
echo:optional default value parameter: %opt_def%
echo:mandatory value parameter: %man%
goto :end
rem
rem Print help text
rem
:help
echo:
echo:Command:
echo: {command}[.cmd] [^<parameters^>] ^<filename^> [^<parameters^>]
echo:
echo:
echo:Parameters:
echo: /f
echo: /flag
echo: Flag type parameter (enable, on, yes ...)
echo:
echo: /F
echo: /noflag
echo: Flag type parameter (disable, off, no ...)
echo:
echo: /o ^<optvalue^>
echo: /opt ^<optvalue^>
echo: Optional value parameter
echo:
echo: /d ^<defvalue^>
echo: /opt_def ^<defvalue^>
echo: Optional value parameter with a default value
echo: Defaults to default_value
echo:
echo: /m ^<manvalue^>
echo: /man ^<manvalue^>
echo: Mandatory value parameter
echo:
echo: /?
echo: /h
echo: /help
echo: Prints this help text
echo:
echo:
echo:Values:
echo: ^<optvalue^>
echo: Description of the allowed optional values.
echo:
echo: ^<defvalue^>
echo: Description of the allowed optional values with default.
echo:
echo: ^<manvalue^>
echo: Description of the allowed mandatory values.
echo:
goto :end
rem
rem Error handling
rem
:too_few_args
echo:--------
echo:ERROR!
echo: {command} requires at least one argument.
echo: No argument was specified.
echo:--------
goto :end
:too_many_raw_args
echo:--------
echo:ERROR!
echo: {command} requires exactly one non-parameter argument.
echo: You have specified both «!raw!» and «!arg!».
echo:--------
goto :end
:file_not_found
echo:--------
echo:ERROR!
echo: {command} could not find file «!raw!».
echo:--------
goto :end
:invalid_param
echo:--------
echo:ERROR!
echo: {command} does not recognise the parameter «!arg!».
echo:--------
goto :end
:missing_value
echo:--------
echo:ERROR!
echo: {command} «!arg!» requires an additional argument value.
echo:--------
goto :end
:man_missing
echo:--------
echo:ERROR!
echo: {command} requires mandatory parameter «man».
echo:--------
goto :end
rem
rem Restore code page and kill local variables
rem
:end
chcp %cp%
> nul
endlocal
goto :EOF
@liorean
Copy link
Author

liorean commented Feb 6, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment