TCL中getopts的使用方法
How to use getopts in TCL
我想使用 get opts to 运行 获取文件名。
我希望能够输入这样的命令:
tclsh -f application_report.txt delete_file.tcl
然后可以在我的脚本中打印:
application_report.txt
你必须把脚本的名字放在第一位,因为它是由 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
行。
我想使用 get opts to 运行 获取文件名。
我希望能够输入这样的命令:
tclsh -f application_report.txt delete_file.tcl
然后可以在我的脚本中打印:
application_report.txt
你必须把脚本的名字放在第一位,因为它是由 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
行。