Quando scrivo file batch, solitamente si basano sul seguente modello:
@title Optional Title
@echo off
setlocal EnableExtensions DisableDelayedExpansion
::Comment that describes purpose and usage of the script; this should also
::contain (original) script name, version, date and author, if applicable.
rem Define global constants (read-only variables) here (names begin with '_').
rem Initialise global variables (those accessed also by sub-routines) here,
rem if any (names begin with '$').
rem Parse command line arguments and/or user input data here;
rem regard cases when too few or too many arguments are provided;
rem regard cases when user input is empty.
rem Place code for processing here:
rem In general, avoid non-ASCII characters within functional code;
rem use lower-case (for commands, sub-commands, verbs and keywords);
rem use upper-case only for switches, variable names and 'for' references;
rem place single space between commands, switches and arguments;
rem place single spaces around redirection operators too;
rem avoid excessive line concatenation (by '&');
rem insert explanatory comments using the 'rem' command.
(
rem Indent parenthesised blocks.
)
rem Return resulting output here.
endlocal
exit /B
:SUB rtn_return ref_variable val_value
setlocal DisableDelayedExpansion
::Comment that describes purpose and usage of the sub-routine;
::include the meanings of the aforementioned arguments (like:
::'rtn_*' denote return values, so they hold variable names;
::'ref_*' denote references, so they hold variable names too;
::'val_*' denote values, so they hold immediate values).
rem Define local constants here (names begin with '_').
rem Parse arguments here; check whether too few/many arguments are given;
rem Place code for processing here.
rem Return resulting output here; regard the 'endlocal' barrier
rem (for 'rtn_*' arguments, whose values need to survive it).
endlocal
exit /B
Se un sistema di guida (supporto per /?
switch) sta per essere implementato, solitamente utilizzo questo modello:
@title Optional Title
@echo off
setlocal EnableExtensions DisableDelayedExpansion
::Comment that describes purpose and usage of the script; this should also
::contain (original) script name, version, date and author, if applicable.
::::
::::"%~nx0" [version 0.0]
::::
::::Help message text that describes the purpose of the script in detail;
::::some parts of that text may be placed elsewhere in the script.
::::The continued text could describe the exact usage of the script,
::::illustrating the syntax and listing all the accepted parameters.
::::
rem Define global constants (read-only variables) here (names begin with '_').
rem Initialise global variables (those accessed also by sub-routines) here,
rem if any (names begin with '$').
rem Parse command line arguments and/or user input data here;
rem regard cases when too few or too many arguments are provided;
rem regard cases when user input is empty.
if "%~1"=="/?" call :HLP & exit /B
rem Place code for processing here:
rem In general, avoid non-ASCII characters within functional code;
rem use lower-case (for commands, sub-commands, verbs and keywords);
rem use upper-case only for switches, variable names and 'for' references;
rem place single space between commands, switches and arguments;
rem place single spaces around redirection operators too;
rem avoid excessive line concatenation (by '&');
rem insert explanatory comments using the 'rem' command.
(
rem Indent parenthesised blocks.
)
rem Return resulting output here.
endlocal
exit /B
:SUB rtn_return ref_variable val_value
setlocal DisableDelayedExpansion
::Comment that describes purpose and usage of the sub-routine;
::include the meanings of the aforementioned arguments (like:
::'rtn_*' denote return values, so they hold variable names;
::'ref_*' denote references, so they hold variable names too;
::'val_*' denote values, so they hold immediate values).
rem Define local constants here (names begin with '_').
rem Parse arguments here; check whether too few/many arguments are given;
rem Place code for processing here.
rem Return resulting output here; regard the 'endlocal' barrier
rem (for 'rtn_*' arguments, whose values need to survive it).
endlocal
exit /B
:HLP
setlocal DisableDelayedExpansion
for /F "delims=" %%H in ('findstr /B /L "::::" "%~f0"') do (
call set "LINE=%%H"
setlocal EnableDelayedExpansion
echo(!LINE:*::::=!
endlocal
)
endlocal
exit /B
::::
:::: USAGE:
::::
:::: %~nx0 [/optional] [/switch] {/alternative | /switches} parameter
::::
:::: /optional optional switch
:::: /switch optional switch
:::: /alternative alternative switch
:::: /switches alternative switch
:::: parameter required parameter
::::
Scopri tutti i commenti che contengono i consigli più importanti sullo stile di codifica.