您必须先输入脚本的名称,因为它是由tclsh 自己处理的,以知道要运行什么命令。选项随之而来。 (事实上,tclsh 本身确实接受少量选项,但您几乎不想提供它们,除非脚本编码的自动检测出错。)
当您使用cmdline package 时,请按以下步骤操作:
脚本:
package require cmdline
# Description of the options
set OPTIONS {
{f.arg "output file"}
}
# Part of the usage message; feel free to tweak
set USAGE ": tclsh delete_file.tcl \[options]\noptions:"
# Initialise some defaults; totally not necessary...
dict set opts {
f "default_file.txt"
}
# Process the options
try {
array set opts [cmdline::getoptions argv $OPTIONS $USAGE]
} trap {CMDLINE USAGE} {msg} {
puts stderr $msg
exit 1
}
# Now, we can just use them; $::argv is everything not processed
set filename $opts(f)
# ...
如何调用:
tclsh delete_file.tcl -f application_report.txt
如果您在脚本顶部使用#! 行,您可能需要调整USAGE 行。