批处理选项代码示例 - 命令行参数解析指南
批处理选项是一种用于批处理脚本中的命令行参数的机制。以下是一些批处理选项的代码示例:
- '/?': 显示命令的帮助信息
@echo off
echo This is a batch file.
echo.
echo Usage: mybatchfile [/option]
echo.
echo '/?' Display this help message
echo '/v' Verbose output
echo '/l' Log output to file
echo.
- '/v': 打开详细输出模式
@echo off
set verbose=false
if '%1'=='/v' set verbose=true
if %verbose%==true (
echo Running in verbose mode.
rem 执行详细的命令
) else (
rem 执行普通的命令
)
- '/l': 记录输出到文件
@echo off
set log=false
if '%1'=='/l' set log=true
if %log%==true (
set logfilename=output.log
echo Logging output to %logfilename%...
rem 将输出写入文件
) else (
rem 将输出发送到控制台
)
- '/f': 指定输入文件
@echo off
if '%1'=='/f' (
if '%2'=='' (
echo Please specify the input file.
exit /b
) else (
set inputfile=%2
)
) else (
set inputfile=default.txt
)
echo Running with input file: %inputfile%
rem 执行命令,使用指定的输入文件
- '/o': 指定输出文件
@echo off
set outputfile=output.txt
if '%1'=='/o' (
if '%2'=='' (
echo Please specify the output file.
exit /b
) else (
set outputfile=%2
)
)
echo Running with output file: %outputfile%
rem 将输出写入指定的输出文件
这些示例演示了如何使用批处理选项在批处理脚本中实现命令行参数的解析和处理。
原文地址: https://www.cveoy.top/t/topic/lCfd 著作权归作者所有。请勿转载和采集!