Smalltalk 中未记录的 getopt
我正在编写一个命令行应用程序,用于加密密码和解密密码哈希值。 Getopt 的文档没有提供示例,所以我不知道如何使用 Getopt 类。 邮件列表日志中散布着一些线索。
特别是,我不知道 with:pattern
的格式、指定 CLI 参数的字符串以及值是必需的、可选的还是省略的。
ios7crypt.st:
"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
| args password hash |
"Drop the program name."
"The shebang joins the arguments; we must split them."
args := (Smalltalk getArgv: 2) substrings: $ .
args do: [ :arg | Transcript show: 'Raw arg: ', arg; cr. ].
Getopt parse: args with: '-e: -d: -t' do: [ :opt :arg |
Transcript show: 'Opt: ', (opt asString), ' Arg: ', arg; cr.
"..."
].
运行示例:
$ ./ios7crypt.st -e monkey
Raw arg: -e
Raw arg: monkey
Opt: e Arg: monkey
之前,-e
和 monkey
都传递给脚本,但 Getopt 的 do:
默默地删除了 -e
,所以输出如下:
$ ./ios7crypt.st -e monkey
Raw arg: -e
Raw arg: monkey
Opt: Arg: monkey
I'm writing a command line app that encrypts passwords and decrypts password hashes. The docs for Getopt provide no examples, so I have no idea how to use the Getopt class. A few clues are scattered in mailing list logs.
In particular, I don't know the format of with: pattern
, the string specifying CLI arguments and whether values are required, optional, or omitted.
ios7crypt.st:
"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
| args password hash |
"Drop the program name."
"The shebang joins the arguments; we must split them."
args := (Smalltalk getArgv: 2) substrings: $ .
args do: [ :arg | Transcript show: 'Raw arg: ', arg; cr. ].
Getopt parse: args with: '-e: -d: -t' do: [ :opt :arg |
Transcript show: 'Opt: ', (opt asString), ' Arg: ', arg; cr.
"..."
].
Example run:
$ ./ios7crypt.st -e monkey
Raw arg: -e
Raw arg: monkey
Opt: e Arg: monkey
Previously, both -e
and monkey
were passed to the script, but Getopt's do:
silently dropped -e
, so the output looked like:
$ ./ios7crypt.st -e monkey
Raw arg: -e
Raw arg: monkey
Opt: Arg: monkey
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您在调试时使用#printString。它将提供面向程序员的表示(与 #displayString 相反)。
顺便说一句,您可以在 shebang 调用中使用“$@”,这样参数就不会被连接。
I suggest you use #printString when debugging. It will give a programmer-oriented representation (as opposed to #displayString).
BTW, you can use "$@" in the shebang invocation so that arguments are not joined.